• Springboot+Mybatis-puls整合


    一、Springboot+Mybatis-puls整合

    1.1、简介Mybtis-Plus

    MyBatis-Plus 是一个 Mybatis 增强版工具,在 MyBatis 上扩充了其他功能没有改变其基本功能,为了简化开发提交效率而存在。

    官网文档地址:https://mp.baomidou.com/guide/

    MyBatis-Plus 特性:https://mp.baomidou.com/guide/#%E7%89%B9%E6%80%A7

    1.2、使用 SpringBoot 快速使用 MyBatis-Plus

    (1)准备工作

    1. 需要 Java 开发环境(JDK)
    2. 以及相应的开发工具(IDE)。
    3. 需要 maven(用来下载相关依赖的 jar 包)。
    4. 需要 SpringBoot。
    5. 可以使用 IDEA 安装一个 mybatis-plus 插件。

    (2)创建一个 SpringBoot 项目。

    方式一:去官网 https://start.spring.io/ 初始化一个,然后导入 IDE 工具即可。

    方式二:直接使用 IDE 工具创建一个。 Spring Initializer。

    推荐使用国内:https://start.aliyun.com/

    (3)添加 MyBatis-Plus 依赖(mybatis-plus-boot-starter)

    
            
                com.baomidou
                mybatis-plus-boot-starter
                3.4.2
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    (4)为了测试开发,此处使用 mysql8,需要引入 mysql 相关依赖。

    为了简化代码,引入 lombok 依赖(减少 getter、setter 等方法)。

    
            
                mysql
                mysql-connector-java
                runtime
            
            
            
                org.projectlombok
                lombok
                true
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    (5)在 里面添加如下进行yml、properties、xml自动扫描

    
            
                
                    src/main/java
                    
                        **/*.yml
                        **/*.properties
                        **/*.xml
                    
                    false
                
                
                    src/main/resources
                    
                        **/*.yml
                        **/*.properties
                        **/*.xml
                    
                    false
                
            
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    (6)完整依赖文件(pom.xml)

    
    
        4.0.0
        com.zwl
        springboot_04
        0.0.1-SNAPSHOT
        springboot_04
        Demo project for Spring Boot
    
        
            1.8
            UTF-8
            UTF-8
            2.3.7.RELEASE
        
    
        
            
            
                com.baomidou
                mybatis-plus-boot-starter
                3.4.2
            
    
            
    
    
    
    
    
            
            
                com.alibaba
                druid
                1.2.8
            
            
            
                log4j
                log4j
                1.2.17
            
            
            
                org.springframework.boot
                spring-boot-starter-jdbc
            
            
            
                org.springframework.boot
                spring-boot-starter-web
            
            
            
                mysql
                mysql-connector-java
                8.0.26
            
            
                org.springframework.boot
                spring-boot-starter-test
                test
                
                    
                        org.junit.vintage
                        junit-vintage-engine
                    
                
            
            
            
                org.projectlombok
                lombok
            
            
                com.baomidou
                mybatis-plus-extension
                3.4.3.4
            
        
    
        
            
                
                    org.springframework.boot
                    spring-boot-dependencies
                    ${spring-boot.version}
                    pom
                    import
                
            
        
    
        
    
            
                
                    src/main/java
                    
                        **/*.yml
                        **/*.properties
                        **/*.xml
                    
                    false
                
                
                    src/main/resources
                    
                        **/*.yml
                        **/*.properties
                        **/*.xml
                    
                    false
                
            
            
                
                    org.apache.maven.plugins
                    maven-compiler-plugin
                    3.8.1
                    
                        1.8
                        1.8
                        UTF-8
                    
                
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                    2.3.7.RELEASE
                    
                        com.zwl.Springboot04Application
                    
                    
                        
                            repackage
                            
                                repackage
                            
                        
                    
                
            
        
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147

    (6)使用一个表进行测试。

    仅供参考,可以定义 创建时间、修改时间等字段。

    DROP DATABASE IF EXISTS testMyBatisPlus;
    CREATE DATABASE testMyBatisPlus;
    USE testMyBatisPlus;
    DROP TABLE IF EXISTS user;
    CREATE TABLE user
    (
     ?  id BIGINT(20) NOT NULL COMMENT '主键ID',
     ?  name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
     ?  age INT(11) NULL DEFAULT NULL COMMENT '年龄',
     ?  email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
     ?  PRIMARY KEY (id)
    );
    ?
    INSERT INTO user (id, name, age, email) VALUES
    (1, 'Jone', 18, 'test1@qq.com'),
    (2, 'Jack', 20, 'test2@qq.com'),
    (3, 'Tom', 28, 'test3@qq.com'),
    (4, 'Sandy', 21, 'test4@qq.com'),
    (5, 'Billie', 24, 'test5@qq.com');
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    (7)在 application.yml 文件中配置 mysql 数据源信息。

    mysql5如下配置

    spring:
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/testMyBatisPlus?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
        username: root
        password: root
      thymeleaf:
        cache: false   #设置为false,否则会有缓存,导致页面没法及时看到更新后的效果。
    #修改端口号默认是8080
    server:
      port: 8888
    #mybatis-plus相关配置
    mybatis-plus:
      mapper-locations: classpath:mapper/*.xml #扫描mapper下的所有xml文件
      configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
      type-aliases-package: com.zwl.entity   #扫描实体类包/配置别名 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    mysql8如下配置

    # mysql连接信息
    spring:
      datasource:
     ?  driver-class-name: com.mysql.cj.jdbc.Driver
     ?  url: jdbc:mysql://localhost:3306/testMyBatisPlus?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
     ?  username: root
     ?  password: root
      thymeleaf:
     ?  cache: false ?#设置为false,否则会有缓存,导致页面没法及时看到更新后的效果。
    #修改端口号默认是8080
    server:
      port: 8888
    #mybatis-plus相关配置
    mybatis-plus:
      mapper-locations: classpath:mapper/*.xml #扫描mapper下的所有xml文件
      configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
      type-aliases-package: com.zwl.entity   #扫描实体类包/配置别名
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    (8)编写实体类、Service、ServiceImpl、UserMapper、UserMapper.xml等文件文件

    方法一:手动编写(容易出错)

    (1)编写表对应的 实体类User。

    import lombok.Data;
    ?
    @Data
    public class Users {
     ? ?private Long id;
     ? ?private String name;
     ? ?private int age;
     ? ?private String email;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    (2)编写操作实体类的 Mapper 类。

    直接继承 BaseMapper,这是 mybatis-plus 封装好的类。

    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.zx.mybatis_plus.bean.Users;
    ?
    public interface UsersMapper extends BaseMapper {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    BaseMapper封装了CRUD相关的方法

    (3)实体类、Mapper 类都写好了,就可以使用了。

    Step1:先得在启动类里扫描 Mapper 类,即添加 @MapperScan 注解

    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    @MapperScan("com.zwl.mapper")  //填写对应mapper存放位置,自动识别mapper下的所有**Mapper
    @SpringBootApplication
    public class MybatisplusDemoApplication {
    ?
     ? ?public static void main(String[] args) {
     ? ? ? ?SpringApplication.run(MybatisplusDemoApplication.class, args);
     ?  }
    ?
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    Step2:写一个测试类测试一下。

    import com.zwl.entity.User;
    import com.zwl.mapper.UserMapper;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import java.util.List;
    
    @SpringBootTest
    class Springboot05ApplicationTests {
    
    
        @Autowired
        UserMapper userMapper;
    
        //查询所有用户
        @Test
        void contextLoads() {
            List users = userMapper.selectList(null);
            for (User user : users) {
                System.out.println(user);
            }
        }
    
        //根据ID查询当前用户
        @Test
        void test1() {
            User user = userMapper.selectById(2);
            System.out.println(user);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    方法二:通过 IDEA 的Database与Mybatisx插件自动生成

    (9)总结:通过以上简单操作,就能对 user 表进行 CRUD 操作,不需要去编写 xml 文件。

    注:若遇到 @Autowired 标记的变量出现 红色下划线,但是不影响 正常运行。

    可以进入 Settings,找到 Inspection,并选择其中的 Spring Core -> Code -> Autowiring for Bean Class,将 Error 改为 Warning,即可。

    二、Mybatis-Plus内置方法

    如下:

    Mybtis-Plus常用的内置方法_的博客-CSDN博客_mybatisplus自带方法

  • 相关阅读:
    【Unity3D】Shader Graph简介
    notejs+nvm+angular+typescript.js环境 Hertzbeat 配置
    探索设计模式的魅力:状态模式揭秘-如何优雅地处理复杂状态转换
    若要对多态类进行深拷贝,应使用虚函数的clone,而不是公开的拷贝构造赋值
    2024 遗传编程实战(一)基因实战
    Java基础篇:什么是hashCode 以及 hashCode()与equals()的联系
    二分套网络流:ABC320G
    栈、队列、字符串
    2.1 继续Hello World
    Axure教程—单色折线图(中继器)
  • 原文地址:https://blog.csdn.net/m0_67401134/article/details/126041016