Integration of SpringBoot with SpringDateJPA and Mybatis

I. integration of SpringBoot and SpringDateJPA

1-1, demand

Query database - > get data -------- display on the page

1-2. Integration steps

1-2-1. Create a SpringBoot project

See my last essay for specific steps

https://www.cnblogs.com/pengpengdeyuan/p/11798804.html

1-2-2. Add maven dependency (pom.xml)

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--Add to springdatajpa Dependence-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--freemarker Template dependency-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        <!--Lombok rely on-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.6</version>
        </dependency>
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
    </dependencies>

1-2-3. Create MySQL data table

 

 

 

CREATE TABLE user(
    uid INT PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(30),
    `password` VARCHAR(10)
);

 

1-2-4. Configure database connection in configuration file

Create application profile under resources folder

 

 

 

There are two kinds of configuration files, either of which can be selected

application.properties:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/user
spring.datasource.username=(Own MySQL The user name of root)
spring.datasource.password=(Own MySQL Password)

spring.jpa.database=MySQL
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true

application.yml

spring:
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/user?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
    username: (Own MySQL The user name of root)
    password: (Own MySQL Password)
  jpa:
    database: MySQL
    show-sql: true
    generate-ddl: true

 

1-2-5. Create User entity class object

@Entity//Specify entity class, you must specify@id
@Table(name = "user")//Specified table name
@Data//Automatically generate the get\set Method, toString Method, hashCode Method, equals Method
@AllArgsConstructor//Automatic generation of full parameter construction after compilation
@NoArgsConstructor//Automatically generate nonparametric construction after compilation
public class User {
    @Id//Primary key
    @GeneratedValue(strategy = GenerationType.IDENTITY)//Self increment
    private int uid;
    private String username;
    private String password;
}

1-2-6. Create Controller layer

@RestController//Amount to@ResponseBody Follow@Controller
public class UserController {
    @Autowired
    private UserDao userDao;

    @RequestMapping(value = "/user")
    public List<User> getUser(){
        return userDao.findAll();
    }
}

1-2-7. Create a persistence layer

public interface UserDao extends JpaRepository<User,Integer> { }

1-2-8. Browser access after starting the project

 

 

 

II. Integration of SpringBoot and Mybatis

Based on the previous section, configure Mybatis

2-1. pom.xml file

 

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.pk</groupId>
<artifactId>springbootday02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootday02</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--Add to springdatajpa Dependence-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--freemarker Template dependency-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!--Lombok rely on-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>

<build>
<!--Set up Mapper Mapping is available. java perhaps resources Under folder-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>

</build>

</project>

2-2. Create a new MyUser entity class

You can also directly use the entity class in the previous section. Note that lombok annotation cannot be repeated with the constructor, set, get and other methods you created

public class MyUser {
    private int uid;
    private String username;
    private String password;

    public MyUser(int uid, String username, String password) {
        this.uid = uid;
        this.username = username;
        this.password = password;
    }

    public MyUser() {
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    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;
    }

    @Override
    public String toString() {
        return "MyUser{" +
                "uid=" + uid +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

 

2-3. Create Controller layer class

@Controller
@RequestMapping("/myuser")
public class MyUserController {
    @Autowired
    private MyUserMapper myUserMapper;

    @RequestMapping(value = "/getMyUserList",method = RequestMethod.GET)
    public String getMyUserList(Model model){
        List<MyUser> userList = myUserMapper.getMyUserList();
        model.addAttribute("userList",userList);
        return "myuser";
    }
}

2-4. Create mapper interface

public interface MyUserMapper {

    List<MyUser> getMyUserList();

}

2-5. Create mapper.xml file

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="MyUserMapper Full pathname of the interface">
    <select id="getMyUserList" resultType="MyUser Full pathname of entity class">
        select * from user
    </select>
</mapper>

2-6. Create template file

Create the myuser.ftl file (Freemarker template) in the templates folder under the resources folder

 

 

 

<html>
    <head>
        <title>User</title>
    </head>
    <body>
        <table align="center" border="1px" cellpadding="5px" cellspacing="0px">
            <tr>
                <th>ID</th>
                <th>User name</th>
                <th>Password</th>
            </tr>
            <#list userList as user>
                <tr>
                    <td>${user.uid}</td>
                    <td>${user.username}</td>
                    <td>${user.password}</td>
                </tr>
            </#list>
        </table>
    </body>
</html>

2-7. Browser access results

Tags: Java Spring MySQL Mybatis Maven

Posted on Tue, 05 Nov 2019 06:23:14 -0800 by remlabm