# 整合 MyBatis-Plus

MyBatis-Plus 是一个MyBatis增强工具,在MyBatis的基础上只做增强不做改变,为简化开发、提高效率而生…… MP官网:https://mp.baomidou.com/

# 简单使用

  1. 创建Spring Boot工程
  2. 引入依赖
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>${mybatis-plus.version}</version>
</dependency>
1
2
3
4
5

注意:mybatis-plus-boot-starter 里面已经引入了 mybatis 的所有相关依赖,不需要额外引入mybatis依赖 mybatis-spring-boot-starter

  1. 配置数据源
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/mybatis_plus?useUnicode=true&characterEncoding=UTF-8&useSSL=true
    username: root
    password: root
mybatis-plus:
  config-location: classpath:mapper/*Mapper.xml 
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
1
2
3
4
5
6
7
8
9
10
  1. 创建实体类
@Data
@Builder
@TableName("student")
public class User {

    @TableId(type = IdType.AUTO)
    private Integer id;

    private Integer deptId;

    private String name;

    private String remark;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  1. 创建Mapper接口,继承BaseMapper接口,需要扫描@MapperScan
public interface UserMapper extends BaseMapper<User> {
}
1
2
  1. 单元测试
@SpringBootTest
class MybatisPlusMapperTests {

    @Autowired
    UserMapper userMapper;

    @Test
    void testSelectList() {
        //通过条件构造器查询,没有条件,参数为null
        List<User> users = userMapper.selectList(null);
        users.forEach(System.out::println);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# Mapper层增删改查

  • BaseMapper
@SpringBootTest
class MybatisPlusMapperTests {

    @Autowired
    UserMapper userMapper;

    @Test
    void testSelectList() {
        //通过条件构造器查询,没有条件,参数为null
        List<User> users = userMapper.selectList(null);
        users.forEach(System.out::println);
    }

    @Test
    void testInsert() {
        //新增用户信息,INSERT INTO user ( id, name, age, email ) VALUES ( ?, ?, ?, ? )
        /*User u = new User();
        u.setName("张三");
        u.setAge(20);
        u.setEmail("zhangsan@163.com");
        int result = userMapper.insert(u);
        System.out.println("result:" + result);
        System.out.println(u);*/
    }

    @Test
    void testDelete() {
        // DELETE FROM user WHERE id=?
        /*int result = userMapper.deleteById(1507566337273229313L);
        System.out.println("result:" + result);*/

        //DELETE FROM user WHERE name = ? AND age = ?
        /*Map<String, Object> map = new HashMap<>();
        map.put("name", "张三");
        map.put("age", 23);
        int result = userMapper.deleteByMap(map);
        System.out.println("result:" + result);*/

        //ELETE FROM user WHERE id IN ( ? , ? , ? )
        /*List<Long> idList = Arrays.asList(1L, 2L, 3L);
        int result = userMapper.deleteBatchIds(idList);
        System.out.println("result:" + result);*/

    }

    @Test
    void testUpdate() {
        //UPDATE user SET name=?, email=? WHERE id=?
        /*User user = new User();
        user.setId(4L);
        user.setName("biubiu");
        user.setEmail("biubiu@163.com");
        int result = userMapper.updateById(user);
        System.out.println("result:" + result);*/
    }

    @Test
    void testSelect() {
        //SELECT id,name,age,email FROM user WHERE id=?
        /*User user = userMapper.selectById(1L);
        System.out.println(user);*/

        //SELECT id,name,age,email FROM user WHERE id IN ( ? , ? , ? )
        /*List<Long> ids = Arrays.asList(1L, 2L, 3L);
        List<User> users = userMapper.selectBatchIds(ids);
        users.forEach(System.out::println);*/

        // SELECT id,name,age,email FROM user WHERE name = ? AND age = ?
        /*Map<String, Object> map = new HashMap<>();
        map.put("name", "jack");
        map.put("age", 20);
        List<User> users = userMapper.selectByMap(map);
        users.forEach(System.out::println);*/

        // 查询所有数据 SELECT id,name,age,email FROM user
        /*List<User> users = userMapper.selectList(null);
        users.forEach(System.out::println);*/
    }

    @Test
    void contextLoads() {
    }

}
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
  • 自定义mapper

mapper.xml文件的配置:mp有个默认的配置 classpath*:/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="com.biubiu.mpdemo.mapper.UserMapper">

    <select id="selectMapById" resultType="java.util.Map">
        SELECT id,name,age,email FROM user WHERE id= #{id}
    </select>

</mapper>
1
2
3
4
5
6
7
8
9
10

# Service 层使用

  1. 自定义Service接口,实现IService
public interface UserService extends IService<User> {
}
1
2
  1. 自己的实现类实现自己的接口,并且继承ServiceImpl<M,T>
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
1
2
3
  1. 单元测试
@SpringBootTest
public class MyBatisPlusServiceTests {

    @Autowired
    UserService userService;

    @Test
    void testCount() {
        // SELECT COUNT( * ) FROM user
        long count = userService.count();
        System.out.println("总记录数:" + count);
    }

    @Test
    void testBatchInsert() {
        //INSERT INTO user ( id, name, age ) VALUES ( ?, ?, ? ) 循环执行
        List<User> users = new ArrayList<>();
        for (int i = 1; i <= 10; i++) {
            User user = new User();
            user.setName("bjq" + i);
            user.setAge(20 + 1);
            users.add(user);
        }
        boolean b = userService.saveBatch(users);
        System.out.println(b);
    }
}
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

# 注解

  1. 表名和实体对应 @TableName("t_user")

除了注解外 mybatis-plus.global-config.db-config.table-prefix=t_

  1. 将实体对应属性指定主键 @TableId

@TableId("uid")@TableId(value="uid"")是一样的,value属性用来指定主键字段 @TableId(value = "user_id", type = IdType.AUTO) type表示主键生成类型

描述
IdType.ASSIGN_ID 默认的,基于雪花算法生成id
IdType.AUTO 自增主键,需要数据库设置id自增,否则无效
IdType.INPUT 用户输入的ID
IdType.NONE 不设置主键类型

除了注解 mybatis-plus.global-config.db-config.id-type=auto 全局也能配置

雪花算法:由Twitter公布的分布式主键生成算法、保证不同表的主键的不重复性,以及相同表的主键的有序性

  1. 属性和字段不一样 @TableField("user_name")

mp默认驼峰命名开启 mybatis-plus.configuration.map-underscore-to-camel-case=true

@TableField(exist = false) 标识表中无此字段

@TableField(condition = SqlCondition.LIKE) 设置模糊查询

  1. 逻辑删除 @TableLogic
public class Student {
    //@TableLogic(value = "0", delval = "1")
    @TableLogic
    private Integer isDeleted;
}
1
2
3
4
5

# 条件构造器 Wrapper

# 附录逆向工程-代码生成器

  • MyBatisPlus代码生成器: mybatis-plus-generator
  • 逆向功能所需的模板引擎: mybatis-plus-generator

需要的依赖我们已经引入了 mybatis-plus-generatorfreemarker 模板引擎

在test/java 路径下新建一个类 MpCodeGenerator

package com.biubiu.generator;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * <p>
 * Generator
 * </p>
 *
 * @author biubiu
 * @date 2021/7/28
 */
public class MpCodeGenerator {
    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + ":");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "!");
    }

    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        // gc.setOutputDir("D:\\test");
        gc.setAuthor("biubiu");
        gc.setOpen(false);
        gc.setServiceName("%sService");
        gc.setSwagger2(true);
        // 实体属性 Swagger2 注解
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/admin?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(scanner("模块名"));
        pc.setParent("com.biubiu.admin");
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };

        // 如果模板引擎是 freemarker
        String templatePath = "/templates/mapper.xml.ftl";
        // 如果模板引擎是 velocity
        // String templatePath = "/templates/mapper.xml.vm";

        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
                return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        /*
        cfg.setFileCreate(new IFileCreate() {
            @Override
            public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
                // 判断自定义文件夹是否需要创建
                checkDir("调用默认方法创建的目录,自定义目录用");
                if (fileType == FileType.MAPPER) {
                    // 已经生成 mapper 文件判断存在,不想重新生成返回 false
                    return !new File(filePath).exists();
                }
                // 允许生成模板文件
                return true;
            }
        });
        */
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();

        // 配置自定义输出模板
        //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
        // templateConfig.setEntity("templates/entity2.java");
        // templateConfig.setService();
        // templateConfig.setController();

        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        // strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
        //strategy.setSuperEntityClass("BaseEntity");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        // 公共父类
        // strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
        //strategy.setSuperControllerClass("BaseController");
        // 写于父类中的公共字段
        //strategy.setSuperEntityColumns("id", "created", "updated", "statu");
        strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
        strategy.setControllerMappingHyphenStyle(true);
        // strategy.setTablePrefix(new String[]{"tb_"});
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}
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
148
149
150

本文对应代码 (opens new window)