Mybatis-Plus分頁的使用與注意事項
1.寫個Mybatis-plus配置類:
是通過攔截器實現(xiàn)分頁
@Configuration
public class MybatisConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
官網(wǎng)復(fù)制即可,只是你需要把數(shù)據(jù)庫改為你使用的,這里我是使用mysql

2.寫接口測試
很簡單
@GetMapping("/test")
public Response test(){
Page<Produce> producePage = new Page<>(1,1);
Page<Produce> page = produceService.page(producePage);
System.out.println(producePage == page);
List<Produce> records = page.getRecords();
for (Produce record : records) {
System.out.println(record);
}
return new Response<>(records, ResultEnum.SUCCESS);
}

默認是會查詢總條數(shù),都有g(shù)et、set方法,可以根據(jù)自己的需求設(shè)置(點開Page類看看)

3.注意
我們傳入的page對象和查詢返回的page對象是同一個


4.如果你還有查詢條件
比如我們只查詢id和price,id小于5的分頁查詢

1.Lambda表達式
@GetMapping("/test")
public Response test(){
Page<Produce> producePage = new Page<>(1,2);
Page<Produce> page = new LambdaQueryChainWrapper<>(produceService.getBaseMapper())
.select(Produce::getPid,Produce::getPrice)
.lt(Produce::getPid,5)
.page(producePage);
return new Response<>(page.getRecords(), ResultEnum.SUCCESS);
}

2.普通查詢
@GetMapping("/test")
public Response test(){
Page<Produce> producePage = new Page<>(1,2);
QueryWrapper<Produce> queryWrapper = new QueryWrapper<>();
queryWrapper.select("pid","price");
queryWrapper.lt("pid",5);
Page<Produce> page = produceService.page(producePage, queryWrapper);
return new Response<>(page.getRecords(), ResultEnum.SUCCESS);
}


總結(jié)
到此這篇關(guān)于Mybatis-Plus分頁的使用與注意事項的文章就介紹到這了,更多相關(guān)Mybatis-Plus分頁使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot中使用jpa下hibernate的ddl-auto方式
這篇文章主要介紹了springboot中使用jpa下hibernate的ddl-auto方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
將RestTemplate的編碼格式改為UTF-8,防止亂碼問題
這篇文章主要介紹了將RestTemplate的編碼格式改為UTF-8,防止亂碼問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
java面試常見問題---ConcurrentHashMap
ConcurrentHashMap是由Segment數(shù)組結(jié)構(gòu)和HashEntry數(shù)組結(jié)構(gòu)組成。Segment的結(jié)構(gòu)和HashMap類似,是一種數(shù)組和鏈表結(jié)構(gòu),今天給大家普及java面試常見問題---ConcurrentHashMap知識,一起看看吧2021-06-06

