ctrl + P = 查看可填的屬性類型?
alt + 回車 = 自動填充數(shù)據(jù)類型
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 ?
// 獲取當前分頁的內(nèi)容
System.out.println("1:"+page.getRecords());
// 獲取總頁數(shù)
System.out.println("2:"+page.getPages());
// 獲取總記錄數(shù)
System.out.println("3:"+page.getTotal());
// 是否有下一頁
System.out.println("4:"+page.hasNext());
// 是否有上一頁
System.out.println("5:"+page.hasPrevious());
}
type-aliasys-package配置pojo對象別名對應(yīng)的包
自定義的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 ?
樂觀鎖 和 悲觀鎖
悲觀鎖:
悲觀鎖的思想是,在操作數(shù)據(jù)之前,先假設(shè)其他并發(fā)操作會對數(shù)據(jù)進行修改,因此悲觀鎖會在對數(shù)據(jù)進行操作前,將其鎖定,確保其他操作無法訪問該數(shù)據(jù),直到當前操作完成。
一個常見的悲觀鎖的例子是數(shù)據(jù)庫中的行級鎖。當多個事務(wù)對數(shù)據(jù)庫中的同一行數(shù)據(jù)進行并發(fā)操作時,悲觀鎖會將該數(shù)據(jù)行鎖定,以防止其他事務(wù)修改該數(shù)據(jù)。只有當當前事務(wù)完成后,其他事務(wù)才能獲取到該行數(shù)據(jù)的鎖并執(zhí)行相應(yīng)的操作。
樂觀鎖:
樂觀鎖的思想是,假設(shè)在數(shù)據(jù)的操作過程中不會有其他事務(wù)對數(shù)據(jù)進行修改,因此樂觀鎖不會顯式地進行鎖定操作。相反,它會在操作完成時進行檢查以確保數(shù)據(jù)的一致性。
一個常見的樂觀鎖的例子是使用版本號(或者時間戳)來實現(xiàn)并發(fā)控制。每個數(shù)據(jù)記錄都有一個版本號,當執(zhí)行更新操作時,樂觀鎖會比較當前數(shù)據(jù)的版本號是否與執(zhí)行更新操作之前的版本號一致。如果一致,說明期間沒有其他操作對數(shù)據(jù)進行修改,更新可以繼續(xù)執(zhí)行;如果不一致,說明期間有其他操作對數(shù)據(jù)進行了修改,更新操作可能會被中斷或者重新執(zhí)行。
Mybatis-Plus插件實現(xiàn)樂觀鎖:
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 添加分頁插件
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
// 添加樂觀鎖插件
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
}
加上@Version注解,標識版本號文章來源:http://www.zghlxwxcb.cn/news/detail-518702.html
@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 將注解標注的值保存到數(shù)據(jù)庫文章來源地址http://www.zghlxwxcb.cn/news/detail-518702.html
@Getter
public enum SexEnum {
MALE(1, "男"),
FAMALE(2, "女");
@EnumValue // 將注解標注的值保存到數(shù)據(jù)庫
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);
}
到了這里,關(guān)于Mybatis-Plus學(xué)習4 Page分頁的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!