SpringBoot与SpringDateJPA和Mybatis的整合

一、SpringBoot与SpringDateJPA的整合

1-1、需求

查询数据库---->得到数据------>展示到页面上

1-2、整合步骤

1-2-1、创建SpringBoot工程

具体步骤查看我上一篇随笔

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

1-2-2、添加maven依赖(pom.xml)

<dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintagegroupId>
                    <artifactId>junit-vintage-engineartifactId>
                exclusion>
            exclusions>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-jpaartifactId>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-freemarkerartifactId>
        dependency>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.6version>
        dependency>
        
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.1.1version>
        dependency>
    dependencies>

1-2-3、创建MySQL数据表

SpringBoot与SpringDateJPA和Mybatis的整合_第1张图片

 

 

 

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

 

1-2-4、在配置文件中配置数据库连接

在resources文件夹下创建application配置文件

SpringBoot与SpringDateJPA和Mybatis的整合_第2张图片

 

 

 

提供两种配置文件,可任选其一

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=(自己MySQL的用户名,一般为root)
spring.datasource.password=(自己MySQL的密码)

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: (自己MySQL的用户名,一般为root)
    password: (自己MySQL的密码)
  jpa:
    database: MySQL
    show-sql: true
    generate-ddl: true

 

1-2-5、创建User实体类对象

@Entity//指定实体类,必须指定@id
@Table(name = "user")//指定表名
@Data//编译后自动生成所有属性的get\set方法、toString方法、hashCode方法、equals方法
@AllArgsConstructor//编译后自动生成全参构造
@NoArgsConstructor//编译后自动生成无参构造
public class User {
    @Id//主键
    @GeneratedValue(strategy = GenerationType.IDENTITY)//自增
    private int uid;
    private String username;
    private String password;
}

1-2-6、创建Controller层

@RestController//相当于@ResponseBody跟@Controller
public class UserController {
    @Autowired
    private UserDao userDao;

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

1-2-7、创建持久化层

public interface UserDao extends JpaRepository { }

1-2-8、启动项目后浏览器访问

SpringBoot与SpringDateJPA和Mybatis的整合_第3张图片

 

 

 

二、SpringBoot与Mybatis的整合

在上一节的基础上,配置Mybatis

2-1、pom.xml文件

 


xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0

org.springframework.boot
spring-boot-starter-parent
2.0.7.RELEASE


com.pk
springbootday02
0.0.1-SNAPSHOT
springbootday02
Demo project for Spring Boot


1.8




org.springframework.boot
spring-boot-starter-web



org.springframework.boot
spring-boot-starter-test
test


org.junit.vintage
junit-vintage-engine





org.springframework.boot
spring-boot-starter-data-jpa



mysql
mysql-connector-java



org.springframework.boot
spring-boot-starter-freemarker



org.projectlombok
lombok
1.18.6



org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1







src/main/java

**/*.properties
**/*.xml

false


src/main/resources

**/*.*

false




org.springframework.boot
spring-boot-maven-plugin





2-2、新建一个MyUser实体类

也可以直接用上一节的实体类,注意lombok注解与自己创建的构造器、set、get等方法不能重复

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、创建Controller层类

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

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

2-4、创建mapper接口

public interface MyUserMapper {

    List getMyUserList();

}

2-5、创建mapper.xml文件

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接口的全路径名">
    <select id="getMyUserList" resultType="MyUser实体类的全路径名">
        select * from user
    select>
mapper>

2-6、创建模板文件

在resources文件夹下的templates文件夹下创建myuser.ftl文件(Freemarker模板)

SpringBoot与SpringDateJPA和Mybatis的整合_第4张图片

 

 

 

<html>
    <head>
        <title>Usertitle>
    head>
    <body>
        <table align="center" border="1px" cellpadding="5px" cellspacing="0px">
            <tr>
                <th>IDth>
                <th>用户名th>
                <th>密码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、浏览器访问结果

SpringBoot与SpringDateJPA和Mybatis的整合_第5张图片

 

 

 

 

 

 

 

你可能感兴趣的:(SpringBoot与SpringDateJPA和Mybatis的整合)