ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

mybatis-plus笔记

2021-09-30 14:30:11  阅读:147  来源: 互联网

标签:p2 小王 30 笔记 version plus productMapper mybatis public


分页插件

MyBatis Plus自带分页插件,只要简单的配置即可实现分页功能

  • 添加配置类 创建config包,创建MybatisPlusConfig类
package com.atguigu.mybatisplus.config;

@Configuration
 //可以将主类中的注解移到此处
@MapperScan("com.atguigu.mybatisplus.mapper") 
public class MybatisPlusConfig {

}
  • 添加分页插件 配置类中添加@Bean配置
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
    return interceptor;
}
  • 测试分页 创建类InterceptorTests
package com.atguigu.mybatisplus;

@SpringBootTest
public class InterceptorTests {

    @Resource
    private UserMapper userMapper;

    @Test
    public void testSelectPage(){

        //创建分页参数
        Page<User> pageParam = new Page<>(1,5);
        //执行分页查询
        userMapper.selectPage(pageParam, null);
        //查看分页参数的成员
        System.out.println(pageParam);
    }
}

乐观锁

场景
一件商品,成本价是80元,售价是100元。老板先是通知小李,说你去把商品价格增加50元。小李正在玩游戏,耽搁了一个小时。正好一个小时后,老板觉得商品价格增加到150元,价格太高,可能会影响销量。又通知小王,你把商品价格降低30元。
此时,小李和小王同时操作商品后台系统。小李操作的时候,系统先取出商品价格100元;小王也在操作,取出的商品价格也是100元。小李将价格加了50元,并将100+50=150元存入了数据库;小王将商品减了30元,并将100-30=70元存入了数据库。是的,如果没有锁,小李的操作就完全被小王的覆盖了。
现在商品价格是70元,比成本价低10元。几分钟后,这个商品很快出售了1千多件商品,老板亏1万多。

普通方案

  1. 数据库中增加商品表
CREATE TABLE product
(
    id BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    name VARCHAR(30) NULL DEFAULT NULL COMMENT '商品名称',
    price INT(11) DEFAULT 0 COMMENT '价格',
    version INT(11) DEFAULT 0 COMMENT '乐观锁版本号',
    PRIMARY KEY (id)
);

INSERT INTO product (id, NAME, price) VALUES (1, '笔记本', 100);
  1. 创建实体类
@Data
public class Product {
    private Long id;
    private String name;
    private Integer price;
    private Integer version;
}
  1. 创建Mapper
public interface ProductMapper extends BaseMapper<Product> {
    
}

4.测试

@Resource
private ProductMapper productMapper;

@Test
public void testConcurrentUpdate() {

    //1、小李
    Product p1 = productMapper.selectById(1L);

    //2、小王
    Product p2 = productMapper.selectById(1L);

    //3、小李将价格加了50元,存入了数据库
    p1.setPrice(p1.getPrice() + 50);
    int result1 = productMapper.updateById(p1);
    System.out.println("小李修改结果:" + result1);

    //4、小王将商品减了30元,存入了数据库
    p2.setPrice(p2.getPrice() - 30);
    int result2 = productMapper.updateById(p2);
    System.out.println("小王修改结果:" + result2);

    //最后的结果
    Product p3 = productMapper.selectById(1L);
    System.out.println("最后的结果:" + p3.getPrice());
}

乐观锁方案

  1. 数据库中添加version字段:取出记录时,获取当前version
SELECT id,`name`,price,`version` FROM product WHERE id=1
  1. 更新时,version + 1,如果where语句中的version版本不对,则更新失败
UPDATE product SET price=price+50, `version`=`version` + 1 WHERE id=1 AND `version`=1

乐观锁实现流程

  1. 修改实体类 添加 @Version 注解
@Version
private Integer version;
  1. 添加乐观锁插件
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());//乐观锁

3.重新执行测试
小王的修改失败!

4.优化流程

if(result2 == 0){//更新失败,重试
    System.out.println("小王重试");
    //重新获取数据
    p2 = productMapper.selectById(1L);
    //更新
    p2.setPrice(p2.getPrice() - 30);
    productMapper.updateById(p2);
}

标签:p2,小王,30,笔记,version,plus,productMapper,mybatis,public
来源: https://blog.csdn.net/weixin_45087693/article/details/120565912

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有