mybatis-plpus
1.mybatis-plus简介
MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis(opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
MyBatis-plus是对MyBatis的增强
1.1特性
- 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
- 损耗小:启动即会自动注入基本 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 操作智能分析阻断,也可自定义拦截规则,预防误操作
1.2快速开始
1.2.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.2.2导入相关依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--导入mysql数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--导入MyBatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
1.2.3数据库配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/one?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=FALSE
username: root
password: root
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #日志打印
1.2.4创建相关的实体类
package com.blb.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
/**
* @author smile
* @date 2021/7/8 0008 11:14
*/
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
1.2.5编写相关的mapper
package com.blb.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.blb.entity.User;
import org.apache.ibatis.annotations.Mapper;
/**
* @author smile
* @date 2021/7/8 0008 11:14
*/
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
1.2.6测试
@Autowired
private UserMapper userMapper;
@Test
public void text() {
List<User> userList = userMapper.selectList(null);
for (User user : userList) {
System.out.println(user);
}
}
2.注解
2.1@TableName
描述:表名注解
属性 | 类型 | 必须指定 | 默认值 | 描述 |
---|---|---|---|---|
value | String | 否 | “” | 表名 |
schema | String | 否 | “” | schema |
keepGlobalPrefix | boolean | 否 | false | 是否保持使用全局的 tablePrefix 的值(如果设置了全局 tablePrefix 且自行设置了 value 的值) |
resultMap | String | 否 | “” | xml 中 resultMap 的 id |
autoResultMap | boolean | 否 | false | 是否自动构建 resultMap 并使用(如果设置 resultMap 则不会进行 resultMap 的自动构建并注入) |
excludeProperty | String[] | 否 | {} |
需要排除的属性名(@since 3.3.1)
|
关于autoResultMap
的说明:
mp会自动构建一个ResultMap并注入到mybatis里(一般用不上).下面讲两句: 因为mp底层是mybatis,所以一些mybatis的常识你要知道,mp只是帮你注入了常用crud到mybatis里 注入之前可以说是动态的(根据你entity的字段以及注解变化而变化),但是注入之后是静态的(等于你写在xml的东西) 而对于直接指定typeHandler,mybatis只支持你写在2个地方:
定义在resultMap里,只作用于select查询的返回结果封装
定义在insert和updatesql的#{property}里的property后面(例:#{property,typehandler=xxx.xxx.xxx}),只作用于设置值 而除了这两种直接指定typeHandler,mybatis有一个全局的扫描你自己的typeHandler包的配置,这是根据你的property的类型去找typeHandler并使用.
2.2@TableId
描述:主键注解
属性 | 类型 | 必须指定 | 默认值 | 描述 |
---|---|---|---|---|
value | String | 否 | “” | 主键字段名 |
type | Enum | 否 | IdType.NONE | 主键类型 |
其中type的取值如下
字段 | 含义 |
---|---|
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方法) |
3.函数的使用
3.1 inert函数的使用
int insert(T entity);
T:实体类
举例
@Test
public void text5()
{
List<Account> accountList=new ArrayList<>();
Account account=new Account();
account.setMoney(5);
account.setName("x");
accountMapper.insert(account);
}
3.2delete函数的使用
// 根据 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< T > | wrapper | 实体对象封装操作类(可以为 null) |
Collection<? extends Serializable> | idList | 主键ID列表(不能为 null 以及 empty) |
Serializable | id | 主键ID |
Map<String, Object> | columnMap | 表字段 map 对象 |
/**
* 删除全部
*/
@Test
public void text6()
{
User user=new User();
user.setName("Jone");
user.setAge(18);
Wrapper<User> userWrapper=new UpdateWrapper<>();
User entity = userWrapper.getEntity();
userMapper.delete(userWrapper);
}
/**
* 根据主键ID删除
*/
@Test
public void text7()
{
userMapper.deleteById(1);
}
/**
* 根据主键的集合删除
*/
@Test
public void text8()
{
List<Integer> integerList=new ArrayList<>();
integerList.add(2);
integerList.add(3);
userMapper.deleteBatchIds(integerList);
}
/**
* 根据columnMap条件删除
*/
@Test
public void text9()
{
Map<String,Object> map=new HashMap<>();
map.put("name","Sandy");
map.put("age",21);
userMapper.deleteByMap(map);
}
3.2.1逻辑删除
逻辑删除:假删除,将对应数据中代表是否被删除字段状态修改为“被删除状态”,之后在数据库中仍旧能查看此条数据记录
使用步骤:
- 在user表中添加一个deleted的字段,用来表示是否被删除,0为否1为是
- 修改实体类user,添加逻辑删除的注解
package com.blb.domain;
import com.baomidou.mybatisplus.annotation.*;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.util.Date;
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
@KeySequence(value = "SEQ_ORACLE_STRING_KEY", clazz = String.class)
public class User {
@TableId(type = IdType.ASSIGN_ID)
private Long id;
@ApiModelProperty("姓名")
private String name;
@ApiModelProperty("年龄")
private int age;
@ApiModelProperty("邮箱")
private String email;
/**创建时间**/
@TableField(fill = FieldFill.INSERT)
private Date createTime;
/**更新时间**/
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
@Version
@TableField(fill = FieldFill.INSERT)
private Integer version;
@TableLogic
private int deleted;
}
测试
@Test
public void delete()
{
userMapper.deleteById(1);
}
3.3update 函数的使用
// 根据 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
public void text10()
{
User user=new User();
user.setName("Jone");
user.setAge(18);
user.setEmail("test1@baomidou.com");
Long aLong = new Long((long) 1);
user.setId(aLong);
userMapper.updateById(user);
}
@Test
public void text11()
{
UpdateWrapper<User> userUpdateWrapper=new UpdateWrapper<>();
userUpdateWrapper.eq("id",1);
User user=new User();
user.setName("Jone");
user.setAge(28);
user.setEmail("test1@baomidou.com");
userMapper.update(user,userUpdateWrapper);
}
3.4select函数的使用
// 根据 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< T > | queryWrappe | 实体对象封装操作类(可以为 null) |
Collection<? extends Serializable> | idList | 主键ID列表(不能为 null 以及 empty) |
Map<String, Object> | columnMap | 表字段 map 对象 |
IPage< T > | page | 分页查询条件(可以为 RowBounds.DEFAULT) |
/**
* 根据id进行查询
*/
@Test
public void text12()
{
User user = userMapper.selectById(1);
System.out.println(user);
}
/**
* 根据entity条件进行查询
*/
@Test
public void text13()
{
QueryWrapper<User> queryWrapper=new QueryWrapper<>();
queryWrapper.eq("age",28);
User user = userMapper.selectOne(queryWrapper);
System.out.println(user);
}
/**
* 更加id批量查询
*/
@Test
public void text14()
{
ArrayList<Integer> list=new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
List<User> userList = userMapper.selectBatchIds(list);
for(User user:userList)
{
System.out.println(user);
}
}
/**
* 查询根据columnMap
*/
@Test
public void text15()
{
Map<String,Object> map=new HashMap<>();
map.put("age",20);
List<User> userList = userMapper.selectByMap(map);
for(User user:userList)
{
System.out.println(user);
}
}
/**
* 根据wrapper条件查询记录总数
*/
@Test
public void text16()
{
QueryWrapper<User> queryWrapper=new QueryWrapper<>();
queryWrapper.lt("age",50);
Integer count = userMapper.selectCount(queryWrapper);
System.out.println(count);
}
/**
* 根据 entity 条件,查询全部记录(并翻页)
*/
@Test
public void text17()
{
Page<User> page = new Page<>(1, 2);
IPage<User> userIPage = userMapper.selectPage(page, null);
long total = userIPage.getTotal();
long pages = userIPage.getPages();
System.out.println(total);
System.out.println(pages);
}
3.4.1分页查询
MyBatis Plus自带分页插件,只要简单的配置即可实现分页功能
- 配置分页插件
/**分页插件**/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor1() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
return interceptor;
}
- 实现分页查询
@Test
public void textPage()
{
//创建page对象 参数:当前页和每页记录数
Page<User> page=new Page<>(1,3);
//调用mp分页查询方法,将数据封装到page里面
userMapper.selectPage(page, null);
System.out.println(page.getCurrent());//当前页
System.out.println(page.getRecords());//每页数据的list集合
long total = page.getTotal();//总记录
System.out.println(total);
long pages = page.getPages();//总页数
System.out.println(pages);
boolean b = page.hasNext();//下一页
System.out.println(b);
boolean b1 = page.hasPrevious();//上一页
System.out.println(b1);
}
3.5自动填充
不需要set到对象里面的值,使用mp方式实现数据的添加
在user表中添加create_time和update_time两个字段,在实体类上同样加上字段
3.5.1在实体类里面进行自动填充属性添加注解
package com.blb.domain;
import com.baomidou.mybatisplus.annotation.*;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.util.Date;
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
@KeySequence(value = "SEQ_ORACLE_STRING_KEY", clazz = String.class)
public class User {
@ApiModelProperty("id")
@TableId(type = IdType.ASSIGN_ID)
private Long id;
@ApiModelProperty("姓名")
private String name;
@ApiModelProperty("年龄")
private int age;
@ApiModelProperty("邮箱")
private String email;
/**创建时间**/
@TableField(fill = FieldFill.INSERT)
private Date createTime;
/**更新时间**/
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
}
3.5.2创建类,实现接口MetaObjectHandler里面的方法
package com.blb.config;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class DateHandler implements MetaObjectHandler {
//使用mp实现添加操作,执行此方法
@Override
public void insertFill(MetaObject metaObject) {
this.setFieldValByName("createTime",new Date(),metaObject);
this.setFieldValByName("updateTime",new Date(),metaObject);
}
//使用mp实现更新操作,执行此方法
@Override
public void updateFill(MetaObject metaObject) {
this.setFieldValByName("updateTime",new Date(),metaObject);
}
}
4.乐观锁
主要适用场景:当要更新一条记录的时候,希望这条记录没有被别人更新,也就是说实现线程安全的数据更新
丢失更新:多个人同时修改一条记录,最后提交把之前提交的数据覆盖
解决方案:悲观锁 乐观锁
4.1乐观锁实现方式
- 出记录时,获取当前version
- 取更新时,带上这个version
- 执行更新时, set version = newVersion where version = oldVersion
- 如果version不对,就更新失败
4.1.1具体实现
- 在表user中加入一个version字段,作为乐观锁的版本号并加上@Version注解并插入自动填充字段
/**版本号**/
@Version
@TableField(fill = FieldFill.INSERT)
private Integer version;
- 数据填充加入版本的默认值
//使用mp实现添加操作,执行此方法
@Override
public void insertFill(MetaObject metaObject) {
this.setFieldValByName("createTime",new Date(),metaObject);
this.setFieldValByName("updateTime",new Date(),metaObject);
this.setFieldValByName("version",1,metaObject);
}
- 在配置类中加入乐观锁的插件
@Configuration
public class MyConfig {
/**乐观锁插件**/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return mybatisPlusInterceptor;
}
}
- 测试(测试一定要先查询在修改)
@GetMapping("/updateUser/{id}")
@ApiOperation("修改用户信息")
public int updateUser(@PathVariable("id") long id)
{
User user1 = userMapper.selectById(id);
user1.setAge(200);
System.out.println(user1);
int i = userMapper.updateById(user1);
return i;
}
最全的代码生成器
@Test
public void getCode() {
// 1、创建代码生成器
AutoGenerator mpg = new AutoGenerator();
// 2、全局配置
GlobalConfig gc = new GlobalConfig();
/*
String projectPath = System.getProperty("user.dir");
System.out.println(projectPath);
*/
gc.setOutputDir("D:\\tools\\maven_project\\blb_parent\\service\\service_edu" + "/src/main/java");
gc.setAuthor("qf");
gc.setOpen(false); //生成后是否打开资源管理器
gc.setFileOverride(false); //重新生成时文件是否覆盖
/*
* mp生成service层代码,默认接口名称第一个字母有I
* UcenterService
* */
gc.setServiceName("%sService"); //去掉Service接口的首字母I
gc.setIdType(IdType.ASSIGN_ID); //主键策略
gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
gc.setSwagger2(true);//开启Swagger2模式
mpg.setGlobalConfig(gc);
// 3、数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/blb_edu?serverTimezone=GMT%2B8");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
// 4、包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName("edu"); //模块名
pc.setParent("cn.hp.service");
pc.setController("controller");
pc.setEntity("entity");
pc.setService("service");
pc.setMapper("mapper");
mpg.setPackageInfo(pc);
// 5、策略配置
StrategyConfig strategy = new StrategyConfig();
//strategy.setInclude("edu_teacher");
strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
strategy.setTablePrefix(pc.getModuleName() + "_"); //生成实体时去掉表前缀
// 数据库表字段映射到实体的命名策略
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// lombok 模型 @Accessors(chain = true) setter链式操作
strategy.setEntityLombokModel(true);
strategy.setLogicDeleteFieldName("is_deleted"); // 逻辑删除字段名
strategy.setEntityBooleanColumnRemoveIsPrefix(true); //去掉布尔值is_前缀
strategy.setRestControllerStyle(true); //restful api风格控制器
strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符
// 自动填充
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);
mpg.setStrategy(strategy);
// 6、执行
mpg.execute();
}