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 .gitignore file

      • atom .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 git repository 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.2

    • Java > Compiler > Compiler compliance level: 1.8

    • General > Workspace > Show full workspace path

    • Console (search for) > Limit console output (uncheck)

  • Create JPA Project

    • File > New > Other > JPA Project > Next

    • Add source folders to structure:

      • src/main/java

      • src/main/resources

      • src/test/java

      • src/test/resources

    • Click Next

      • JPA Implementation > Disable Library Configuration (select)

    • Finish (don't open JPA Perspective if prompted)

  • In the Project Explorer panel:

    • Drag the META-INF folder from src/main/java down one level to src/main/resources

Add Gradle using the following steps:

  • Right-click your project folder and select Configure > Add Gradle Nature

  • Reveal Gradle Tasks by clicking: Window > Show View > Other > Gradle Tasks

  • On the Gradle Tasks panel, select: Build > init (double-click)

  • Open the Console panel: accept defaults for the 4 prompts by hitting enter

  • Right-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/java for entities, i.e. com.myorganization.myproject.entities

  • Create New > JUnit Test

    • Ensure JUnit Jupiter Test is selected

    • Name: UserTest

    • Add: @Before and @After annotations

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.myproject

  • In STS, click New > Spring Starter Project

    • Project name: Name of your project

    • Gradle buildship 2.x

    • Package 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/main and right-click to select New > Folder and give it a name: webapp/WEB-INF

  • In it, create New > Other > JSP File and give it a name: home

  • Add 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.controllers

      • HomeController.java

  • Data

    • com.myorganization.myproject.data

      • UserDAO.java

      • UserDaoJpaImpl.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

  • @PersistenceContext

    • private 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?