Mybatis-Plus学习4 Page分页

ctrl + P = 查看可填的属性类型 

alt + 回车 = 自动填充数据类型

1、使用Page分页需要先配置config类,加上拦截器

@Configuration
@MapperScan("com/learn/mybatisplus/mapper")
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

}
    @Test
    public void testPage() {

        Page<User> page = new Page<>(2, 3);
        userMapper.selectPage(page, null);
        //SELECT id,age,name,email,is_deleted FROM user WHERE is_deleted=0 LIMIT ?
        // 获取当前分页的内容
        System.out.println("1:"+page.getRecords());
        // 获取总页数
        System.out.println("2:"+page.getPages());
        // 获取总记录数
        System.out.println("3:"+page.getTotal());
        // 是否有下一页
        System.out.println("4:"+page.hasNext());
        // 是否有上一页
        System.out.println("5:"+page.hasPrevious());
    }

type-aliasys-package配置pojo对象别名对应的包

自定义的Mapper的page查询:

    /**
     * 通过年龄信息查询并分页
     * @param page mybatis-plus提供的封装好的属性,必须放在第一个位置
     * @param age
     * @return
     */
    Page<User> selectPageVo(@Param("page") Page<User> page, @Param("age") Integer age);


    <select id="selectPageVo" resultType="com.learn.mybatisplus.pojo.User">
        select id, name, age, email from user where age > #{age}
    </select>


        Page<User> page = new Page<>(1, 3);
        userMapper.selectPageVo(page, 20);
        //select id, name, age, email from user where age > ? LIMIT ?

乐观锁 和 悲观锁

悲观锁:
悲观锁的思想是,在操作数据之前,先假设其他并发操作会对数据进行修改,因此悲观锁会在对数据进行操作前,将其锁定,确保其他操作无法访问该数据,直到当前操作完成。

一个常见的悲观锁的例子是数据库中的行级锁。当多个事务对数据库中的同一行数据进行并发操作时,悲观锁会将该数据行锁定,以防止其他事务修改该数据。只有当当前事务完成后,其他事务才能获取到该行数据的锁并执行相应的操作。

乐观锁:
乐观锁的思想是,假设在数据的操作过程中不会有其他事务对数据进行修改,因此乐观锁不会显式地进行锁定操作。相反,它会在操作完成时进行检查以确保数据的一致性。

一个常见的乐观锁的例子是使用版本号(或者时间戳)来实现并发控制。每个数据记录都有一个版本号,当执行更新操作时,乐观锁会比较当前数据的版本号是否与执行更新操作之前的版本号一致。如果一致,说明期间没有其他操作对数据进行修改,更新可以继续执行;如果不一致,说明期间有其他操作对数据进行了修改,更新操作可能会被中断或者重新执行。

Mybatis-Plus插件实现乐观锁:

public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        // 添加分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));

        // 添加乐观锁插件
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }

}

加上@Version注解,标识版本号

@Data
@TableName("t_product")
public class Product {

    private Long id;
    private String name;
    private Integer price;
    @Version
    private Integer version;
}
    @Test
    public void testProduct01() {
        // 小李查询商品价格
        Product productLi = productMapper.selectById(1);
        System.out.println("小李查询的商品价格" + productLi.getPrice());
        Product productWang = productMapper.selectById(1);
        System.out.println("小王查询的商品价格" + productLi.getPrice());

        // 小李将商品价格加50
        productLi.setPrice(productLi.getPrice() + 50);
        productMapper.updateById(productLi);
        // 小王将商品价格减50
        productWang.setPrice(productWang.getPrice() - 30);
        int result = productMapper.updateById(productWang);
        if (result == 0) {
            Product productNew = productMapper.selectById(1);
            productNew.setPrice(productNew.getPrice() - 30);
            productMapper.updateById(productNew);
        }

        // 老板查询商品价格
        Product productBoss = productMapper.selectById(1);
        System.out.println("老板查询的商品价格" + productBoss.getPrice());

    }

枚举,@EnumValue 将注解标注的值保存到数据库

@Getter
public enum SexEnum {

    MALE(1, "男"),
    FAMALE(2, "女");

    @EnumValue // 将注解标注的值保存到数据库
    private Integer sex;
    private String sexName;

    SexEnum(Integer sex, String sexName) {
        this.sex = sex;
        this.sexName = sexName;
    }
}
@TableName("user")
public class User {

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @TableField("age")
    private Integer age;

    @TableField("name")
    private String name;

    @TableField("email")
    private String email;

    @TableField("is_deleted")
    @TableLogic
    private Integer isDeleted;

    private SexEnum sex;

}
    @Test
    public void test() {
        User user = new User();
        user.setName("admin");
        user.setAge(33);
        user.setSex(SexEnum.MALE);
        int result = userMapper.insert(user);
        System.out.println("result = " + result);
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值