MyBatisPlus(简称MP)是基于MyBatis框架基础上开发的增强型工具,旨在简化开发、提高效率
MyBatisPlus的官网为:https://mp.baomidou.com/
int insert (T t)
T:泛型,新增用来保存新增数据
int:返回值,新增成功后返回1,没有新增成功返回的是0
在测试类中进行新增操作:
@SpringBootTest
class Mybatisplus01QuickstartApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testSave() {User user = new User();user.setName("yyds");user.setPassword("yyds");user.setAge(12);user.setTel("10086");userDao.insert(user);}
}
执行测试后,数据库表中就会添加一条数据。但是数据中的主键ID,有点长,这是主键ID生成策略决定的。
int deleteById (Serializable id)
Serializable:参数类型
思考:参数类型为什么是一个序列化类?
int:返回值类型,数据删除成功返回1,未删除数据返回0。
在测试类中进行操作:
@SpringBootTest
class Mybatisplus01QuickstartApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testDelete() {userDao.deleteById(1401856123725713409L);}
}
int updateById(T t);
T:泛型,需要修改的数据内容,注意因为是根据ID进行修改,所以传入的对象中需要有ID属性值
int:返回值,修改成功后返回1,未修改数据返回0
在测试类中进行新增操作:
@SpringBootTest
class Mybatisplus01QuickstartApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testUpdate() {User user = new User();user.setId(1L);user.setName("Tom888");user.setPassword("tom888");userDao.updateById(user);}
}
说明: 修改的时候,只修改实体对象中有值的字段。
T selectById (Serializable id)
@SpringBootTest
class Mybatisplus01QuickstartApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testGetById() {User user = userDao.selectById(2L);System.out.println(user);}
}
List selectList(Wrapper queryWrapper)
@SpringBootTest
class Mybatisplus01QuickstartApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testGetAll() {List userList = userDao.selectList(null);System.out.println(userList);}
}
我们所调用的方法都是来自于DAO接口继承的BaseMapper类中。里面的方法有很多。
分页查询使用的方法是:
IPage selectPage(IPage page, Wrapper queryWrapper)
IPage是一个接口,我们需要找到它的实现类来构建它,有一个实现类为Page
。
@SpringBootTest
class Mybatisplus01QuickstartApplicationTests {@Autowiredprivate UserDao userDao;//分页查询@Testvoid testSelectPage(){//1 创建IPage分页对象,设置分页参数,1为当前页码,3为每页显示的记录数IPage page=new Page<>(1,3);//2 执行分页查询userDao.selectPage(page,null);//3 获取分页结果System.out.println("当前页码值:"+page.getCurrent());System.out.println("每页显示数:"+page.getSize());System.out.println("一共多少页:"+page.getPages());System.out.println("一共多少条数据:"+page.getTotal());System.out.println("数据:"+page.getRecords());}
}
这个拦截器MP已经为我们提供好了,我们只需要将其配置成Spring管理的bean对象即可。
@Configuration
public class MybatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor(){//1 创建MybatisPlusInterceptor拦截器对象MybatisPlusInterceptor mpInterceptor=new MybatisPlusInterceptor();//2 添加分页拦截器mpInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());return mpInterceptor;}
}
如果想查看MP执行的SQL语句,可以修改application.yml配置文件,
mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #打印SQL日志到控制台
这个我们在前面都有见过,比如查询所有和分页查询的时候,都有看到过一个Wrapper
类,这个类就是用来构建查询条件的,如下图所示:
创建一个SpringBoot项目
pom.xml中添加对应的依赖
4.0.0 org.springframework.boot spring-boot-starter-parent 2.5.0 com.yyds mybatisplus_02_dql 0.0.1-SNAPSHOT 1.8 com.baomidou mybatis-plus-boot-starter 3.4.1 org.springframework.boot spring-boot-starter com.alibaba druid 1.1.16 mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-test test org.projectlombok lombok org.springframework.boot spring-boot-maven-plugin
编写UserDao接口
@Mapper
public interface UserDao extends BaseMapper {
}
编写模型类
@Data
public class User {private Long id;private String name;private String password;private Integer age;private String tel;
}
编写引导类
@SpringBootApplication
public class Mybatisplus02DqlApplication {public static void main(String[] args) {SpringApplication.run(Mybatisplus02DqlApplication.class, args);}}
编写配置文件
# dataSource
spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/mybatisplus_db?serverTimezone=UTCusername: rootpassword: root
# mp日志
mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
编写测试类
@SpringBootTest
class Mybatisplus02DqlApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testGetAll(){List userList = userDao.selectList(null);System.out.println(userList);}
}
application.yml添加如下内容:
# mybatis-plus日志控制台输出
mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImplglobal-config:banner: off # 关闭mybatisplus启动图标
取消SpringBoot的log打印
application.yml添加如下内容:
spring:main:banner-mode: off # 关闭SpringBoot启动图标(banner)
在进行查询的时候,我们的入口是在Wrapper这个类上,因为它是一个接口,所以我们需要去找它对应的实现类,关于实现类也有很多,说明我们有多种构建查询条件对象的方式,
@SpringBootTest
class Mybatisplus02DqlApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testGetAll(){QueryWrapper qw = new QueryWrapper();qw.lt("age",18);List userList = userDao.selectList(qw);System.out.println(userList);}
}
lt: 小于(<) ,最终的sql语句为
SELECT id,name,password,age,tel FROM user WHERE (age < ?)
第一种方式介绍完后,有个小问题就是在写条件的时候,容易出错,比如age写错,就会导致查询不成功
@SpringBootTest
class Mybatisplus02DqlApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testGetAll(){QueryWrapper qw = new QueryWrapper();qw.lambda().lt(User::getAge, 10);//添加条件List userList = userDao.selectList(qw);System.out.println(userList);}
}
SELECT id,name,password,age,tel FROM user WHERE (age < ?)
注意: 构建LambdaQueryWrapper的时候泛型不能省。
此时我们再次编写条件的时候,就不会存在写错名称的情况,但是qw后面多了一层lambda()调用
@SpringBootTest
class Mybatisplus02DqlApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testGetAll(){LambdaQueryWrapper lqw = new LambdaQueryWrapper();lqw.lt(User::getAge, 10);List userList = userDao.selectList(lqw);System.out.println(userList);}
}
这种方式就解决了上一种方式所存在的问题。
学完了三种构建查询对象的方式,每一种都有自己的特点,所以用哪一种都行,刚才都是一个条件,那如果有多个条件该如何构建呢?
需求:查询数据库表中,年龄在10岁到30岁之间的用户信息
@SpringBootTest
class Mybatisplus02DqlApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testGetAll(){LambdaQueryWrapper lqw = new LambdaQueryWrapper();lqw.lt(User::getAge, 30);lqw.gt(User::getAge, 10);List userList = userDao.selectList(lqw);System.out.println(userList);}
}
gt:大于(>),最终的SQL语句为
SELECT id,name,password,age,tel FROM user WHERE (age < ? AND age > ?)
构建多条件的时候,可以支持链式编程
LambdaQueryWrapper lqw = new LambdaQueryWrapper();
lqw.lt(User::getAge, 30).gt(User::getAge, 10);
List userList = userDao.selectList(lqw);
System.out.println(userList);
需求:查询数据库表中,年龄小于10或年龄大于30的数据
@SpringBootTest
class Mybatisplus02DqlApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testGetAll(){LambdaQueryWrapper lqw = new LambdaQueryWrapper();lqw.lt(User::getAge, 10).or().gt(User::getAge, 30);List userList = userDao.selectList(lqw);System.out.println(userList);}
}
or()就相当于我们sql语句中的or
关键字,不加默认是and
,最终的sql语句为:
SELECT id,name,password,age,tel FROM user WHERE (age < ? OR age > ?)
需求:查询数据库表中,根据输入年龄范围来查询符合条件的记录
用户在输入值的时候,
如果只输入第一个框,说明要查询大于该年龄的用户
如果只输入第二个框,说明要查询小于该年龄的用户
如果两个框都输入了,说明要查询年龄在两个范围之间的用户
思考第一个问题:后台如果想接收前端的两个数据,该如何接收?
我们可以使用两个简单数据类型,也可以使用一个模型类,但是User类中目前只有一个age属性,如:
@Data
public class User {private Long id;private String name;private String password;private Integer age;private String tel;
}
方案一:添加属性age2,这种做法可以但是会影响到原模型类的属性内容
@Data
public class User {private Long id;private String name;private String password;private Integer age;private String tel;private Integer age2;
}
方案二:新建一个模型类,让其继承User类,并在其中添加age2属性,UserQuery在拥有User属性后同时添加了age2属性。
@Data
public class User {private Long id;private String name;private String password;private Integer age;private String tel;
}@Data
public class UserQuery extends User {private Integer age2;
}
环境准备好后,我们来实现下刚才的需求:
@SpringBootTest
class Mybatisplus02DqlApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testGetAll(){//模拟页面传递过来的查询数据UserQuery uq = new UserQuery();uq.setAge(10);uq.setAge2(30);LambdaQueryWrapper lqw = new LambdaQueryWrapper();if(null != uq.getAge2()){lqw.lt(User::getAge, uq.getAge2());}if( null != uq.getAge()) {lqw.gt(User::getAge, uq.getAge());}List userList = userDao.selectList(lqw);System.out.println(userList);}
}
上面的写法可以完成条件为非空的判断,但是问题很明显,如果条件多的话,每个条件都需要判断,代码量就比较大,来看MP给我们提供的简化方式:
@SpringBootTest
class Mybatisplus02DqlApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testGetAll(){//模拟页面传递过来的查询数据UserQuery uq = new UserQuery();uq.setAge(10);uq.setAge2(30);LambdaQueryWrapper lqw = new LambdaQueryWrapper();lqw.lt(null!=uq.getAge2(),User::getAge, uq.getAge2());lqw.gt(null!=uq.getAge(),User::getAge, uq.getAge());List userList = userDao.selectList(lqw);System.out.println(userList);}
}
目前我们在查询数据的时候,什么都没有做默认就是查询表中所有字段的内容,我们所说的查询投影即不查询所有字段,只查询出指定内容的数据。
@SpringBootTest
class Mybatisplus02DqlApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testGetAll(){LambdaQueryWrapper lqw = new LambdaQueryWrapper();lqw.select(User::getId,User::getName,User::getAge);List userList = userDao.selectList(lqw);System.out.println(userList);}
}
select(…)方法用来设置查询的字段列,可以设置多个,最终的sql语句为:
SELECT id,name,age FROM user
如果使用的不是lambda,就需要手动指定字段
@SpringBootTest
class Mybatisplus02DqlApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testGetAll(){QueryWrapper lqw = new QueryWrapper();lqw.select("id","name","age","tel");List userList = userDao.selectList(lqw);System.out.println(userList);}
}
需求:聚合函数查询,完成count、max、min、avg、sum的使用
count:总记录数
max:最大值
min:最小值
avg:平均值
sum:求和
@SpringBootTest
class Mybatisplus02DqlApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testGetAll(){QueryWrapper lqw = new QueryWrapper();//lqw.select("count(*) as count");//SELECT count(*) as count FROM user//lqw.select("max(age) as maxAge");//SELECT max(age) as maxAge FROM user//lqw.select("min(age) as minAge");//SELECT min(age) as minAge FROM user//lqw.select("sum(age) as sumAge");//SELECT sum(age) as sumAge FROM userlqw.select("avg(age) as avgAge");//SELECT avg(age) as avgAge FROM userList
为了在做结果封装的时候能够更简单,我们将上面的聚合函数都起了个名称,方面后期来获取这些数据
需求:分组查询,完成 group by的查询使用
@SpringBootTest
class Mybatisplus02DqlApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testGetAll(){QueryWrapper lqw = new QueryWrapper();lqw.select("count(*) as count,tel");lqw.groupBy("tel");List
groupBy为分组,最终的sql语句为
SELECT count(*) as count,tel FROM user GROUP BY tel
注意:
MP的查询条件有很多:
需求:根据用户名和密码查询用户信息
@SpringBootTest
class Mybatisplus02DqlApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testGetAll(){LambdaQueryWrapper lqw = new LambdaQueryWrapper();lqw.eq(User::getName, "Jerry").eq(User::getPassword, "jerry");User loginUser = userDao.selectOne(lqw);System.out.println(loginUser);}
}
eq(): 相当于 =
,对应的sql语句为
SELECT id,name,password,age,tel FROM user WHERE (name = ? AND password = ?)
selectList:查询结果为多个或者单个
selectOne:查询结果为单个
需求:对年龄进行范围查询,使用lt()、le()、gt()、ge()、between()进行范围查询
@SpringBootTest
class Mybatisplus02DqlApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testGetAll(){LambdaQueryWrapper lqw = new LambdaQueryWrapper();lqw.between(User::getAge, 10, 30);//SELECT id,name,password,age,tel FROM user WHERE (age BETWEEN ? AND ?)List userList = userDao.selectList(lqw);System.out.println(userList);}
}
需求:查询表中name属性的值以
J
开头的用户信息,使用like进行模糊查询
@SpringBootTest
class Mybatisplus02DqlApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testGetAll(){LambdaQueryWrapper lqw = new LambdaQueryWrapper();lqw.likeLeft(User::getName, "J");//SELECT id,name,password,age,tel FROM user WHERE (name LIKE ?J)List userList = userDao.selectList(lqw);System.out.println(userList);}
}
需求:查询所有数据,然后按照id降序
@SpringBootTest
class Mybatisplus02DqlApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testGetAll(){LambdaQueryWrapper lwq = new LambdaQueryWrapper<>();/*** condition :条件,返回boolean,当condition为true,进行排序,如果为false,则不排序* isAsc:是否为升序,true为升序,false为降序* columns:需要操作的列*/lwq.orderBy(true,false, User::getId);userDao.selectList(lw}
}
除了上面介绍的这几种查询条件构建方法以外还会有很多其他的方法,比如isNull,isNotNull,in,notIn等等方法可供选择,具体参考官方文档的条件构造器来学习使用,具体的网址为:
https://mp.baomidou.com/guide/wrapper.html#abstractwrapper
前面我们已经能从表中查询出数据,并将数据封装到模型类中,这整个过程涉及到一张表和一个模型类:
当表的列名和模型类的属性名发生不一致,就会导致数据封装不到模型对象,这个时候就需要其中一方做出修改,那如果前提是两边都不能改又该如何解决?
MP给我们提供了一个注解@TableField
,使用该注解可以实现模型类属性名和表的列名之间的映射关系
当模型类中多了一个数据库表不存在的字段,就会导致生成的sql语句中在select的时候查询了数据库不存在的字段,程序运行就会报错,错误信息为:
Unknown column ‘多出来的字段名称’ in ‘field list’
具体的解决方案用到的还是@TableField
注解,它有一个属性叫exist
,设置该字段是否在数据库表中存在,如果设置为false则不存在,生成sql语句查询的时候,就不会再查询该字段了。
查询表中所有的列的数据,就可能把一些敏感数据查询到返回给前端,这个时候我们就需要限制哪些字段默认不要进行查询。解决方案是@TableField
注解的一个属性叫select
,该属性设置默认是否需要查询该字段的值,true(默认值)表示默认查询该字段,false表示默认不查询该字段。
名称 | @TableField |
---|---|
类型 | 属性注解 |
位置 | 模型类属性定义上方 |
作用 | 设置当前属性对应的数据库表中的字段关系 |
相关属性 | value(默认):设置数据库表字段名称 exist:设置属性在数据库表字段中是否存在,默认为true,此属性不能与value合并使用 select:设置属性是否参与查询,此属性与select()映射配置不冲突 |
该问题主要是表的名称和模型类的名称不一致,导致查询失败,这个时候通常会报如下错误信息:
Table ‘databaseName.tableNaem’ doesn’t exist,翻译过来就是数据库中的表不存在。
解决方案是使用MP提供的另外一个注解@TableName
来设置表与模型类之间的对应关系。
名称 | @TableName |
---|---|
类型 | 类注解 |
位置 | 模型类定义上方 |
作用 | 设置当前类对应于数据库表关系 |
相关属性 | value(默认):设置数据库表名称 |
查询相关的操作我们已经介绍完了,紧接着我们需要对另外三个,增删改进行内容的讲解。挨个来说明下,首先是新增(insert)中的内容。
不同的业务采用的ID生成方式应该是不一样的,那么在MP中都提供了哪些主键生成策略,以及我们该如何进行选择?
在这里我们又需要用到MP的一个注解叫@TableId
(@TableId(type = IdType.INPUT)
)
名称 | @TableId |
---|---|
类型 | 属性注解 |
位置 | 模型类中用于表示主键的属性定义上方 |
作用 | 设置当前类中主键属性的生成策略 |
相关属性 | value(默认):设置数据库表主键名称 type:设置主键属性的生成策略,值查照IdType的枚举值 |
从源码中可以看到,除了AUTO这个策略以外,还有如下几种生成策略:
分布式ID是什么?
介绍了这些主键ID的生成策略,我们以后该用哪个呢?
简化配置
mybatis-plus:global-config:db-config:id-type: assign_id
配置完成后,每个模型类的主键ID策略都将成为assign_id.
MP会默认将模型类的类名名首字母小写作为表名使用,假如数据库表的名称都以tbl_
开头,那么我们就需要将所有的模型类上添加@TableName
,如:
配置起来还是比较繁琐,简化方式为在配置文件中配置如下内容:
mybatis-plus:global-config:db-config:table-prefix: tbl_
设置表的前缀内容,这样MP就会拿 tbl_
加上模型类的首字母小写,就刚好组装成数据库的表名。
如何实现多条删除,我们找找对应的API方法
int deleteBatchIds(@Param(Constants.COLLECTION) Collection extends Serializable> idList);
需求:根据传入的id集合将数据库表中的数据删除掉。
@SpringBootTest
class Mybatisplus03DqlApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testDelete(){//删除指定多条数据List list = new ArrayList<>();list.add(1402551342481838081L);list.add(1402553134049501186L);list.add(1402553619611430913L);userDao.deleteBatchIds(list);}
}
执行成功后,数据库表中的数据就会按照指定的id进行删除。
除了按照id集合进行批量删除,也可以按照id集合进行批量查询,还是先来看下API
List selectBatchIds(@Param(Constants.COLLECTION) Collection extends Serializable> idList);
方法名称翻译为:查询(根据ID 批量查询),参数是一个集合,可以存放多个id值。
需求:根据传入的ID集合查询用户信息
@SpringBootTest
class Mybatisplus03DqlApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testGetByIds(){//查询指定多条数据List list = new ArrayList<>();list.add(1L);list.add(3L);list.add(4L);userDao.selectBatchIds(list);}
}
查询结果就会按照指定传入的id值进行查询
MP中逻辑删除具体该如何实现?
deleted
列字段名可以任意,内容也可以自定义,比如0
代表正常,1
代表删除,可以在添加列的同时设置其默认值为0
正常。
(1)添加与数据库表的列对应的一个属性名,名称可以任意,如果和数据表列名对不上,可以使用@TableField进行关系映射,如果一致,则会自动对应。
(2)标识新增的字段为逻辑删除字段,使用@TableLogic
@Data
//@TableName("tbl_user") 可以不写是因为配置了全局配置
public class User {@TableId(type = IdType.ASSIGN_UUID)private String id;private String name;@TableField(value="pwd",select=false)private String password;private Integer age;private String tel;@TableField(exist=false)private Integer online;@TableLogic(value="0",delval="1")//value为正常数据的值,delval为删除数据的值private Integer deleted;
}
@SpringBootTest
class Mybatisplus03DqlApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testDelete(){userDao.deleteById(1L);}
}
逻辑删除最后走的是update操作,会将指定的字段修改成删除状态对应的值。
思考
逻辑删除,对查询有没有影响呢?
执行查询操作
@SpringBootTest
class Mybatisplus03DqlApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testFind(){System.out.println(userDao.selectList(null));}
}
可想而知,MP的逻辑删除会将所有的查询都添加一个未被删除的条件,也就是已经被删除的数据是不应该被查询出来的。
如果还是想把已经删除的数据都查询出来该如何实现呢?
@Mapper
public interface UserDao extends BaseMapper {//查询所有数据包含已经被删除的数据@Select("select * from tbl_user")public List selectAll();
}
如果每个表都要有逻辑删除,那么就需要在每个模型类的属性上添加@TableLogic
注解,如何优化?
在配置文件中添加全局配置,如下:
mybatis-plus:global-config:db-config:# 逻辑删除字段名logic-delete-field: deleted# 逻辑删除字面值:未删除为0logic-not-delete-value: 0# 逻辑删除字面值:删除为1logic-delete-value: 1
名称 | @TableLogic |
---|---|
类型 | 属性注解 |
位置 | 模型类中用于表示删除字段的属性定义上方 |
作用 | 标识该字段为进行逻辑删除的字段 |
相关属性 | value:逻辑未删除值 delval:逻辑删除值 |
业务并发现象带来的问题:秒杀
简单来说,乐观锁主要解决的问题是当要更新一条记录的时候,希望这条记录没有被别人更新。
乐观锁的实现方式:
- 数据库表中添加version列,比如默认值给1
- 第一个线程要修改数据之前,取出记录时,获取当前数据库中的version=1
- 第二个线程要修改数据之前,取出记录时,获取当前数据库中的version=1
- 第一个线程执行更新时,set version = newVersion where version = oldVersion
- newVersion = version+1 [2]
- oldVersion = version [1]
- 第二个线程执行更新时,set version = newVersion where version = oldVersion
- newVersion = version+1 [2]
- oldVersion = version [1]
- 假如这两个线程都来更新数据,第一个和第二个线程都可能先执行
- 假如第一个线程先执行更新,会把version改为2,
- 第二个线程再更新的时候,set version = 2 where version = 1,此时数据库表的数据version已经为2,所以第二个线程会修改失败
- 假如第二个线程先执行更新,会把version改为2,
- 第一个线程再更新的时候,set version = 2 where version = 1,此时数据库表的数据version已经为2,所以第一个线程会修改失败
- 不管谁先执行都会确保只能有一个线程更新数据,这就是MP提供的乐观锁的实现原理分析。
分析完步骤后,具体的实现步骤如下:
列名可以任意,比如使用version
,给列设置默认值为1
根据添加的字段列名,在模型类中添加对应的属性值
@Data
//@TableName("tbl_user") 可以不写是因为配置了全局配置
public class User {@TableId(type = IdType.ASSIGN_UUID)private String id;private String name;@TableField(value="pwd",select=false)private String password;private Integer age;private String tel;@TableField(exist=false)private Integer online;private Integer deleted;@Versionprivate Integer version;
}
@Configuration
public class MpConfig {@Beanpublic MybatisPlusInterceptor mpInterceptor() {//1.定义Mp拦截器MybatisPlusInterceptor mpInterceptor = new MybatisPlusInterceptor();//2.添加乐观锁拦截器mpInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());return mpInterceptor;}
}
@SpringBootTest
class Mybatisplus03DqlApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testUpdate(){User user = new User();user.setId(3L);user.setName("Jock666");userDao.updateById(user);}
}
你会发现,这次修改并没有更新version字段,原因是没有携带version数据。
添加version数据
@SpringBootTest
class Mybatisplus03DqlApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testUpdate(){User user = new User();user.setId(3L);user.setName("Jock666");user.setVersion(1);userDao.updateById(user);}
}
你会发现,我们传递的是1,MP会将1进行加1,然后,更新回到数据库表中。
所以要想实现乐观锁,首先第一步应该是拿到表中的version,然后拿version当条件在将version加1更新回到数据库表中,所以我们在查询的时候,需要对其进行查询
@SpringBootTest
class Mybatisplus03DqlApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testUpdate(){//1.先通过要修改的数据id将当前数据查询出来User user = userDao.selectById(3L);//2.将要修改的属性逐一设置进去user.setName("Jock888");userDao.updateById(user);}
}
大概分析完乐观锁的实现步骤以后,我们来模拟一种加锁的情况,看看能不能实现多个人修改同一个数据的时候,只能有一个人修改成功。
@SpringBootTest
class Mybatisplus03DqlApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testUpdate(){//1.先通过要修改的数据id将当前数据查询出来User user = userDao.selectById(3L); //version=3User user2 = userDao.selectById(3L); //version=3user2.setName("Jock aaa");userDao.updateById(user2); //version=>4user.setName("Jock bbb");userDao.updateById(user); //verion=3?条件还成立吗?}
}
官方文档:
https://mp.baomidou.com/guide/interceptor-optimistic-locker.html#optimisticlockerinnerinterceptor
4.0.0 org.springframework.boot spring-boot-starter-parent 2.5.1 com.yyds mybatisplus_04_generator 0.0.1-SNAPSHOT 1.8 org.springframework.boot spring-boot-starter-web com.baomidou mybatis-plus-boot-starter 3.4.1 com.alibaba druid 1.1.16 mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-test test org.projectlombok lombok 1.18.12 com.baomidou mybatis-plus-generator 3.4.1 org.apache.velocity velocity-engine-core 2.3 org.springframework.boot spring-boot-maven-plugin
@SpringBootApplication
public class Mybatisplus04GeneratorApplication {public static void main(String[] args) {SpringApplication.run(Mybatisplus04GeneratorApplication.class, args);}}
public class CodeGenerator {public static void main(String[] args) {//1.获取代码生成器的对象AutoGenerator autoGenerator = new AutoGenerator();//设置数据库相关配置DataSourceConfig dataSource = new DataSourceConfig();dataSource.setDriverName("com.mysql.cj.jdbc.Driver");dataSource.setUrl("jdbc:mysql://localhost:3306/mybatisplus_db?serverTimezone=UTC");dataSource.setUsername("root");dataSource.setPassword("root");autoGenerator.setDataSource(dataSource);//设置全局配置GlobalConfig globalConfig = new GlobalConfig();globalConfig.setOutputDir(System.getProperty("user.dir")+"/mybatisplus_04_generator/src/main/java"); //设置代码生成位置globalConfig.setOpen(false); //设置生成完毕后是否打开生成代码所在的目录globalConfig.setAuthor("yyds"); //设置作者globalConfig.setFileOverride(true); //设置是否覆盖原始生成的文件globalConfig.setMapperName("%sDao"); //设置数据层接口名,%s为占位符,指代模块名称globalConfig.setIdType(IdType.ASSIGN_ID); //设置Id生成策略autoGenerator.setGlobalConfig(globalConfig);//设置包名相关配置PackageConfig packageInfo = new PackageConfig();packageInfo.setParent("com.aaa"); //设置生成的包名,与代码所在位置不冲突,二者叠加组成完整路径packageInfo.setEntity("domain"); //设置实体类包名packageInfo.setMapper("dao"); //设置数据层包名autoGenerator.setPackageInfo(packageInfo);//策略设置StrategyConfig strategyConfig = new StrategyConfig();strategyConfig.setInclude("tbl_user"); //设置当前参与生成的表名,参数为可变参数strategyConfig.setTablePrefix("tbl_"); //设置数据库表的前缀名称,模块名 = 数据库表名 - 前缀名 例如: User = tbl_user - tbl_strategyConfig.setRestControllerStyle(true); //设置是否启用Rest风格strategyConfig.setVersionFieldName("version"); //设置乐观锁字段名strategyConfig.setLogicDeleteFieldName("deleted"); //设置逻辑删除字段名strategyConfig.setEntityLombokModel(true); //设置是否启用lombokautoGenerator.setStrategy(strategyConfig);//2.执行生成操作autoGenerator.execute();}
}
对于代码生成器中的代码内容,我们可以直接从官方文档中获取代码进行修改,
https://mp.baomidou.com/guide/generator.html
运行成功后,会在当前项目中生成很多代码,代码包含controller
,service
,mapper
和entity
MP提供了一个Service接口和实现类,分别是:IService
和ServiceImpl
,后者是对前者的一个具体实现。
以后我们自己写的Service就可以进行如下修改:
public interface UserService extends IService{}@Service
public class UserServiceImpl extends ServiceImpl implements UserService{}
修改以后的好处是,MP已经帮我们把业务层的一些基础的增删改查都已经实现了,可以直接进行使用。
编写测试类进行测试:
@SpringBootTest
class Mybatisplus04GeneratorApplicationTests {private IUserService userService;@Testvoid testFindAll() {List list = userService.list();System.out.println(list);}}
注意: mybatisplus_04_generator项目中对于MyBatis的环境是没有进行配置,如果想要运行,需要提取将配置文件中的内容进行完善后在运行。
思考:在MP封装的Service层都有哪些方法可以用?
查看官方文档:https://mp.baomidou.com/guide/crud-interface.html