Project Setup
Workspace Folder and Git Setup¶
-
Create a workspace for the project:
mkdir ~/SD/Java/JPACrudProject -
Create a
.gitignore:cd ~/SD/Java/JPACrudProject/ atom .gitignore
.DS_Store
Servers
target
build
bin
default
.metadata
.settings
.gradle
*.war
*.bak
-
Create a Github repository named JPACRUDProject.
-
Copy the repository initialization commands from the repo page and run them in your ~/SD/Java/JPACrudProject/ folder.
-
Do another
git add .and agit commit -m "Add .gitignore". -
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¶
- Create a folder for your database development files:
mkdir ~/SD/Java/JPACrudProject/DB
-
Create a MySQL Workbench schema model.
-
Set the name of the database physical schema to an appropriate name (we'll use
myprojectdbas an example). -
Create
username@localhostuser withtable.modifyrole for your database, where "username" is specific to your project.- Make a note of your:
- Schema name
- Username
- Password
- Make a note of your:
-
Save the MWB file to
~/SD/Java/JPACrudProject/DB/myprojectdb.mwb(where myprojectdb is a name appropriate for your schema.) -
Create your entity table with an appropriate table name.
- Give it an
idINTEGER primary key column with AutoIncrement. - Add one other column for now so you can test your Persistence Unit configuration.
- 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.
- Give it an
Project Workspace Setup¶
-
In STS, use File | Open Workspace to open your JPACrudProject folder while also leaving VideoStore open for reference.
-
In the STS Preferences, change the following settings:
-
Java | Compiler Compiler compliance Level:
1.8 - Gradle:
Specific Gradle version(current latest version) - Run/Debug | Console: UN-check Limit console output.
- General | Workspace: Show full workspace
- Apply and Close
JPA Project Setup¶
Setup a JPA project for your JPA entity(s) and tests.¶
- Create a new JPA Project with an appropriate name.
- Add new source folders to the build path:
src/main/resourcessrc/test/javasrc/test/resources
- Under JPA implementation, choose Disable Library Configuration
- Do NOT open the JPA perspective.
- Move the
META-INFfolder fromsrc/main/javatosrc/main/resources.
Configure Gradle¶
- Add Gradle Nature to the project.
- Open the Gradle Tasks view.
- Under the
build setuptask group, run theinittask. - Go to the Console view and press Return to answer the required questions until you get
BUILD SUCCESSFUL. - Refresh the Package Explorer view to see the new gradle files.
- Copy the contents of the
build.gradlefrom the JPAVideoStore project into your new project'sbuild.gradle; save and Gradle Refresh.
Configure the JPA Persistence Unit¶
- Copy the
log4j.propertiesfile from JPAVideoStore into your new project'ssrc/main/resourcesandsrc/test/resourcesfolders. - From the JPAVideoStore
persistence.xmlcopy the contents from between the<persistence-unit>tags (not the whole file) into your new project'spersistence.xml, between the opening and closing<persistence-unit>tags. - Change the schema name in the JDBC URL to the name you gave your schema.
- Set the values for user and password to the application user you created in your model.
Stub out and test your entity¶
- 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. - Create the new package and entity class in
src/main/java. - Add the id field and one other field in the class (leave the rest for later).
- Add no-arg ctor, gets/sets, toString, etc.
- Annotate your entity.
- Create a matching package under
src/test/javaand create a new JUnit test case for your entity. - Build out the JUnit test until it passes.
Create a Spring Boot Project for your MVC controller, DAO, and DAO implementation.¶
- Copy the base package name from your entity class (the package name up to but not including
.entities) - Create a new Spring Starter Project.
- Paste the base package name into the Package field.
- Give the project an appropriate name (this project name will appear in the URL once you deploy the project.)
- Set the Type to Gradle
- Set the Java Version to 8.
- Set the Packaging to War.
-
Add the Spring Web, Spring Data JPA, and MySQL Driver dependencies.
-
Add JSP dependencies to the
dependenciesblock in the Boot project'sbuild.gradle:
implementation 'javax.servlet:jstl:1.2'
implementation 'org.apache.tomcat.embed:tomcat-embed-jasper'
-
Connect the projects using your Boot project's
settings.gradleandbuild.gradle. -
Do a Gradle refresh.
-
Update the Boot project's
application.propertiesusing the one from the BootMVCVideoStore application. -
Change the schema name in the datasource URL, the username, and the password to match your MySQL schema model.
-
Create
src/main/webapp/WEB-INF/for your JSPs. Static content (HTML, CSS, image files) will go insrc/main/webapp/. -
Create packages for your controller and DAO, and get a basic controller route and JSP view working.
Next Steps¶
-
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.
-
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.
-
With your DB and entity complete you can focus on implementing CRUD using MVC.
-
As you work, commit and push frequently.