一、描述
實現下圖中的功能,分析一下該功能,既有分頁查詢又有根據計劃狀態、開始時間、公司名稱進行動態查詢。
二、實現方式
Controller層
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
/** * @param userId 專員的id * @param planState 計劃狀態 * @param planStartTime 計劃開始時間 * @param emtCode 公司名稱-分身id * @return java.util.List<com.hc360.crm.entity.po.PlanCustomer> * @Author zhaoxiaodong * @Description 高級查詢-根據計劃狀態、計劃的時間、公司名稱查詢 * @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) { //獲取該專員下所有狀態為未開始的計劃 List<CrmCustomerPlan> myPlanList = crmCustomerPlanService.selectNoStartPlan(userId); if (StringUtil.isNotEmpty(planStartTime)){ //判斷計劃的開始時間和當前時間 DateTimeFormatter dtf = DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss" ); LocalDateTime planTime = LocalDateTime.parse(planStartTime, dtf); //存放已逾期的計劃 List<CrmCustomerPlan> overDuePlan = new ArrayList<>(); for (CrmCustomerPlan customerPlan : myPlanList) { if (LocalDateTime.now().isAfter(planTime)) { //當前時間在計劃時間之后,說明過了計劃時間,這時候我們要將它的狀態改為已逾期 customerPlan.setPlanState(PlanStateEnum.OVERDUE.getCode()); overDuePlan.add(customerPlan); } } if (overDuePlan.size() > 0 ) { //遍歷完之后,我們就可以對數據進行更改了 crmCustomerPlanService.updateBatchById(overDuePlan); } } //接下來,就是對數據進行查詢 return crmCustomerPlanService.selectPlanByStateTimeCompany(limit,page,userId, planState, planStartTime, emtCode); } |
在Controller中有limit、page。limit為每頁限制的數量、page為第幾頁
Service層
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
/** * @param userId * @return java.util.List<com.hc360.crm.entity.po.PlanCustomer> * @Author zhaoxiaodong * @Description 高級查詢-根據計劃狀態、時間、公司名稱查詢 * @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層中,可以通過if和QueryWrapper實現動態SQL的查詢。
分頁,用到了Page對象,一定要是Mybatis的。然后調用selectPage,將對象和查詢條件傳入進去即可。
三、 總結
MybatisPlus是真的好用,省了我們寫很多的SQL語句 以及配置信息
Mybatis的分頁配置信息
1
2
3
4
5
6
7
8
9
|
/** * 新的分頁插件 */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); mybatisPlusInterceptor.addInnerInterceptor( new PaginationInnerInterceptor(DbType.MYSQL)); return mybatisPlusInterceptor; } |
到此這篇關于MybatisPlus實現分頁查詢和動態SQL查詢的示例代碼的文章就介紹到這了,更多相關MybatisPlus 分頁查詢和動態SQL查詢內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/MyxZxd/article/details/120551766