MyBatis-Plus

简介

MyBatis-Plus (简称 MP)是一个MyBatis的增强工具

在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

特性

  • 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
  • 损耗小:启动即会自动注入基本 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 操作智能分析阻断,也可自定义拦截规则,预防误操作

支持数据库

  • mysql,oracle,db2,h2,hsql,sqlite,postgresql,sqlserver,Phoenix,Gauss ,clickhouse,Sybase,OceanBase,Firebird,cubrid,goldilocks,csiidb
  • 达梦数据库,虚谷数据库,人大金仓数据库,南大通用(华库)数据库,南大通用数据库,神通数据库,瀚高数据库

框架结构

快速开始

入门

  1. 创建数据表
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)
);

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');
  1. 创建Spring Boot项目
  2. 导入依赖
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

<!--jdbc-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.3.2</version>
</dependency>
  1. 数据库配置
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password:
    url: jdbc:mysql://localhost:3306/mybatis-plus?useUnicode=true&characterEncoding=utf8
  1. 创建和表对应的实体类
package com.zeng.pojo;
import lombok.Data;

@Data
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}
  1. mapper接口
package com.zeng.mapper;

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

public interface UserMapper extends BaseMapper<User> {

}
  1. 扫描Mapper
package com.zeng;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.zeng.mapper")
@SpringBootApplication
public class MybatisPlusApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybatisPlusApplication.class, args);
    }
}
  1. 测试类
package com.zeng;

import com.zeng.mapper.UserMapper;
import com.zeng.pojo.User;
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 MybatisPlusApplicationTests {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void testSelect() {
        System.out.println(("----- selectAll method test ------"));
        List<User> userList = userMapper.selectList(null);
        userList.forEach(System.out::println);
    }
}
  1. 输出结果
...
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)
...

小结

  • 遇到的问题
  1. @Autowired 标记的变量变红:

    1. 不影响 正常运行
    2. 可以换成@Resource
  2. 导入依赖问题:

    1. 不要导入mybatis-plus,要导入mybatis-plus-boot-starter
  • 知识点
  1. Mapper接口继承的BaseMapper类已经封装了常用的CRUD,直接调用即可
  2. 一定不要忘了扫描Mapper,@MapperScan

Log配置

# 方式一
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl 
    
# 方式二 application.yml 中增加配置,指定 mapper 文件所在的包
logging:
  level:
    com.baomidou.example.mapper: debug

注解

简单认识常用注解

【@TableName 】
    @TableName               用于定义表名
注:
    常用属性:
        value                用于定义表名

【@TableId】
    @TableId                 用于定义表的主键
注:
    常用属性:
        value           用于定义主键字段名
        type            用于定义主键类型(主键策略 IdType)

   主键策略:
      IdType.AUTO          主键自增,系统分配,不需要手动输入
      IdType.NONE          未设置主键
      IdType.INPUT         需要自己输入 主键值。
      IdType.ASSIGN_ID     系统分配 ID,用于数值型数据(Long,对应 mysql 中 BIGINT 类型)。
      IdType.ASSIGN_UUID   系统分配 UUID,用于字符串型数据(String,对应 mysql 中 varchar(32) 类型)。

【@TableField】  
    @TableField            用于定义表的非主键字段。
注:
    常用属性:
        value                用于定义非主键字段名
        exist                用于指明是否为数据表的字段, true 表示是,false 为不是。
        fill                 用于指定字段填充策略(FieldFill)。
        
    字段填充策略:(一般用于填充 创建时间、修改时间等字段)
        FieldFill.DEFAULT         默认不填充
        FieldFill.INSERT          插入时填充
        FieldFill.UPDATE          更新时填充
        FieldFill.INSERT_UPDATE   插入、更新时填充。

【@TableLogic】
    @TableLogic           用于定义表的字段进行逻辑删除(非物理删除)
注:
    常用属性:
        value            用于定义未删除时字段的值
        delval           用于定义删除时字段的值
        
【@Version】
    @Version             用于字段实现乐观锁

ASSIGN_ID(雪花算法)

如果不设置类型值,默认则使用IdType.ASSIGN_ID策略(自3.3.0起)。该策略会使用雪花算法自动生成主键ID,主键类型为长或字符串(分别对应的MySQL的表字段为BIGINT和VARCHAR)

雪花算法(雪花)是分布式ID生成算法其核心思想就是:使用一个64位的长型的数字作为全局唯一ID。在分布式系统中的应用十分广泛,且ID引入了时间戳,基本上保持自增的。

@Data
public class UserInfo {
     //指定主键生成策略使用雪花算法(默认策略)
     @TableId(type = IdType.ASSIGN_ID)
     private Long id;
     private String userName;
     private String passWord;
}

ASSIGN_UUID(排除中划线的UUID)

@Data
public class UserInfo {
     //指定主键生成策略为不含中划线的UUID
     @TableId(type = IdType.ASSIGN_UUID)
     private String id;
     private String userName;
     private String passWord;
}

代码生成器

简介

AutoGenerator 简介
  AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。
  与 mybatis 中的 mybatis-generator-core 类似。

依赖

代码生成器依赖

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.4.1</version>
</dependency>

模板引擎 依赖

<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity-engine-core</artifactId>
    <version>2.3</version>
</dependency>

配置

// Step1:代码生成器
AutoGenerator mpg = new AutoGenerator();

// Step2:全局配置
GlobalConfig gc = new GlobalConfig();
// 填写代码生成的目录(需要修改)
String projectPath = "E:\\Course\\Java\\IdeaProjects\\Mybatis-Plus";
// 拼接出代码最终输出的目录
gc.setOutputDir(projectPath + "/src/main/java");
// 配置开发者信息(可选)(需要修改)
gc.setAuthor("Jiuan");
// 配置是否打开目录,false 为不打开(可选)
gc.setOpen(false);
// 实体属性 Swagger2 注解,添加 Swagger 依赖,开启 Swagger2 模式(可选)
//gc.setSwagger2(true);
// 重新生成文件时是否覆盖,false 表示不覆盖(可选)
gc.setFileOverride(false);
// 配置主键生成策略,此处为 ASSIGN_ID(可选)
gc.setIdType(IdType.ASSIGN_ID);
// 配置日期类型,此处为 ONLY_DATE(可选)
gc.setDateType(DateType.ONLY_DATE);
// 默认生成的 service 会有 I 前缀
gc.setServiceName("%sService");
mpg.setGlobalConfig(gc);

// Step3:数据源配置(需要修改)
DataSourceConfig dsc = new DataSourceConfig();
// 配置数据库 url 地址
dsc.setUrl("jdbc:mysql://localhost:3306/mybatis-plus?useUnicode=true&characterEncoding=utf8");
// dsc.setSchemaName("testMyBatisPlus"); // 可以直接在 url 中指定数据库名
// 配置数据库驱动
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
// 配置数据库连接用户名
dsc.setUsername("root");
// 配置数据库连接密码
dsc.setPassword("");
mpg.setDataSource(dsc);

// Step:4:包配置
PackageConfig pc = new PackageConfig();
// 配置父包名(需要修改)
pc.setParent("com.zeng");
// 配置模块名(需要修改)
pc.setModuleName("test_mybatis_plus");
// 配置 entity 包名
pc.setEntity("entity");
// 配置 mapper 包名
pc.setMapper("mapper");
// 配置 service 包名
pc.setService("service");
// 配置 controller 包名
pc.setController("controller");
mpg.setPackageInfo(pc);

// Step5:策略配置(数据库表配置)
StrategyConfig strategy = new StrategyConfig();
// 指定表名(可以同时操作多个表,使用 , 隔开)(需要修改)
strategy.setInclude("user");
// 配置数据表与实体类名之间映射的策略
strategy.setNaming(NamingStrategy.underline_to_camel);
// 配置数据表的字段与实体类的属性名之间映射的策略
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// 配置 lombok 模式
strategy.setEntityLombokModel(true);
// 配置 rest 风格的控制器(@RestController)
strategy.setRestControllerStyle(true);
// 配置驼峰转连字符
strategy.setControllerMappingHyphenStyle(true);
// 配置表前缀,生成实体时去除表前缀
// 表名为 test_mybatis_plus_user,模块名为 test_mybatis_plus,去除前缀后剩下为 user。
strategy.setTablePrefix(pc.getModuleName() + "_");
mpg.setStrategy(strategy);

// Step6:执行代码生成操作
mpg.execute();

小结

  1. 使用代码生成器之前先要配置数据库才能使用
spring:
    datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        username: root
        password:
        url: jdbc:mysql://localhost:3306/mybatis-plus?useUnicode=true&characterEncoding=utf8
  1. 一定要导入依赖,生成器只是生成代码,没有依赖,如lombok

CRUD

Mapper接口

说明:

  • 通用 CRUD 封装BaseMapper接口,为 Mybatis-Plus 启动时自动解析实体表关系映射转换为 Mybatis 内部对象注入容器
  • 泛型 T 为任意实体对象
  • 参数 Serializable 为任意类型主键 Mybatis-Plus 不推荐使用复合主键约定每一张表都有自己的唯一 id 主键
  • 对象 Wrapper 为条件构造器

Insert

// 插入一条记录
int insert(T entity);

参数说明

类型 参数名 描述
T entity 实体对象
@Test
void testInsert(){
    //根据对象增加
    User user = new User();
    user.setName("zeng");
    user.setEmail("zeng@qq.com");
    user.setAge(18);
    userMapper.insert(user);
}

Delete

// 根据 entity 条件,删除记录
int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);
// 删除(根据ID 批量删除)
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// 根据 ID 删除
int deleteById(Serializable id);
// 根据 columnMap 条件,删除记录,多条件
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);

参数说明

类型 参数名 描述
Wrapper wrapper 实体对象封装操作类(可以为 null)
Collection<? extends Serializable> idList 主键ID列表(不能为 null 以及 empty)
Serializable id 主键ID
Map<String, Object> columnMap 表字段 map 对象
void testDelete(){
    //构造器
    //userMapper.delete(null);

    //根据主键多个删除
    List<Integer> idList = new ArrayList<>();
    idList.add(1);
    idList.add(2);
    userMapper.deleteBatchIds(idList);

    //主键删除
    userMapper.deleteById("3");

    //多条件删除
    Map<String, Object> columnMap = new HashMap<>();
    columnMap.put("id",4);
    columnMap.put("age",21);
    userMapper.deleteByMap(columnMap);
}

Update

// 根据 whereWrapper 条件,更新记录
int update(@Param(Constants.ENTITY) T updateEntity, @Param(Constants.WRAPPER) Wrapper<T> whereWrapper);
// 根据 ID 修改
int updateById(@Param(Constants.ENTITY) T entity);

参数说明

类型 参数名 描述
T entity 实体对象 (set 条件值,可为 null)
Wrapper updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
@Test
void testUpdate(){
    //构造器
    //userMapper.update();

    //根据对象
    User user = new User();
    user.setId((long)1);
    user.setName("张三");
    userMapper.updateById(user);
}

Select

// 根据 ID 查询
T selectById(Serializable id);
// 根据 entity 条件,查询一条记录
T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

// 查询(根据ID 批量查询)
List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// 根据 entity 条件,查询全部记录
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 查询(根据 columnMap 条件)
List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
// 根据 Wrapper 条件,查询全部记录
List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录。注意: 只返回第一个字段的值
List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

// 根据 entity 条件,查询全部记录(并翻页)
IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录(并翻页)
IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询总记录数
Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

参数说明

类型 参数名 描述
Serializable id 主键ID
Wrapper queryWrapper 实体对象封装操作类(可以为 null)
Collection<? extends Serializable> idList 主键ID列表(不能为 null 以及 empty)
Map<String, Object> columnMap 表字段 map 对象
IPage page 分页查询条件(可以为 RowBounds.DEFAULT)
@Test
void testSelect(){
    //查询全部
    List<User> users = userMapper.selectList(null);

    for (User user:users) {
        System.out.println(user);
    }
}

Service接口

说明:

  • 通用 Service CRUD 封装IService接口,进一步封装 CRUD 采用 get 查询单行 remove 删除``list 查询集合 page 分页 前缀命名方式区分 Mapper 层避免混淆,
  • 泛型 T 为任意实体对象
  • 建议如果存在自定义通用 Service 方法的可能,请创建自己的 IBaseService 继承 Mybatis-Plus 提供的基类
  • 对象 Wrapper 为 条件构造器

Save

// 插入一条记录(选择字段,策略插入)
boolean save(T entity);
// 插入(批量)
boolean saveBatch(Collection<T> entityList);
// 插入(批量)
boolean saveBatch(Collection<T> entityList, int batchSize);

参数说明

类型 参数名 描述
T entity 实体对象
Collection entityList 实体对象集合
int batchSize 插入批次数量

SaveOrUpdate

// TableId 注解存在更新记录,否插入一条记录
boolean saveOrUpdate(T entity);
// 根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法
boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper);
// 批量修改插入
boolean saveOrUpdateBatch(Collection<T> entityList);
// 批量修改插入
boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);

参数说明

类型 参数名 描述
T entity 实体对象
Wrapper updateWrapper 实体对象封装操作类 UpdateWrapper
Collection entityList 实体对象集合
int batchSize 插入批次数量

Remove

// 根据 entity 条件,删除记录
boolean remove(Wrapper<T> queryWrapper);
// 根据 ID 删除
boolean removeById(Serializable id);
// 根据 columnMap 条件,删除记录
boolean removeByMap(Map<String, Object> columnMap);
// 删除(根据ID 批量删除)
boolean removeByIds(Collection<? extends Serializable> idList);

参数说明

类型 参数名 描述
Wrapper queryWrapper 实体包装类 QueryWrapper
Serializable id 主键ID
Map<String, Object> columnMap 表字段 map 对象
Collection<? extends Serializable> idList 主键ID列表

Update

// 根据 UpdateWrapper 条件,更新记录 需要设置sqlset
boolean update(Wrapper<T> updateWrapper);
// 根据 whereWrapper 条件,更新记录
boolean update(T updateEntity, Wrapper<T> whereWrapper);
// 根据 ID 选择修改
boolean updateById(T entity);
// 根据ID 批量更新
boolean updateBatchById(Collection<T> entityList);
// 根据ID 批量更新
boolean updateBatchById(Collection<T> entityList, int batchSize);

参数说明

类型 参数名 描述
Wrapper updateWrapper 实体对象封装操作类 UpdateWrapper
T entity 实体对象
Collection entityList 实体对象集合
int batchSize 更新批次数量

Get

// 根据 ID 查询
T getById(Serializable id);
// 根据 Wrapper,查询一条记录。结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")
T getOne(Wrapper<T> queryWrapper);
// 根据 Wrapper,查询一条记录
T getOne(Wrapper<T> queryWrapper, boolean throwEx);
// 根据 Wrapper,查询一条记录
Map<String, Object> getMap(Wrapper<T> queryWrapper);
// 根据 Wrapper,查询一条记录
<V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);

参数说明

类型 参数名 描述
Serializable id 主键ID
Wrapper queryWrapper 实体对象封装操作类 QueryWrapper
boolean throwEx 有多个 result 是否抛出异常
T entity 实体对象
Function<? super Object, V> mapper 转换函数

ListList

// 查询所有
List<T> list();
// 查询列表
List<T> list(Wrapper<T> queryWrapper);
// 查询(根据ID 批量查询)
Collection<T> listByIds(Collection<? extends Serializable> idList);
// 查询(根据 columnMap 条件)
Collection<T> listByMap(Map<String, Object> columnMap);
// 查询所有列表
List<Map<String, Object>> listMaps();
// 查询列表
List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper);
// 查询全部记录
List<Object> listObjs();
// 查询全部记录
<V> List<V> listObjs(Function<? super Object, V> mapper);
// 根据 Wrapper 条件,查询全部记录
List<Object> listObjs(Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录
<V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);

参数说明

类型 参数名 描述
Wrapper queryWrapper 实体对象封装操作类 QueryWrapper
Collection<? extends Serializable> idList 主键ID列表
Map<?String, Object> columnMap 表字段 map 对象
Function<? super Object, V> mapper 转换函数

Page

// 无条件分页查询
IPage<T> page(IPage<T> page);
// 条件分页查询
IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper);
// 无条件分页查询
IPage<Map<String, Object>> pageMaps(IPage<T> page);
// 条件分页查询
IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper);

参数说明

类型 参数名 描述
IPage page 翻页对象
Wrapper queryWrapper 实体对象封装操作类 QueryWrapper

Count

// 查询总记录数
int count();
// 根据 Wrapper 条件,查询总记录数
int count(Wrapper<T> queryWrapper);

参数说明

类型 参数名 描述
Wrapper queryWrapper 实体对象封装操作类 QueryWrapper

Chain

query

// 链式查询 普通
QueryChainWrapper<T> query();
// 链式查询 lambda 式。注意:不支持 Kotlin
LambdaQueryChainWrapper<T> lambdaQuery(); 

// 示例:
query().eq("column", value).one();
lambdaQuery().eq(Entity::getId, value).list();

update

// 链式更改 普通
UpdateChainWrapper<T> update();
// 链式更改 lambda 式。注意:不支持 Kotlin 
LambdaUpdateChainWrapper<T> lambdaUpdate();

// 示例:
update().eq("column", value).remove();
lambdaUpdate().eq(Entity::getId, value).update(entity);

条件构造器

简介

Wrapper 条件构造抽象类

  • AbstractWrapper 查询条件封装,用于生成 sql 中的 where 语句。
    • QueryWrapper Entity 对象封装操作类,用于查询。
    • UpdateWrapper Update 条件封装操作类,用于更新。
  • AbstractLambdaWrapper 使用 Lambda 表达式封装 wrapper
    • LambdaQueryWrapper 使用 Lambda 语法封装条件,用于查询。
    • LambdaUpdateWrapper 使用 Lambda 语法封装条件,用于更新。

条件

比较大小

【比较大小: ( =, <>, >, >=, <, <= )】
    eq(R column, Object val); // 等价于 =
	例: eq("name", "老王") ---> name = '老王'
        
    ne(R column, Object val); // 等价于 <>
	例: ne("name", "老王") ---> name <> '老王'
        
    gt(R column, Object val); // 等价于 >
	例: gt("name", "老王") ---> name > '老王'
        
    ge(R column, Object val); // 等价于 >=
	例: ge("name", "老王") ---> name >= '老王'
        
    lt(R column, Object val); // 等价于 <
	例: lt("name", "老王") ---> name < '老王'
        
    le(R column, Object val); // 等价于 <=
	例: le("name", "老王") ---> name <= '老王'

范围

【范围:(between、not between、in、not in)】
   between(R column, Object val1, Object val2); // 等价于 between a and b
   例: between("age", 18, 30) ---> age between 18 and 30
  
   notBetween(R column, Object val1, Object val2); // 等价于 not between a and b
   例: notBetween("age", 18, 30) ---> age not between 18 and 30
   
   in(R column, Object... values); // 等价于 字段 IN (v0, v1, ...)
   例: in("age",{1,2,3}) ---> age in (1,2,3)
   
   notIn(R column, Object... values); // 等价于 字段 NOT IN (v0, v1, ...)
   例: notIn("age",{1,2,3}) ---> age not in (1,2,3)
   
   inSql(R column, Object... values); // 等价于 字段 IN (sql 语句)
   例: inSql("id", "select id from table where id < 3") ---> id in (select id from table where id < 3)
   
   notInSql(R column, Object... values); // 等价于 字段 NOT IN (sql 语句)

模糊匹配

【模糊匹配:(like)】
    like(R column, Object val); // 等价于 LIKE '%值%'
	例: like("name", "王") ---> name like '%王%'
        
    notLike(R column, Object val); // 等价于 NOT LIKE '%值%'
	例: notLike("name", "王") ---> name not like '%王%'
    
    likeLeft(R column, Object val); // 等价于 LIKE '%值'
	例: likeLeft("name", "王") ---> name like '%王'
        
    likeRight(R column, Object val); // 等价于 LIKE '值%'
	例: likeRight("name", "王") ---> name like '王%'

空值比较

【空值比较:(isNull、isNotNull)】
    isNull(R column); // 等价于 IS NULL,
	例: isNull("name") ---> name is null
        
    isNotNull(R column); // 等价于 IS NOT NULL
	例: isNotNull("name") ---> name is not null

分组排序

【分组、排序:(group、having、order)】
    groupBy(R... columns); // 等价于 GROUP BY 字段, ..., 
	例: groupBy("id", "name") ---> group by id,name
        
    orderByAsc(R... columns); // 等价于 ORDER BY 字段, ... ASC, 
	例: orderByAsc("id", "name") ---> order by id ASC,name ASC
        
    orderByDesc(R... columns); // 等价于 ORDER BY 字段, ... DESC
	例: orderByDesc("id", "name") ---> order by id DESC,name DESC
        
    having(String sqlHaving, Object... params); // 等价于 HAVING ( sql语句 )
	例: having("sum(age) > {0}", 11) ---> having sum(age) > 11

拼接嵌套sql

【拼接、嵌套 sql:(or、and、nested、apply)】
    or(); // 等价于 a or b, 
	例:eq("id",1).or().eq("name","老王") ---> id = 1 or name = '老王'
        
	or(Consumer<Param> consumer); // 等价于 or(a or/and b),or 嵌套
	例: or(i -> i.eq("name", "李白").ne("status", "活着")) ---> or (name = '李白' and status <> '活着')
        
	and(Consumer<Param> consumer); // 等价于 and(a or/and b),and 嵌套
	例: and(i -> i.eq("name", "李白").ne("status", "活着")) ---> and (name = '李白' and status <> '活着')
        
	nested(Consumer<Param> consumer); // 等价于 (a or/and b),普通嵌套
	例: nested(i -> i.eq("name", "李白").ne("status", "活着")) ---> (name = '李白' and status <> '活着')
        
	apply(String applySql, Object... params); // 拼接sql(若不使用 params 参数,可能存在 sql 注入)
	例: apply("date_format(dateColumn,'%Y-%m-%d') = {0}", "2008-08-08") ---> date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")
        
	last(String lastSql); // 无视优化规则直接拼接到 sql 的最后,可能存若在 sql 注入。

	exists(String existsSql); // 拼接 exists 语句
	例: exists("select id from table where age = 1") ---> exists (select id from table where age = 1)

QueryWrapper

【QueryWrapper 条件:】
    select(String... sqlSelect); // 用于定义需要返回的字段。
	例: select("id", "name", "age") ---> select id, name, age
        
    select(Predicate<TableFieldInfo> predicate); // Lambda 表达式,过滤需要的字段。

    lambda(); // 返回一个 LambdaQueryWrapper
@Test
void testSelect2(){
    // Step1:创建一个 QueryWrapper 对象
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();

    // Step2:构造查询条件
    queryWrapper
        .select("id", "name", "age")
        .eq("age", 20)
        .like("name", "j");

    // Step3:执行查询
    userMapper.selectList(queryWrapper).forEach(System.out::println);
}

UpdateWrapper

【UpdateWrapper 条件:】
    set(String column, Object val); // 用于设置 set 字段值。
	例: set("name", null) ---> set name = null
        
    etSql(String sql); // 用于设置 set 字段值。
	例: setSql("name = '老李头'") ---> set name = '老李头'
        
    lambda(); // 返回一个 LambdaUpdateWrapper 
@Test
void testUpdate2(){
    // Step1:创建一个 UpdateWrapper 对象
    UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();

    // Step2:构造查询条件
    updateWrapper
        .set("name","张三")
        .eq("age",20);

    // Step3:执行查询
    userMapper.update(null,updateWrapper);
}

自动填充

简介

添加修改数据时,每次都会使用相同的方式进行填充。比如 数据的创建时间修改时间等。

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

手动操作

@Autowired
private UserService userService;

@Test
public void testUpdate() {
    User user = new User();
    user.setName("tom").setAge(20).setEmail("tom@163.com");
    // 手动添加数据
    user.setCreateTime(new Date()).setUpdateTime(new Date());
    if (userService.save(user)) {
        userService.list().forEach(System.out::println);
    } else {
        System.out.println("添加数据失败");
    }
}

数据库操作

数据库级别(工作中不允许你修改数据库)直接设置默认值,更新的操作

自动填充

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

  2. 实体类字段属性上需要@TableField 注解,标注需要进行填充的字段

/**
 * 创建时间
 */
@TableField(fill = FieldFill.INSERT)
private Date createTime;
/**
 * 最后修改时间
 */
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
  1. 自定义一个类,实现 MetaObjectHandler 接口,并重写方法

    添加 @Component 注解,交给 Spring 去管理

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.util.Date;

@Component 
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        //自动填充
		this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
    }
  
    @Override
    public void updateFill(MetaObject metaObject) {
		//自动填充
        this.setFieldValByName("updateTime",new Date(),metaObject);
    }
}
  1. 测试
@Test
public void testAutoFill() {
    User user = new User();
    user.setName("tom").setAge(20).setEmail("tom@163.com");
    if (userService.save(user)) {
        userService.list().forEach(System.out::println);
    } else {
        System.out.println("添加数据失败");
    }
}

逻辑删除

简介

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

逻辑删除:

指的是修改数据的某个字段,使其表示为已删除状态,而非删除数据,保留该数据在数据库中,但是查询时不显示该数据(查询时过滤掉该数据

使用

步骤1: 配置com.baomidou.mybatisplus.core.config.GlobalConfig$DbConfig

  • 例: application.yml
mybatis-plus:
  global-config:
    db-config:
      logic-delete-field: flag  # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置步骤2)
      logic-delete-value: 1 # 逻辑已删除值(默认为 1)
      logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)

步骤2: 实体类字段上加上@TableLogic注解

@TableLogic
private Integer deleted;

问题

如何 insert ?

  1. 字段在数据库定义默认值(推荐)
  2. insert 前自己 set 值
  3. 使用自动填充功能
@Override
public void insertFill(MetaObject metaObject) {
    //自动填充
    this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
    this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
    //逻辑删除
    this.strictInsertFill(metaObject, "deleted", Integer.class, 0);
}

分页插件

简介

与 mybatis 的插件 pagehelper 用法类似,通过简单的配置即可使用

使用

配置分页插件:

  1. 编写一个 配置类,内部使用 @Bean 注解将 PaginationInterceptor 交给 Spring 容器管理。

  2. mapper 扫描也可在此写上(把前面写在主程序的转到这里)

@Configuration
@MapperScan("com.zeng.test_mybatis_plus.mapper")
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
    }
}
  1. 分页测试
@Test
public void testPage() {
    // Step1:创建一个 Page 对象
    Page<User> page = new Page<>();
    // Page<User> page = new Page<>(2, 5);
    // Step2:调用 mybatis-plus 提供的分页查询方法
    // Mapper接口:
    // userMapper.selectPage(page, null);
    // Service接口:
    userService.page(page, null);
    // Step3:获取分页数据
    System.out.println(page.getCurrent()); // 获取当前页
    System.out.println(page.getTotal()); // 获取总记录数
    System.out.println(page.getSize()); // 获取每页的条数
    System.out.println(page.getRecords()); // 获取每页数据的集合
    System.out.println(page.getPages()); // 获取总页数
    System.out.println(page.hasNext()); // 是否存在下一页
    System.out.println(page.hasPrevious()); // 是否存在上一页
}

乐观锁

简介

  1. 读问题、写问题

    读问题:指的是从数据库中读取数据时遇到的问题

    如:脏读、幻读、不可重复读。

    写问题:指的是数据写入数据库时遇到的问题

    如:丢失更新(多个线程同时对某条数据更新,无论执行顺序如何,都会丢失其他线程更新的数据)

  1. 如何解决问题

    乐观锁:总是假设最好的情况,每次读取数据时认为数据不会被修改(即不加锁),当进行更新操作时,会判断这条数据是否被修改,未被修改,则进行更新操作。若被修改,则数据更新失败,可以对数据进行重试(重新尝试修改数据)

    悲观锁:总是假设最坏的情况,每次读取数据时认为数据会被修改(即加锁),当进行更新操作时,直接更新数据,结束操作后释放锁(此处才可以被其他线程读取)

  2. 乐观锁、悲观锁使用场景

    乐观锁一般用于读比较多的场合,尽量减少加锁的开销

    悲观锁一般用于写比较多的场合,尽量减少类似乐观锁重试更新引起的性能开销

  3. 乐观锁两种实现方式

    方式一:通过版本号机制实现

    在数据表中增加一个 version 字段

    取数据时,获取该字段,更新时以该字段为条件进行处理(即set version = newVersion where version = oldVersion)

    若 version 相同,则更新成功(给新 version 赋一个值,一般加 1)

    若 version 不同,则更新失败,可以重新尝试更新操作。

    方式二:通过 CAS 算法实现

    CAS 为 Compare And Swap 的缩写,即比较交换,是一种无锁算法(即在不加锁的情况实现多线程之间的变量同步)

  4. mybatis-plus 实现乐观锁(通过 version 机制)

    1. 取出记录时,获取当前version
    2. 更新时,带上这个version
    3. 执行更新时, set version = newVersion where version = oldVersion
    4. 如果version不对,就更新失败

使用

  1. 编写一个配置类(可以与上例的分页插件共用一个配置类),将 OptimisticLockerInterceptor 通过 @Bean 交给 Spring 管理。
@Configuration
@MapperScan("com.zeng.test_mybatis_plus.mapper")
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
        //乐观锁
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }
}
  1. 在实体类的字段上加上@Version注解
@Version
private Integer version;
  1. version默认值填充(也可设置数据库默认值
@Override
public void insertFill(MetaObject metaObject) {
    //自动填充
    this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
    this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
    //逻辑删除
    this.strictInsertFill(metaObject, "deleted", Integer.class, 0);
    //乐观锁
    this.strictInsertFill(metaObject, "version", Integer.class, 1);
}
  1. 测试
@Test
public void testVersion() {
    User user = new User();
    user.setName("tom");
    user.setAge(20);
    user.setEmail("tom@163.com");
    userService.save(user);
    userService.list().forEach(System.out::println);
    user.setName("jarry");
    userService.update(user, null);
    userService.list().forEach(System.out::println);
}
posted @ 2021-09-10 09:06  Anzengsir  阅读(156)  评论(0)    收藏  举报