四、插件
4.1 MybatisX快速开发插件
两个功能:
Java与XML调回跳转
Mapper方法自动生成XML
4.2 乐观锁插件
锁的机制:
锁的机制主要用来处理数据冲突之后的解决方式,比如在关系型数据库里,如果一个事物执行的操作对某个数据加了锁,那么只有当这个事物把锁释放,其他的事物才能执行与改锁冲突的操作
4.2.1 悲观锁
正如其名,它指的是对数据被外界修改持保守(悲观),因此在整个数据处理过程中,将数据处于锁定状态。悲观锁的实现,往往依靠数据库提供的锁机制
4.2.2 乐观锁
相对悲观锁而言,乐观锁假设认为数据一般情况下不会有冲突,所以在数据进行提交更新的时候,才会正式对数据的冲突与否进行检测,如果发现了冲突,则让返回用户错误的信息,让用户决定如何去做。乐观锁的实现方式一般是记录数据版本
4.2.3 乐观锁的实现方式
取出记录,获取当前Version
更新时,带上这个version
执行更新时,set version = newVersion where version = oldVersion
如果version不对,就更新失败
4.2.4主要适用场景
意图: 当要更新一条记录的时候,希望这条记录没有被别人更新
乐观锁实现方式:
取出记录时,获取当前version
更新时,带上这个version 执行更新时, set version = newVersion where version = oldVersion
如果version不对,就更新失败
插件配置
@Bean public OptimisticLockerInterceptor optimisticLockerInterceptor(){ return new OptimisticLockerInterceptor(); }
注解实体类
package com.pyy.mp.pojo; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.Version; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class User { @TableId(type = IdType.AUTO) private Long id; private String name; private Integer age; private String email; @Version private Integer version; }
测试
@Test public void testUpdate() { User user = new User(); user.setAge(30); user.setId(2L); user.setVersion(1);//获取到version为1 userMapper.updateById(user); } ==> Preparing: UPDATE user SET age=?, version=? WHERE id=? AND version=? ==> Parameters: 30(Integer), 2(Integer), 2(Long), 1(Integer) <== Updates: 0 Time:0 ms - ID:com.pyy.mp.mapper.UserMapper.updateById Execute SQL: UPDATE user SET age=30, version=2 WHERE id=2 AND version=1
可以看到sql语句中有version条件,更新时会修改verison为2。
如果再次执行,更新则不成功。这样就避免了多人更新时导致数据的不一致
4.2.5特别说明
支持的数据类型只有:int,Integer,long,Long,Date,Timestamp,LocalDateTime
整数类型下 newVersion = oldVersion + 1
newVersion 会回写到 entity 中
仅支持 updateById(id) 与 update(entity, wrapper) 方法
在 update(entity, wrapper) 方法下, wrapper 不能复用!!!
4.3 性能分析插件
@profile 注解的作用是指定类或方法在特定的 Profile 环境生效
4.3.1 在配置类添加插件
package com.pyy.mp.conf; import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; @Configuration @MapperScan("com.pyy.mp") public class MPConfig { @Bean//配置分页插件 public PaginationInterceptor paginationInterceptor(){ return new PaginationInterceptor(); } @Bean @Profile({"dev","test"}) //设置dev test环境开启,保证效率 public PerformanceInterceptor performanceInterceptor(){ PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor(); performanceInterceptor.setMaxTime(1);//设置sql执行的最大时间 performanceInterceptor.setFormat(true); return performanceInterceptor; } }
4.3.2 yaml文件
spring: datasource: username: root password: root url: jdbc:mysql://localhost:3306/mp driver-class-name: com.mysql.cj.jdbc.Driver profiles: active: dev
4.3.3 测试
可以看到代码被格式化了,但是确报错了。因为我们前面设置了最大查询时间为一毫秒,所以我们可以通过这个插件来进行调优,看看哪些语句执行时间太长
修改后在进行测试
4.4 分页插件
配置类中添加分页插件
//分页插件 @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); }
测试
/** * 测试分页查询 */ @Test public void testPage() { //创建page对象 // 传入两个参数,当前页和每页显示记录数 Page<User> userPage = new Page<>(1,3); // 调用mp分页查询的方法 // 调用mp分页查询的过程中,底层封装 // 把分页所有数据封装到page对象里面 userMapper.selectPage(userPage,null); System.out.println(userPage.getCurrent());//获取当前页 System.out.println(userPage.getSize());//每页显示记录数 System.out.println(userPage.getTotal());//总记录数 System.out.println(userPage.getPages());//总页数 System.out.println(userPage.hasNext());//判断是否有下一页 System.out.println(userPage.hasPrevious());//判断是否有上一页 }