栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

MybatisPlus实现分页查询和动态SQL查询

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MybatisPlus实现分页查询和动态SQL查询

一、描述

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

二、实现方式

Controller层

   
    @PostMapping("/selectPlanByStateTimeCompany")
    public Page 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 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 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层

    
   @Override
   public Page selectPlanByStateTimeCompany(int limit,int page,Long userId, int planState, String planStartTime, Long emtCode) {
       Page pagelimit= new Page(page,limit);
       QueryWrapper 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的分页配置信息

  
  @Bean
  public MybatisPlusInterceptor mybatisPlusInterceptor() {
      MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
      mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
      return mybatisPlusInterceptor;
  }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/274497.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号