服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - MybatisPlus实现分页查询和动态SQL查询的示例代码

MybatisPlus实现分页查询和动态SQL查询的示例代码

2022-01-24 13:02赵晓东-Nastu Java教程

本文主要介绍了MybatisPlus实现分页查询和动态SQL查询的示例代码,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

一、描述

实现下图中的功能,分析一下该功能,既有分页查询又有根据计划状态、开始时间、公司名称进行动态查询。

MybatisPlus实现分页查询和动态SQL查询的示例代码

二、实现方式

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

延伸 · 阅读

精彩推荐