Full Stack MVC
This section describes a technical overview of starting a new project with Java, Spring MVC, and JPA.
Overview
Initialize local workspace
Create Git repository
Create and configure JPA project
Create and configure Spring Boot project
Connect Spring Boot with JPA project
Create and configure MySQL database
Update Controllers
Write JUnit test and run smoke test
Initialize local workspace
Start by preparing space on your local environment where your project files will be stored.
Open a terminal
Change directory to where your workspace will be
cd ~/Documents/sites
Create
.gitignorefileatom .gitignore
Add the following and save the file:
.DS_Store Servers target build bin .metadata # .settings .gradle *.war *.bak
Create Git repository
We will use Git as the version control system.
Create a new repository at GitHub.com
Initialize your local folder as a
gitrepository and perform an initial commit:
Create and configure JPA project
Set up the Java Persistence API project in Spring Tool Suite (STS).
Set or confirm in STS >
Preferences:Gradle > Specific Gradle version: 7.4.2Java > Compiler > Compiler compliance level: 1.8General > Workspace > Show full workspace pathConsole (search for) > Limit console output (uncheck)
Create JPA Project
File > New > Other > JPA Project > NextAdd source folders to structure:
src/main/javasrc/main/resourcessrc/test/javasrc/test/resources
Click Next
JPA Implementation > Disable Library Configuration (select)
Finish (don't open JPA Perspective if prompted)
In the
Project Explorerpanel:Drag the
META-INFfolder fromsrc/main/javadown one level tosrc/main/resources
Add Gradle using the following steps:
Right-click your project folder and select
Configure > Add Gradle NatureReveal Gradle Tasks by clicking:
Window > Show View > Other > Gradle TasksOn the Gradle Tasks panel, select:
Build > init (double-click)Open the Console panel: accept defaults for the 4 prompts by hitting
enterRight-click the project folder and select:
Refresh
Update build.gradle:
Run Gradle Refresh by right-clicking the project and selecting Gradle > Refresh Gradle Project
Create log4j.properties file under src/main/resources and src/test/resources with the following contents:
Under the META-INF folder, update persistence.xml using the following as a guideline:
Copy the fully-qualified name from your listed entity and navigate to src/main/java (right-click) > New > Class
In the Name, paste the text from your clipboard as copied from the persistence.xml file above (it should auto-populate the package name), i.e. com.myorganization.myproject.entities.User
Stub out JUnit test
Create a new package under
src/test/javafor entities, i.e.com.myorganization.myproject.entitiesCreate
New > JUnit TestEnsure
JUnit Jupiter Testis selectedName:
UserTestAdd:
@Beforeand@Afterannotations
Run Gradle refresh again, then commit and push your updates to the project repository.
Create and configure Spring Boot project
Get the package name from the JPA project you just created, i.e.
com.myorganization.myprojectIn STS, click
New > Spring Starter ProjectProject name: Name of your project
Gradle buildship 2.xPackage name: Must match with JPA project
Create packages and add empty stub class to each package
Include Spring dependencies:
Spring Web
Spring Data JPA
MySQL Driver
Click
Finish
Configure JPA and JSTL dependencies. In build.gradle add the following:
Save and perform Gradle refresh.
Connect Spring Boot and JPA Projects
JPA project:
settings.gradle> copy project name, i.e.'JPAMyProject'Boot project:
settings.gradle> add:includeFlat 'JPAMyProject'Boot project:
build.gradle> add:implementation project(':JPAMyProject')
Save all files and perform another Gradle refresh. Verify by expanding the Projects and External Dependencies folder (in the Spring Boot project) and see JPAMyProject at the bottom.
Commit (Create Spring Boot MVC Project, add JPA Project dependency) and push.
Create web folder
Create the webapp/WEB-INF folder under src/main
In Package Explorer, expand
src/mainand right-click to selectNew > Folderand give it a name:webapp/WEB-INFIn it, create
New > Other > JSP Fileand give it a name:homeAdd a tag to pass data into from the backend:
${DEBUG}
Create Packages
Packages are folders that house separate concerns in a project. Create the following packages in the same level as com.myorganization.myproject:
Controllers
com.myorganization.myproject.controllersHomeController.java
Data
com.myorganization.myproject.dataUserDAO.javaUserDaoJpaImpl.java
Controllers
Data
In the data package, select New > Interface and give it a name (i.e. UserDAO
Add a method to find a user by ID (import with CMD + O)
User findById(int userId);
In the package explorer, expand and right-click the purple I icon and select New > Class and give it a name:
UserDaoJpaImpl
@Service@Transactional@PersistenceContextprivate Entity Manager em;
File UserDaoImpl.java
Update application.properties
File src/main/resources/application.properties
Commit and push
git commit -m 'Configure Spring, stub MVC code'
git pull
git push
Last updated
Was this helpful?