Spring Boot CRUD Example With MySQL

This example will tell you how to use spring boot data JPA to implement insert, update, delete and select database table operation on MySQL database table. With spring boot data JPA the database table operation command has been wrapped to method, you just need to create a java interface which extends basic spring boot data JPA Repository interface ( for example CrudRepository), then you only need to define database table operation method( such as findBy<column name>, deleteBy<column name> etc) in the custom Repository interface, and the method name must follow special naming rules. You do not need to write sql command in the Repository interface.

1. Create MySQL Database Table.

  1. Create a MySQL database with name dev2qa_example.
    create mysql dev2qa_example database
  2. Create a table user_account in above dev2qa_example database. Remember check the AI checkbox for the id column. Otherwise an error Spring Boot JPA Table ‘dbname.hibernate_sequence’ Doesn’t Exist will be thrown.
    mysql table user_account structure

2. Create Spring Boot Project.

  1. Launch spring tool suite, click File —> New —> Spring Starter Project menu item to open below New Spring Starter Project wizard. Input related project info like below. And click Next button.
    sprint boot crud mysql project initialize wizard
  2. Add JPA, MySQL and Web libraries in the dependencies wizard. And click Finish button to complete the project initialization.
    sprint boot crud mysql project dependencies lib

3. Spring Boot JPA CRUD Example Project Files.

Below are the source files in this project. We will introduce them one by one.

sprint boot crud mysql example files

3.1 SpringBootCrudMySqlApplication.java

This is the spring boot example starting java class. It will be loaded and run first in spring boot application.

package com.dev2qa.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

//@SpringBootApplication
@Configuration
@ComponentScan(basePackages = { "com.dev2qa.example" })
@EnableAutoConfiguration
public class SpringBootCrudMySqlApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootCrudMySqlApplication.class, args);
    }
}

3.2 UserAccountController.java

This is the spring mvc controller java class which will map user request url to processing method.

package com.dev2qa.example.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dev2qa.example.entity.UserAccount;
import com.dev2qa.example.repository.UserAccountRepository;

@Controller
@RequestMapping(path = "/userAccount")
public class UserAccountController {

    @Autowired
    UserAccountRepository userAccountRepository;

    /*
     * Mapping url exmaple:
     * http://localhost:8080/userAccount/add?userName=Jerry&password=888888&email=
     * [email protected]
     * http://localhost:8080/userAccount/add?userName=Richard&password=888888&email=
     * [email protected]
     */
    @GetMapping(path = "/add")
    @ResponseBody
    public String addUser(@RequestParam String userName, @RequestParam String password, @RequestParam String email) {

        UserAccount userAccount = new UserAccount();
        userAccount.setUsername(userName);
        userAccount.setPassword(password);
        userAccount.setEmail(email);

        userAccountRepository.save(userAccount);

        String ret = "User account has been added, user name = " + userName + ", password = " + password + ", email = "
                + email;

        return ret;

    }

    /*
     * Mapping url exmaple: http://localhost:8080/userAccount/findAll
     */
    @GetMapping(path = "/findAll")
    @ResponseBody
    public String findAllUser() {

        StringBuffer retBuf = new StringBuffer();

        List<UserAccount> userAccountList = (List<UserAccount>) userAccountRepository.findAll();

        if (userAccountList != null) {
            for (UserAccount userAccount : userAccountList) {
                retBuf.append("user name = ");
                retBuf.append(userAccount.getUsername());
                retBuf.append(", password = ");
                retBuf.append(userAccount.getPassword());
                retBuf.append(", email = ");
                retBuf.append(userAccount.getEmail());
                retBuf.append("\r\n");
            }
        }

        if (retBuf.length() == 0) {
            retBuf.append("No record find.");
        } else {
            retBuf.insert(0, "<pre>");
            retBuf.append("</pre>");
        }

        return retBuf.toString();
    }

    /*
     * Mapping url exmaple:
     * http://localhost:8080/userAccount/findByName?userName=Jerry
     */
    @GetMapping(path = "/findByName")
    @ResponseBody
    public String findByName(@RequestParam String userName) {

        StringBuffer retBuf = new StringBuffer();

        List<UserAccount> userAccountList = (List<UserAccount>) userAccountRepository.findByUsername(userName);

        if (userAccountList != null) {
            for (UserAccount userAccount : userAccountList) {
                retBuf.append("user name = ");
                retBuf.append(userAccount.getUsername());
                retBuf.append(", password = ");
                retBuf.append(userAccount.getPassword());
                retBuf.append(", email = ");
                retBuf.append(userAccount.getEmail());
                retBuf.append("\r\n");
            }
        }

        if (retBuf.length() == 0) {
            retBuf.append("No record find.");
        }

        return retBuf.toString();
    }

    /*
     * Mapping url exmaple:
     * http://localhost:8080/userAccount/findByNameAndPassword?userName=Jerry&
     * password=888888
     */
    @GetMapping(path = "/findByNameAndPassword")
    @ResponseBody
    public String findByNameAndPassword(@RequestParam String userName, @RequestParam String password) {

        StringBuffer retBuf = new StringBuffer();

        List<UserAccount> userAccountList = (List<UserAccount>) userAccountRepository
                .findByUsernameAndPassword(userName, password);

        if (userAccountList != null) {
            for (UserAccount userAccount : userAccountList) {
                retBuf.append("user name = ");
                retBuf.append(userAccount.getUsername());
                retBuf.append(", password = ");
                retBuf.append(userAccount.getPassword());
                retBuf.append(", email = ");
                retBuf.append(userAccount.getEmail());
                retBuf.append("<br/>");
            }
        }

        if (retBuf.length() == 0) {
            retBuf.append("No record find.");
        }

        return retBuf.toString();
    }

    /*
     * Mapping url exmaple:
     * http://localhost:8080/userAccount/updateUser?userName=Jerry&password=hello&
     * [email protected]
     */
    @GetMapping(path = "/updateUser")
    @ResponseBody
    public String updateUser(@RequestParam String userName, @RequestParam String password, @RequestParam String email) {

        StringBuffer retBuf = new StringBuffer();

        List<UserAccount> userAccountList = userAccountRepository.findByUsername(userName);

        if (userAccountList != null) {
            for (UserAccount userAccount : userAccountList) {
                userAccount.setUsername(userName);
                userAccount.setPassword(password);
                userAccount.setEmail(email);
                userAccountRepository.save(userAccount);
            }
        }

        retBuf.append("User data update successfully.");

        return retBuf.toString();
    }

    /*
     * Mapping url exmaple:
     * http://localhost:8080/userAccount/deleteByUserName?userName=Richard
     */
    @GetMapping(path = "/deleteByUserName")
    @ResponseBody
    public String deleteByUserName(@RequestParam String userName) {

        StringBuffer retBuf = new StringBuffer();

        userAccountRepository.deleteByUsername(userName);

        retBuf.append("User data has been deleted successfully.");

        return retBuf.toString();
    }

    /*
     * Mapping url exmaple:
     * http://localhost:8080/userAccount/deleteByUserNameAndPassword?userName=
     * Richard&password=888888
     */
    @GetMapping(path = "/deleteByUserNameAndPassword")
    @ResponseBody
    public String deleteByUserNameAndPassword(@RequestParam String userName, @RequestParam String password) {

        StringBuffer retBuf = new StringBuffer();

        userAccountRepository.deleteByUsernameAndPassword(userName, password);

        retBuf.append("User data has been deleted successfully.");

        return retBuf.toString();
    }

}

3.3 UserAccount.java

This is the entity java class that will be mapped to MySQL table user_account. Please note the id generate strategy must be GenerationType.IDENTITY, if you use Generation.AUTO and the MySQL table id column is set to auto increment, then an error will be thrown.

package com.dev2qa.example.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/* Map this entity class to user_account table. */
@Entity(name = "user_account")
public class UserAccount {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @javax.persistence.Column(name = "user_name")
    private String username;

    private String password;

    private String email;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}

3.4 UserAccountRepository.java

This is the custom spring boot data JPA repository interface that extends CrudRepository. You just need to define related method then spring framework will automatically run related sql command to implement the method. This make coding more quickly.

package com.dev2qa.example.repository;

import java.util.List;

import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;

import com.dev2qa.example.entity.UserAccount;

public interface UserAccountRepository extends CrudRepository<UserAccount, Long> {

    /*
     * Get user list by user name. Please note the format should be
     * findBy<column_name>.
     */
    List<UserAccount> findByUsername(String username);

    /*
     * Get user list by user name and password. Please note the format should be
     * findBy<column_name_1>And<column_name_2>.
     */
    List<UserAccount> findByUsernameAndPassword(String username, String password);

    @Transactional
    void deleteByUsernameAndPassword(String username, String password);

    @Transactional
    void deleteByUsername(String username);

}

3.5 application.properties

This is the resources file which contains MySQL jdbc data source connection data used by the example.

# MySQL jdbc connection url.
spring.datasource.url=jdbc:mysql://localhost:3306/dev2qa_example
# MySQL jdbc driver class name.
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
# MySQL database username and password
spring.datasource.username=root
spring.datasource.password=root

3.6 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>SpringBootCRUDMySQL</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>SpringBootCRUDMySQL</name>
    <description>Spring boot access mysql with crud operation.</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3.7 Run The Example.

  1. Right click the project name.
  2. Click Run As —> Spring Boot App menu item from the popup menu list.
  3. After the application startup, input the mapping url for related UserAccountController java class method in a web browser to see the result.
READ :   How To Disable Or Customize Spring Boot Whitelabel Error Page

Reference

  1. How To Install MySQL On Ubuntu
  2. Resolve Spring Boot JPA Table ‘dbname.hibernate_sequence’ Doesn’t Exist Error
(Visited 28 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.