Skip to content

Project Setup

Workspace Folder and Git Setup

  1. Create a workspace for the project:

    mkdir ~/SD/Java/JPACrudProject
    

  2. Create a .gitignore:

    cd ~/SD/Java/JPACrudProject/
    atom .gitignore
    

.DS_Store
Servers
target
build
bin
default
.metadata
.settings
.gradle
*.war 
*.bak
  1. Create a Github repository named JPACRUDProject.

  2. Copy the repository initialization commands from the repo page and run them in your ~/SD/Java/JPACrudProject/ folder.

  3. Do another git add . and a git commit -m "Add .gitignore".

  4. Be sure to add, commit, and push frequently throughout this project; for example, after completing each of the following setup sections would be a good time for a git commit.

MySQL Schema Setup

  1. Create a folder for your database development files:
mkdir ~/SD/Java/JPACrudProject/DB
  1. Create a MySQL Workbench schema model.

  2. Set the name of the database physical schema to an appropriate name (we'll use myprojectdb as an example).

  3. Create username@localhost user with table.modify role for your database, where "username" is specific to your project.

    1. Make a note of your:
      • Schema name
      • Username
      • Password
  4. Save the MWB file to ~/SD/Java/JPACrudProject/DB/myprojectdb.mwb (where myprojectdb is a name appropriate for your schema.)

  5. Create your entity table with an appropriate table name.

    1. Give it an id INTEGER primary key column with AutoIncrement.
    2. Add one other column for now so you can test your Persistence Unit configuration.
    3. In the Inserts tab, add one row of data with a column value we can check in a JUnit entity test; make sure to hit Apply Changes, then save your MWB file and forward engineer the schema.

Project Workspace Setup

  1. In STS, use File | Open Workspace to open your JPACrudProject folder while also leaving VideoStore open for reference.

  2. In the STS Preferences, change the following settings:

  3. Java | Compiler Compiler compliance Level: 1.8

  4. Gradle: Specific Gradle version (current latest version)
  5. Run/Debug | Console: UN-check Limit console output.
  6. General | Workspace: Show full workspace
  7. Apply and Close

JPA Project Setup

Setup a JPA project for your JPA entity(s) and tests.

  1. Create a new JPA Project with an appropriate name.
  2. Add new source folders to the build path:
    • src/main/resources
    • src/test/java
    • src/test/resources
  3. Under JPA implementation, choose Disable Library Configuration
  4. Do NOT open the JPA perspective.
  5. Move the META-INF folder from src/main/java to src/main/resources.

Configure Gradle

  1. Add Gradle Nature to the project.
  2. Open the Gradle Tasks view.
  3. Under the build setup task group, run the init task.
  4. Go to the Console view and press Return to answer the required questions until you get BUILD SUCCESSFUL.
  5. Refresh the Package Explorer view to see the new gradle files.
  6. Copy the contents of the build.gradle from the JPAVideoStore project into your new project's build.gradle; save and Gradle Refresh.

Configure the JPA Persistence Unit

  1. Copy the log4j.properties file from JPAVideoStore into your new project's src/main/resources and src/test/resources folders.
  2. From the JPAVideoStore persistence.xml copy the contents from between the <persistence-unit> tags (not the whole file) into your new project's persistence.xml, between the opening and closing <persistence-unit> tags.
  3. Change the schema name in the JDBC URL to the name you gave your schema.
  4. Set the values for user and password to the application user you created in your model.

Stub out and test your entity

  1. Choose an approriate package name for your project and change the <class> declaration with a new package name and the name of your entity class.
  2. Create the new package and entity class in src/main/java.
  3. Add the id field and one other field in the class (leave the rest for later).
  4. Add no-arg ctor, gets/sets, toString, etc.
  5. Annotate your entity.
  6. Create a matching package under src/test/java and create a new JUnit test case for your entity.
  7. Build out the JUnit test until it passes.

Create a Spring Boot Project for your MVC controller, DAO, and DAO implementation.

  1. Copy the base package name from your entity class (the package name up to but not including .entities)
  2. Create a new Spring Starter Project.
  3. Paste the base package name into the Package field.
  4. Give the project an appropriate name (this project name will appear in the URL once you deploy the project.)
  5. Set the Type to Gradle
  6. Set the Java Version to 8.
  7. Set the Packaging to War.
  8. Add the Spring Web, Spring Data JPA, and MySQL Driver dependencies.

  9. Add JSP dependencies to the dependencies block in the Boot project's build.gradle:

implementation 'javax.servlet:jstl:1.2'
implementation 'org.apache.tomcat.embed:tomcat-embed-jasper'
  1. Connect the projects using your Boot project's settings.gradle and build.gradle.

  2. Do a Gradle refresh.

  3. Update the Boot project's application.properties using the one from the BootMVCVideoStore application.

  4. Change the schema name in the datasource URL, the username, and the password to match your MySQL schema model.

  5. Create src/main/webapp/WEB-INF/ for your JSPs. Static content (HTML, CSS, image files) will go in src/main/webapp/.

  6. Create packages for your controller and DAO, and get a basic controller route and JSP view working.

Next Steps

  1. Finish designing your database schema. This will be a single-table application. After completing the column definitions of your entity table, add data for them in the Inserts tab and forward engineer.

  2. Update your Java entity class adding fields for your new columns. Don't forget getters/setters and a toString, and update the JUnit test appropriately.

  3. With your DB and entity complete you can focus on implementing CRUD using MVC.

  4. As you work, commit and push frequently.


Prev -- Up