• MybatisPlus乐观锁悲观锁和分页查询逻辑删除


    目录

     乐观锁和悲观锁

    测试乐观锁单线程成功的例子 

     乐观锁多线程失败案例

    查询操作

    分页查询

     删除操作

    删除单个

     删除多个

     按map删除

    逻辑删除


     乐观锁和悲观锁

    乐观锁:顾名思义十分乐观,他总是认为不会出现问题,无论干什么都不去上锁,如果出现了问题,在次更新值测试

    在数据库中新增字段version,在实体类中也要增加对应的,使用@Version表示他是一个乐观锁

    乐观锁:先查询,获取它的版本号,然后在一系列的操作后,让他的版本号+1,前提是这个版本号还是之前查出来的那个版本号。

    1. @Version//乐观锁
    2. private Integer version;

     写一个配置类

    1. @Configuration
    2. @MapperScan("com.mapper")
    3. public class MyBatisPlusConfig {
    4. @Bean
    5. public MybatisPlusInterceptor mybatisPlusInterceptor() {
    6. MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    7. interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
    8. return interceptor;
    9. }
    10. }

    测试乐观锁单线程成功的例子 

     更新下

    1. @Test
    2. void update() {
    3. User user = userMapper.selectById(5L);
    4. user.setName("kongchaoAfter");
    5. user.setAge(21);
    6. user.setEmail("kc@qq.com");
    7. userMapper.updateById(user);
    8. }

     说明:

    • 支持的数据类型只有:int,Integer,long,Long,Date,Timestamp,LocalDateTime
    • 整数类型下 newVersion = oldVersion + 1
    • newVersion 会回写到 entity 中
    • 仅支持 updateById(id) 与 update(entity, wrapper) 方法
    • 在 update(entity, wrapper) 方法下, wrapper 不能复用!!!

     

     乐观锁多线程失败案例

    1. @Test
    2. void update2() {
    3. //线程1
    4. User user = userMapper.selectById(5L);
    5. user.setName("kongchao1");
    6. user.setAge(11);
    7. user.setEmail("kc1@qq.com");
    8. //模拟另一个线程插队
    9. User user2 = userMapper.selectById(5L);
    10. user2.setName("kongchao2");
    11. user2.setAge(22);
    12. user2.setEmail("kc2@qq.com");
    13. userMapper.updateById(user2);
    14. //若没有乐观锁,则会覆盖user2的操作
    15. userMapper.updateById(user);
    16. }

    数据库 

     运行结果

    查询操作

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

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

    1. @SpringBootTest
    2. @MapperScan("com.mapper")//扫描mapper,自动创建实现类
    3. class MybatisPlusQuickApplicationTests {
    4. @Autowired
    5. private UserMapper userMapper;
    6. //按条件查询之一使用map操作
    7. @Test
    8. public void testSelectByBatchIds(){
    9. HashMap map = new HashMap<>();
    10. // 自定义要查询
    11. map.put("name","kc");
    12. map.put("age",18);
    13. List users = userMapper.selectByMap(map);
    14. users.forEach(System.out::println);
    15. }
    16. }

    分页查询

    在配置类中加入分页插件

    1. @Configuration
    2. @MapperScan("com.mapper")
    3. public class MyBatisPlusConfig {
    4. // 分页插件
    5. @Bean
    6. public PaginationInterceptor paginationInterceptor(){
    7. return new PaginationInterceptor();
    8. }
    9. }

    在测试类中测试

    1. //测试分页查询
    2. @Test
    3. public void pageTest(){
    4. //第一页,每页三个数据
    5. Page page=new Page<>(1, 3);
    6. userMapper.selectPage(page, null);
    7. page.getRecords().forEach(System.out::print);
    8. }

    可以获取各种参数 

    运行结果

     删除操作

    删除单个

    1. @Test
    2. public void deleteById(){
    3. userMapper.deleteById(1583366844257988614L);
    4. }

     删除多个

    1. //删除多个
    2. @Test
    3. public void deleteByIds(){
    4. userMapper.deleteBatchIds(Arrays.asList(1583366844257988612L,1583366844257988613L));
    5. }

     按map删除

    1. //通过map删除
    2. @Test
    3. public void deleteMap(){
    4. HashMap hashMap=new HashMap<>();
    5. hashMap.put("name", "kc");
    6. userMapper.deleteByMap(hashMap);
    7. }

    逻辑删除

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

    逻辑删除:数据还存在数据库中,是增加了一个deleted字段,以这个字段作为条件,当字段=1时不显示这条数据

    这个功能类似于回收站。并没有真正删除。

    在数据库中添加字段deleted然后在实体类中也加入,并带上注解

    1. @TableLogic//逻辑删除
    2. private Integer deleted;

     在application.yaml配置文件中配置

    1. mybatis-plus:
    2. global-config:
    3. db-config:
    4. # 没有逻辑删除的显示为0,否则显示1
    5. logic-delete-value: 1
    6. logic-not-delete-value: 0

    测试之前的删除

    1. //逻辑删除单个
    2. @Test
    3. public void deleteById(){
    4. userMapper.deleteById(6L);
    5. }

     运行结果,删除只是将deleted字段变为1

     测试查询

    1. // 测试查询
    2. @Test
    3. public void testSelectById(){
    4. User user = userMapper.selectById(6L);
    5. }

    运行结果,发现也会加上deleted=0的条件

  • 相关阅读:
    HashMap集合
    第二章:Qt下载与安装 之 2.2 Qt安装
    低代码与医疗的结合
    插入排序和归并排序
    2023 ICPC 网络赛 第一场 部分题解 (待完善)
    对象头markword-锁升级
    Docker部署前后端服务示例
    考研机试题
    Spring Boot文档阅读笔记-3 Ways to Add Custom Header in Spring SOAP Request
    【数据结构】栈(stack)
  • 原文地址:https://blog.csdn.net/weixin_60719453/article/details/127817816