MyBatisPlus一篇从入门到实战

MyBatisPlus概述

简介

MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

类似的有:tk-mapperMyBatisPlusSpringDataJPA

特性

  • 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
  • 损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作
  • 强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求
  • 支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错
  • 支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题
  • 支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作
  • 支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere )
  • 内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用
  • 内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询
  • 分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库
  • 内置性能分析插件:可输出 Sql 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询
  • 内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作

快速入门

快速入门:https://mp.baomidou.com/guide/quick-start.html#

1、导入对应的依赖

2、研究依赖如何配置

3、代码如何编写

4、提高扩展技术能力!

步骤

1、创建数据库 mybatis_plus

2、创建user表

-- 建库
CREATE DATABASE `mybatis_plus` default character set = 'utf8';
USE `mybatis_plus`;
-- 建表
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)
);
-- 插入数据
-- 真实开发中,version(乐观锁)、deleted(逻辑删除)、gmt_create、gmt_modified
DELETE FROM user;
INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');

3、初始化项目!使用SpringBoot初始化!

4、添加依赖

<!-- springboot依赖 -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.1.RELEASE</version>
    <relativePath/>
</parent>

<dependencies>
    <!-- 数据库驱动 --> 
    <dependency> 
        <groupId>mysql</groupId> 
        <artifactId>mysql-connector-java</artifactId> 
    </dependency>
    <!-- lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <!-- mybatis-plus 是自己开发,并非官方的! -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.3.2</version>
    </dependency>
    <!-- spring test -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

说明:不要同时导入 mybatis 和 mybatis-plus!版本的差异!mybatis-plus 依赖包含了mybatis

5、连接数据库配置!与 mybatis 相同

# mysql 5 驱动不同 com.mysql.jdbc.Driver
# mysql 8 驱动不同com.mysql.cj.jdbc.Driver、需要增加时区的配置(高版本兼容低版本)

serverTimezone=GMT%2B8
spring.datasource.username=root 
spring.datasource.password=password
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 

6、传统方式pojo-dao(连接mybatis,配置mapper.xml文件)-service-controller

6、使用了mybatis-plus之后

  • 编写实体类 User.java(pojo)
@Data 
@AllArgsConstructor 
@NoArgsConstructor 
public class User { 
    private Long id; 
    private String name;
    private Integer age;
    private String email;
}
  • 编写Mapper类 UserMapper.java
package com.liusx.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.liusx.pojo.User;

// 在对应的Mapper上面继承基本的类 BaseMapper
public interface UserMapper extends BaseMapper<User> {
    // 所有的CRUD操作都已经编写完成了
    // 你不需要像以前的配置一大堆文件了!
}
  • 在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹:
@SpringBootApplication
@MapperScan("com.baomidou")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(QuickStartApplication.class, args);
    }
}
  • 测试类中测试
@SpringBootTest
public class MPCrudTest {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void testSelect() {
        System.out.println(("----- selectAll method test ------"));
        List<User> userList = userMapper.selectList(null);
        Assert.assertEquals(5, userList.size());
        userList.forEach(System.out::println);
    }

}
  • 输出
User(id=1, name=Jone, age=18, email=test1@baomidou.com)
User(id=2, name=Jack, age=20, email=test2@baomidou.com)
User(id=3, name=Tom, age=28, email=test3@baomidou.com)
User(id=4, name=Sandy, age=21, email=test4@baomidou.com)
User(id=5, name=Billie, age=24, email=test5@baomidou.com)

配置日志

  • 先配置日志方便后面查看
# 配置日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
----- selectAll method test ------
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@48535004] was not registered for synchronization because synchronization is not active
2020-07-12 11:51:13.725  INFO 62756 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-07-12 11:51:13.887  INFO 62756 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
JDBC Connection [HikariProxyConnection@344751179 wrapping com.mysql.cj.jdbc.ConnectionImpl@2009f9b0] will not be managed by Spring
==>  Preparing: SELECT id,name,age,email FROM user 
==> Parameters: 
<==    Columns: id, name, age, email
<==        Row: 1, Jone, 18, test1@baomidou.com
<==        Row: 2, Jack, 20, test2@baomidou.com
<==        Row: 3, Tom, 28, test3@baomidou.com
<==        Row: 4, Sandy, 21, test4@baomidou.com
<==        Row: 5, Billie, 24, test5@baomidou.com
<==      Total: 5
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@48535004]
....

配置文件:application.yml

mybatis-plus:
  # 如果是放在src/main/java目录下 classpath:/com/yourpackage/*/mapper/*Mapper.xml
  # 如果是放在resource目录 classpath:/mapper/*Mapper.xml
  mapper-locations: classpath:/mapper/*Mapper.xml
  #实体扫描,多个package用逗号或者分号分隔
  typeAliasesPackage: com.yourpackage.*.entity
  global-config:
    #主键类型  0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
    id-type: 3
    #字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
    field-strategy: 2
    #驼峰下划线转换
    db-column-underline: true
    #mp2.3+ 全局表前缀 mp_
    #table-prefix: mp_
    #刷新mapper 调试神器
    #refresh-mapper: true
    #数据库大写下划线转换
    #capital-mode: true
    # Sequence序列接口实现类配置
    key-generator: com.baomidou.mybatisplus.incrementer.OracleKeyGenerator
    #逻辑删除配置(下面3个配置)
    logic-delete-value: 1
    logic-not-delete-value: 0
    sql-injector: com.baomidou.mybatisplus.mapper.LogicSqlInjector
    #自定义填充策略接口实现
    meta-object-handler: com.baomidou.springboot.MyMetaObjectHandler
  configuration:
    #配置返回数据库(column下划线命名&&返回java实体是驼峰命名),自动匹配无需as(没开启这个,SQL需要写as: select user_id as userId) 
    map-underscore-to-camel-case: true
    cache-enabled: false
    #配置JdbcTypeForNull, oracle数据库必须配置
    jdbc-type-for-null: 'null' 

CRUD及扩展

MyBatis-Plus Samples: MyBatis-Plus 的官方示例

https://gitee.com/baomidou/mybatis-plus-samples

插入操作

// 测试插入 
@Test
public void testInsert(){
    User user = new User();
    user.setName("Sam");
    user.setAge(3);
    user.setEmail("8629303@qq.com");
    int result = userMapper.insert(user); // 帮我们自动生成id 
    System.out.println(result); // 受影响的行数 
    System.out.println(user); // 发现,id会自动回填
}
==>  Preparing: INSERT INTO user ( name, age, email ) VALUES ( ?, ?, ? ) 
==> Parameters: Sam(String), 3(Integer), 8629303@qq.com(String)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@48535004]
1
User(id=1282164051669012482, name=Sam, age=3, email=8629303@qq.com)
2020-07-12 12:07:38.116  INFO 35868 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-07-12 12:07:38.122  INFO 35868 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

Process finished with exit code 0

数据库插入的id的默认值为:全局的唯一id

主键生成策略

分布式系统唯一id生成:https://www.cnblogs.com/haoxinyue/p/5208136.html

雪花算法:

snowflflake是Twitter开源的分布式ID生成算法,结果是一个long型的ID。其核心思想是:使用41bit作为毫秒数,10bit作为机器的ID(5个bit是数据中心,5个bit的机器ID),12bit作为毫秒内的流水号(意味着每个节点在每毫秒可以产生 4096 个 ID),最后还有一个符号位,永远是0。可以保证几乎全球唯一!

主键自增

1、实体类字段上:@TableId(type = IdType.AUTO)

2、数据库主键字段一定要设置为:自动递增

3、再次测试插入

==>  Preparing: INSERT INTO user ( name, age, email ) VALUES ( ?, ?, ? ) 
==> Parameters: Sam(String), 3(Integer), 8629303@qq.com(String)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@48535004]
1
User(id=1282164051669012483, name=Sam, age=3, email=8629303@qq.com)
2020-07-12 12:07:38.116  INFO 35868 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-07-12 12:07:38.122  INFO 35868 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

Process finished with exit code 0

对比之前的新增后返回的ID,发现自动增加了+1

public enum IdType {
    AUTO(0),// 数据库id自增
    NONE(1),// 未设置主键
    INPUT(2),// 手动输入
    ASSIGN_ID(3),//分配ID(主键类型为Number(Long和Integer)
    ASSIGN_UUID(4),//分配UUID,主键类型为String(since 3.3.0)
    /** @deprecated */
    @Deprecated
    ID_WORKER(3),// 默认的全局唯一id,废弃
    /** @deprecated */
    @Deprecated
    ID_WORKER_STR(3),//ID_WORKER_STR 字符串表示法,废弃
    /** @deprecated */
    @Deprecated
    UUID(4);// 全局唯一id uuid,废弃
}
描述
AUTO 数据库ID自增
NONE 无状态,该类型为未设置主键类型(注解里等于跟随全局,全局里约等于 INPUT)
INPUT insert前自行set主键值
ASSIGN_ID 分配ID(主键类型为Number(Long和Integer)或String)(since 3.3.0),使用接口IdentifierGenerator的方法nextId(默认实现类为DefaultIdentifierGenerator雪花算法)
ASSIGN_UUID 分配UUID,主键类型为String(since 3.3.0),使用接口IdentifierGenerator的方法nextUUID(默认default方法)
ID_WORKER 分布式全局唯一ID 长整型类型(please use ASSIGN_ID)
UUID 32位UUID字符串(please use ASSIGN_UUID)
ID_WORKER_STR 分布式全局唯一ID 字符串类型(please use ASSIGN_ID)

更新操作

// 测试更新 
@Test
public void testUpdate(){
    User user = new User();
    // 通过条件自动拼接动态sql 
    user.setId(6L);
    user.setName("我是小刘");
    user.setAge(18);
    // 注意:updateById 但是参数是一个 对象! 
    int i = userMapper.updateById(user);
    System.out.println(i);
}
==>  Preparing: UPDATE user SET name=?, age=? WHERE id=? 
==> Parameters: 我是小刘(String), 18(Integer), 6(Long)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@f3fcd59]
1
2020-07-12 12:22:58.795  INFO 57392 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-07-12 12:22:58.801  INFO 57392 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

Process finished with exit code 0

自动填充

创建时间、修改时间!这些个操作一遍都是自动化完成的,我们不希望手动更新!

阿里巴巴开发手册:所有的数据库表:gmt_create、gmt_modifified几乎所有的表都要配置上!而且需

要自动化!

方式一:数据库级别(工作中不允许你修改数据库)

1、在表中新增字段 create_time, update_time(默认CURRENT_TIMESTAMP)

2、再次测试插入方法,我们需要先把实体类同步!

private Date createTime; 
private Date updateTime;

3、再次更新查看结果即可

方式二:代码级别

1、删除数据库的默认值、更新操作!

2、实体类字段属性上需要增加注解

// 字段添加填充内容 
@TableField(fill = FieldFill.INSERT) 
private Date createTime; 

@TableField(fill = FieldFill.INSERT_UPDATE) 
private Date updateTime; 
public enum FieldFill {
    DEFAULT,		// 默认不处理
    INSERT, 		// 插入填充字段
    UPDATE, 		// 更新填充字段
    INSERT_UPDATE	// 插入和更新填充字段
}

3、编写处理器来处理这个注解即可!

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    // 插入时的填充策略
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        // 起始版本 3.3.0(推荐使用)
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date()); 
        
        // 也可以使用(3.3.0 该方法有bug请升级到之后的版本如`3.3.1.8-SNAPSHOT`)
        // this.fillStrategy(metaObject, "createTime", new Date());
        // this.fillStrategy(metaObject, "updateTime", new Date());
    }

    // 更新时的填充策略
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        // 起始版本 3.3.0(推荐使用)
        this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
       
        // 也可以使用(3.3.0 该方法有bug请升级到之后的版本如`3.3.1.8-SNAPSHOT`)
        // this.fillStrategy(metaObject, "updateTime", new Date());
    }
}

4、测试插入

5、测试更新、观察时间即可!

乐观锁

乐观锁:故名思意十分乐观,它总是认为不会出现问题,无论干什么不去上锁!如果出现了问题,再次更新值测试

悲观锁:故名思意十分悲观,它总是认为总是出现问题,无论干什么都会上锁!再去操作

我们这里主要讲解 乐观锁机制!

乐观锁实现方式:

  • 取出记录时,获取当前 version

  • 更新时,带上这个version

  • 执行更新时, set version = newVersion where version = oldVersion

  • 如果version不对,就更新失败

乐观锁:1、先查询,获得版本号 version = 1

-- A 
update user set name = "liusx", version = version + 1 
where id = 2 and version = 1 

-- B 线程抢先完成,这个时候 version = 2,会导致 A 修改失败! 
update user set name = "liusx", version = version + 1 
where id = 2 and version = 1

测试一下MP的乐观锁插件

1、给数据库中增加version字段!

2、我们实体类加对应的字段

@Version //乐观锁Version注解 
private Integer version;

3、注册组件

@MapperScan("com.liusx")
@EnableTransactionManagement
@Configuration // 配置类
public class MyBatisPlusConfig {
    // 注册乐观锁插件
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }
}

4、测试一下!

// 测试乐观锁成功!
@Test
public void testOptimisticLocker(){
    // 1、查询用户信息
    User user = userMapper.selectById(6L);
    // 2、修改用户信息
    user.setName("liusx");
    user.setEmail("110@qq.com");
    // 3、执行更新操作
    userMapper.updateById(user);
}

// 测试乐观锁失败!多线程下
@Test
public void testOptimisticLocker2(){
    // 线程 1
    User user = userMapper.selectById(6L);
    user.setName("liusx111");
    user.setEmail("8629303@qq.com");

    // 模拟另外一个线程执行了插队操作
    User user2 = userMapper.selectById(6L);
    user2.setName("liusx222");
    user2.setEmail("8629303@qq.com");
    userMapper.updateById(user2);
    // 自旋锁来多次尝试提交!
    userMapper.updateById(user); // 如果没有乐观锁就会覆盖插队线程的值!
}
  • testOptimisticLocker()输出
==>  Preparing: UPDATE user SET name=?, age=?, email=?, create_time=?, update_time=?, version=? WHERE id=? AND version=? 
==> Parameters: liusx(String), 21(Integer), 110@qq.com(String), 2020-07-12 12:24:07.0(Timestamp), 2020-07-12 13:40:40.0(Timestamp), 2(Integer), 6(Long), 1(Integer)
<==    Updates: 1
  • testOptimisticLocker2()输出
==>  Preparing: UPDATE user SET name=?, age=?, email=?, create_time=?, update_time=?, version=? WHERE id=? AND version=? 
==> Parameters: liusx222(String), 21(Integer), 8629303@qq.com(String), 2020-07-12 12:24:07.0(Timestamp), 2020-07-12 13:40:40.0(Timestamp), 3(Integer), 6(Long), 2(Integer)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5ac6c4f2]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2e53b094] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@972720850 wrapping com.mysql.cj.jdbc.ConnectionImpl@61d34b4] will not be managed by Spring
==>  Preparing: UPDATE user SET name=?, age=?, email=?, create_time=?, update_time=?, version=? WHERE id=? AND version=? 
==> Parameters: liusx111(String), 21(Integer), 8629303@qq.com(String), 2020-07-12 12:24:07.0(Timestamp), 2020-07-12 13:40:40.0(Timestamp), 3(Integer), 6(Long), 2(Integer)
<==    Updates: 0

查询操作

// 测试查询
@Test 
public void testSelectById(){ 
    User user = userMapper.selectById(1L); 
    System.out.println(user); 
}

// 测试批量查询! 
@Test 
public void testSelectByBatchId(){ 
    List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3)); 
    users.forEach(System.out::println); 
}

// 按条件查询之一使用map操作 
@Test 
public void testSelectByBatchIds(){ 
    HashMap<String, Object> map = new HashMap<>(); 
    // 自定义要查询 
    map.put("name","Jack"); 
    map.put("age",3); 
    List<User> users = userMapper.selectByMap(map); 
    users.forEach(System.out::println); 
}

分页查询

  • 原始的 limit 进行分页
  • pageHelper 第三方插件
  • MybatisPlus 其实也内置了分页插件!

MybatisPlus内置了分页插件的使用

1、配置拦截器组件即可

// 分页插件 
@Bean
public PaginationInterceptor paginationInterceptor() { 
    return new PaginationInterceptor(); 
}

2、直接使用Page对象即可!

// 测试分页查询 
@Test
public void testPage(){
    // 参数一:当前页
    // 参数二:页面大小
    // 使用了分页插件之后,所有的分页操作也变得简单的!
    Page<User> page = new Page<>(2,3);
    userMapper.selectPage(page,null);
    page.getRecords().forEach(System.out::println);
    System.out.println(page.getTotal());
}

删除操作

根据 id 删除记录

// 测试删除 
@Test 
public void testDeleteById(){ 
    userMapper.deleteById(6L); 
}

// 通过id批量删除 
@Test 
public void testDeleteBatchId(){ 
    userMapper.deleteBatchIds(Arrays.asList(6L,1240620674645544962L)); 
}
// 通过map删除
@Test 
public void testDeleteMap(){ 
    HashMap<String, Object> map = new HashMap<>(); 
    map.put("name","我是小刘"); 
    userMapper.deleteByMap(map);
}

逻辑删除

物理删除 :从数据库中直接移除

逻辑删除 :再数据库中没有被移除,而是通过一个变量来让他失效! deleted = 0 => deleted = 1

管理员可以查看被删除的记录!防止数据的丢失,类似于回收站!

测试一下:

1、在数据表中增加一个 deleted 字段

2、配置!

# 配置逻辑删除
# 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置步骤3的注解)
# mybatis-plus.global-config.db-config.logic-delete-field=deleted
mybatis-plus.global-config.db-config.logic-delete-value=1
mybatis-plus.global-config.db-config.logic-not-delete-value=0

3、实体类中增加属性

@TableLogic //逻辑删除 
private Integer deleted; 

4、测试一下删除,发现走的是更新而不是删除

==>  Preparing: UPDATE user SET deleted=1 WHERE id=? AND deleted=0 
==> Parameters: 6(Long)
<==    Updates: 1

条件构造器

十分重要:Wrapper,我们写一些复杂的sql就可以使用它来替代!

文档:https://mp.baomidou.com/guide/wrapper.html#abstractwrapper

1、测试一:

@Test
void contextLoads() {
    // 查询name不为空的用户,并且邮箱不为空的用户,年龄大于等于12
    QueryWrapper<User> wrapper = new QueryWrapper<>();
    wrapper
        .isNotNull("name")
        .isNotNull("email")
        .ge("age",12);// age >= 18
    // 和我们刚才学习的map对比一下
    userMapper.selectList(wrapper).forEach(System.out::println);
}

2、测试二:eq条件查询

@Test 
void test2(){ 
    // 查询名字 Jack
    QueryWrapper<User> wrapper = new QueryWrapper<>(); 
    wrapper.eq("name","Jack"); // name = 'Jack'
    // 查询一个数据,出现多个结果使用List 或者 Map 
    User user = userMapper.selectOne(wrapper); 
    System.out.println(user); 
}

3、测试三:区间查询

@Test 
void test3(){ 
    // 查询年龄在 20 ~ 30 岁之间的用户 
    QueryWrapper<User> wrapper = new QueryWrapper<>(); 
    wrapper.between("age",20,30); // 区间 age between 20 and 30
    Integer count = userMapper.selectCount(wrapper);// 查询结果数 
    System.out.println(count); 
}

4、测试四:模糊查询

// 模糊查询
@Test 
void test4(){ 
    QueryWrapper<User> wrapper = new QueryWrapper<>(); 
    // 左和右 t% 
    wrapper 
        .notLike("name","e") 
        .likeRight("email","t"); 
    List<Map<String, Object>> maps = userMapper.selectMaps(wrapper); 
    maps.forEach(System.out::println); 
}

5、测试五:模糊查询

// 模糊查询 
@Test 
void test5(){ 
    QueryWrapper<User> wrapper = new QueryWrapper<>(); 
    // id 在子查询中查出来 
    //  ==》id in (select id from table where id < 3)
    wrapper.inSql("id","select id from user where id<3"); 
    List<Object> objects = userMapper.selectObjs(wrapper); 
    objects.forEach(System.out::println); 
}

6、测试六:排序

// 排序
@Test 
void test6(){ 
    QueryWrapper<User> wrapper = new QueryWrapper<>(); 
    // 通过id进行排序 
    wrapper.orderByAsc("id"); 
    List<User> users = userMapper.selectList(wrapper); 
    users.forEach(System.out::println); 
}

ActiveRecord模式

1、实体类继承Model

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User extends Model<User> {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String name;
    private Integer age;
    private String email;
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;
    @Version //乐观锁Version注解
    private Integer version;
    private Integer deleted;

    @Override
    protected Serializable pkVal() {
        /**
         * AR 模式这个必须有,否则 xxById 的方法都将失效!
         * 另外 UserMapper 也必须 AR 依赖该层注入,有可无 XML
         */
        return id;
    }
}

2、测试用例

// 查找
@Test
public void testSelectById() {
    User user = new User();
    user.setId(4L);
    User u = user.selectById(); // 在这个位置直接执行selectById
    System.out.println("user1 = " + u);
}

// 新增操作
@Test
public void testInsert() {
    User u = new User();
    u.setName("无道");
    u.setAge(100);
    u.setEmail("8629303@qq.com");
    boolean insert = u.insert();
    System.out.println("insert = " + insert);
}

// 更新操作
@Test
public void testUpdate() {
    User u = new User();
    u.setId(5L);
    u.setAge(22);
    boolean b = u.updateById();
    System.out.println("b = " + b);
}

// 删除操作
@Test
public void testDelete() {
    User u = new User();
    u.setId(6L);
    boolean b = u.deleteById();
    System.out.println("b = " + b);
}

// 根据条件查询
@Test
public void testSelectCondition() {
    QueryWrapper<User> wrapper = new QueryWrapper<>();
    wrapper.ge("age", 30);
    User u = new User();
    List<User> users = u.selectList(wrapper);
    System.out.println("users = " + users);
}

不可以删除UserMapper接口,因为底层还是使用到了接口。

代码自动生成器

AutoGeneratorMyBatis-Plus的代码生成器,通过AutoGenerator可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率

https://mp.baomidou.com/guide/generator.html

package com.liusx;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.apache.commons.lang3.StringUtils;

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

// 代码自动生成器
public class CodeGenerator {

    private static final String URL = "jdbc:mysql://192.168.3.55:3306/mybatis_plus? useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8";
    private static final String DRIVERNAME = "com.mysql.cj.jdbc.Driver";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "password";
    private static final String MODULE = "/mybatis_plus"; // module名称

    public static void main(String[] args) { 
        // 需要构建一个 代码自动生成器 对象 
        AutoGenerator mpg = new AutoGenerator();

        // 配置策略 
        // 1、全局配置 
        GlobalConfig gc = new GlobalConfig(); 
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + MODULE + "/src/main/java");
        gc.setAuthor("liusx"); // 代码开发人员
        gc.setOpen(true); // 是否打开输出目录
        gc.setFileOverride(true); // 是否覆盖
        gc.setServiceName("%sService"); // 去Service的I前缀 
        gc.setIdType(IdType.AUTO); // 指定生成的主键的ID类型
        gc.setDateType(DateType.ONLY_DATE); 
        gc.setSwagger2(false);  // 实体属性 Swagger2 注解
        mpg.setGlobalConfig(gc); 

        //2、设置数据源 
        DataSourceConfig dsc = new DataSourceConfig(); 
        dsc.setUrl(URL);
        dsc.setDriverName(DRIVERNAME);
        dsc.setUsername(USERNAME);
        dsc.setPassword(PASSWORD);
        dsc.setDbType(DbType.MYSQL); 
        mpg.setDataSource(dsc); 

        // 3、包的配置
        PackageConfig pc = new PackageConfig();
        //pc.setModuleName("generator"); // 父包模块名
        pc.setModuleName(scanner("模块名")); // 父包模块名
        pc.setParent("com.liusx"); // 父包名。如果为空,将下面子包名必须写全部,否则就只需写子包名
        pc.setController("controller");
        pc.setService("service");
        pc.setServiceImpl("service"); // service.impl 默认包名
        pc.setMapper("mapper");
        pc.setXml("mapper");
        pc.setEntity("entity");
        mpg.setPackageInfo(pc);

        //4、策略配置 
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel); // 数据库表映射到实体的命名策略,当前驼峰
        strategy.setColumnNaming(NamingStrategy.underline_to_camel); // 数据库表字段映射到实体的命名策略
        strategy.setEntityLombokModel(true); // 自动lombok
        strategy.setRestControllerStyle(true); // 生成 @RestController控制器
        strategy.setControllerMappingHyphenStyle(true); // ip:8080/helloId ==>ip:8080/hello_id
        strategy.setLogicDeleteFieldName("deleted"); // 逻辑删除属性名称
        // 自动填充配置
        TableFill gmtCreate = new TableFill("gmt_create", FieldFill.INSERT); 
        TableFill gmtModified = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE); 
        ArrayList<TableFill> tableFills = new ArrayList<>(); 
        tableFills.add(gmtCreate); 
        tableFills.add(gmtModified); 
        strategy.setTableFillList(tableFills); 
        // 乐观锁 
        strategy.setVersionFieldName("version");
        // 设置要映射的表名
        //strategy.setInclude("user");
        strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
        mpg.setStrategy(strategy);
        mpg.execute(); //执行 
    }

    /**
     * <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.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "!");
    }
}

SQL性能分析插件

该功能依赖 p6spy 组件,完美的输出打印 SQL 及执行时长 3.1.0 以上版本

1、p6spy 依赖引入

<!-- p6spy性能分析插件-->
<dependency>
    <groupId>p6spy</groupId>
    <artifactId>p6spy</artifactId>
    <version>3.8.5</version>
</dependency>

2、修改yml/properties配置文件:

#spring.datasource.url=jdbc:mysql://192.168.3.55:3306/mybatis_plus?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# sql分析插件需要切换url及驱动具体看mybatis-plus官网
spring.datasource.url=jdbc:p6spy:mysql://192.168.3.55:3306/mybatis_plus?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.driver-class-name=com.p6spy.engine.spy.P6SpyDriver

3、导入spy.properties 配置到resources目录下:

modulelist=com.baomidou.mybatisplus.extension.p6spy.MybatisPlusLogFactory,com.p6spy.engine.outage.P6OutageFactory
# 自定义日志打印
logMessageFormat=com.baomidou.mybatisplus.extension.p6spy.P6SpyLogger
# 日志输出到控制台
appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger
# 使用日志系统记录 sql
# appender=com.p6spy.engine.spy.appender.Slf4JLogger
# 设置 p6spy driver 代理
deregisterdrivers=true
# 取消JDBC URL前缀
useprefix=true
# 配置记录 Log 例外,可去掉的结果集有error,info,batch,debug,statement,commit,rollback,result,resultset.
excludecategories=info,debug,result,commit,resultset
# 日期格式
dateformat=yyyy-MM-dd HH:mm:ss
# 实际驱动可多个
#driverlist=org.h2.Driver
# 是否开启慢SQL记录
outagedetection=true
# 慢SQL记录标准 2 秒
outagedetectioninterval=1

# 输出到文件中,需要把上面配置输出到控制台的配置注释掉
logfile=log.log

4、然后随便找个测试类测试

下面是性能分析插件打印的:
==> Parameters: 
 Consume Time:10 ms 2020-07-12 18:39:41
 Execute SQL:SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND (id IN (select id from user where id<3))

发现该方法的SQL执行了10ms毫秒

注意!

  • driver-class-name 为 p6spy 提供的驱动类
  • url 前缀为 jdbc:p6spy 跟着冒号为对应数据库连接地址
  • 打印出sql为null,在excludecategories增加commit
  • 批量操作不打印sql,去除excludecategories中的batch
  • 批量操作打印重复的问题请使用MybatisPlusLogFactory (3.2.1新增)
  • 该插件有性能损耗,不建议生产环境使用
原文地址:https://www.cnblogs.com/liusuixing/p/14092361.html