MybatisPlus實(shí)現(xiàn)分頁(yè)查詢和動(dòng)態(tài)SQL查詢的示例代碼
一、描述
實(shí)現(xiàn)下圖中的功能,分析一下該功能,既有分頁(yè)查詢又有根據(jù)計(jì)劃狀態(tài)、開(kāi)始時(shí)間、公司名稱進(jìn)行動(dòng)態(tài)查詢。

二、實(shí)現(xiàn)方式
Controller層
/**
* @param userId 專員的id
* @param planState 計(jì)劃狀態(tài)
* @param planStartTime 計(jì)劃開(kāi)始時(shí)間
* @param emtCode 公司名稱-分身id
* @return java.util.List<com.hc360.crm.entity.po.PlanCustomer>
* @Author zhaoxiaodong
* @Description 高級(jí)查詢-根據(jù)計(jì)劃狀態(tài)、計(jì)劃的時(shí)間、公司名稱查詢
* @Date 9:04 2021/9/29
*/
@PostMapping("/selectPlanByStateTimeCompany")
public Page<CrmCustomerPlan> selectPlanByStateTimeCompany(@RequestParam(required = false,defaultValue = "1")int limit, @RequestParam(required = false,defaultValue = "1")int page, @RequestParam(required = true) Long userId,@RequestParam(required = false,defaultValue = "0") int planState,@RequestParam(required = false) String planStartTime,@RequestParam(required = false) Long emtCode) {
//獲取該專員下所有狀態(tài)為未開(kāi)始的計(jì)劃
List<CrmCustomerPlan> myPlanList = crmCustomerPlanService.selectNoStartPlan(userId);
if (StringUtil.isNotEmpty(planStartTime)){
//判斷計(jì)劃的開(kāi)始時(shí)間和當(dāng)前時(shí)間
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime planTime = LocalDateTime.parse(planStartTime, dtf);
//存放已逾期的計(jì)劃
List<CrmCustomerPlan> overDuePlan = new ArrayList<>();
for (CrmCustomerPlan customerPlan : myPlanList) {
if (LocalDateTime.now().isAfter(planTime)) {
//當(dāng)前時(shí)間在計(jì)劃時(shí)間之后,說(shuō)明過(guò)了計(jì)劃時(shí)間,這時(shí)候我們要將它的狀態(tài)改為已逾期
customerPlan.setPlanState(PlanStateEnum.OVERDUE.getCode());
overDuePlan.add(customerPlan);
}
}
if (overDuePlan.size() > 0) {
//遍歷完之后,我們就可以對(duì)數(shù)據(jù)進(jìn)行更改了
crmCustomerPlanService.updateBatchById(overDuePlan);
}
}
//接下來(lái),就是對(duì)數(shù)據(jù)進(jìn)行查詢
return crmCustomerPlanService.selectPlanByStateTimeCompany(limit,page,userId, planState, planStartTime, emtCode);
}
在Controller中有l(wèi)imit、page。limit為每頁(yè)限制的數(shù)量、page為第幾頁(yè)
Service層
/**
* @param userId
* @return java.util.List<com.hc360.crm.entity.po.PlanCustomer>
* @Author zhaoxiaodong
* @Description 高級(jí)查詢-根據(jù)計(jì)劃狀態(tài)、時(shí)間、公司名稱查詢
* @Date 9:06 2021/9/29
*/
@Override
public Page<CrmCustomerPlan> selectPlanByStateTimeCompany(int limit,int page,Long userId, int planState, String planStartTime, Long emtCode) {
Page<CrmCustomerPlan> pagelimit= new Page(page,limit);
QueryWrapper<CrmCustomerPlan> crmCustomerPlanQueryWrapper = new QueryWrapper<>();
crmCustomerPlanQueryWrapper.eq("create_user_id", userId);
if (planState!=0){
crmCustomerPlanQueryWrapper.eq("plan_state", planState);
}
if (StringUtil.isNotEmpty(planStartTime)){
crmCustomerPlanQueryWrapper.eq("plan_start_time", planStartTime);
}
if (StringUtil.isNotEmpty(String.valueOf(emtCode))){
crmCustomerPlanQueryWrapper.eq("emt_code", emtCode);
}
return crmCustomerPlanMapper.selectPage(pagelimit,crmCustomerPlanQueryWrapper);
}
在Service層中,可以通過(guò)if和QueryWrapper實(shí)現(xiàn)動(dòng)態(tài)SQL的查詢。
分頁(yè),用到了Page對(duì)象,一定要是Mybatis的。然后調(diào)用selectPage,將對(duì)象和查詢條件傳入進(jìn)去即可。
三、 總結(jié)
MybatisPlus是真的好用,省了我們寫很多的SQL語(yǔ)句 以及配置信息
Mybatis的分頁(yè)配置信息
/**
* 新的分頁(yè)插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return mybatisPlusInterceptor;
}
到此這篇關(guān)于MybatisPlus實(shí)現(xiàn)分頁(yè)查詢和動(dòng)態(tài)SQL查詢的示例代碼的文章就介紹到這了,更多相關(guān)MybatisPlus 分頁(yè)查詢和動(dòng)態(tài)SQL查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot中使用mybatisplus自帶插件實(shí)現(xiàn)分頁(yè)的示例代碼
- 完美解決MybatisPlus插件分頁(yè)查詢不起作用總是查詢?nèi)繑?shù)據(jù)問(wèn)題
- 詳解MyBatisPlus如何實(shí)現(xiàn)分頁(yè)和查詢操作
- MybatisPlus分頁(yè)排序查詢字段帶有下劃線的坑及解決
- mybatisplus解除分頁(yè)限制的實(shí)現(xiàn)
- 詳解MybatisPlus3.4版本之后分頁(yè)插件的使用
- MyBatisPlus分頁(yè)時(shí)排序的實(shí)現(xiàn)
- MyBatis-Plus 分頁(yè)插件使用示例
相關(guān)文章
Java.try catch finally 的執(zhí)行順序說(shuō)明
這篇文章主要介紹了Java.try catch finally 的執(zhí)行順序說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
Java多線程之readwritelock讀寫分離的實(shí)現(xiàn)代碼
這篇文章主要介紹了Java多線程之readwritelock讀寫分離的相關(guān)內(nèi)容,文中涉及具體實(shí)例代碼,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10
通過(guò)實(shí)例解析Java分布式鎖三種實(shí)現(xiàn)方法
這篇文章主要介紹了通過(guò)實(shí)例解析Java分布式鎖三種實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Intellij IDEA基于Springboot的遠(yuǎn)程調(diào)試(圖文)
這篇文章主要介紹了Intellij IDEA基于Springboot的遠(yuǎn)程調(diào)試(圖文),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
SpringBoot服務(wù)上實(shí)現(xiàn)接口限流的方法
這篇文章主要介紹了SpringBoot服務(wù)上實(shí)現(xiàn)接口限流的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10

