SpringCloud工作笔记089---SpringBoot中Mybatis使用Condition_Criteria如何筛选日期类型数据

 技术交流QQ群【JAVA,C++,Python,.NET,BigData,AI】:170933152 

看例子,做备份,防止以后会用:

 @GetMapping
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size,@RequestParam(name="sid",defaultValue = "") Integer sid,@RequestParam(name="uname",defaultValue = "") String uname,@RequestParam(name="type",defaultValue = "") Integer type,@RequestParam(name="startTime",defaultValue = "") String startTime,@RequestParam(name="endTime",defaultValue = "") String endTime,@RequestParam(name="info",defaultValue = "") String info,@RequestParam(name="sysType",defaultValue = "") Integer sysType) {
        PageHelper.startPage(page, size);
        Condition con=new Condition(SysLoginfo.class);
        Example.Criteria crea=con.createCriteria();
        if(CmUtil.isNotEmpty(uname)){
            crea.andEqualTo("uname",uname);
        }
        if (CmUtil.isNotEmpty(type)){
            crea.andEqualTo("type",type);
        }
        if(CmUtil.isNotEmpty(info)){
            crea.andEqualTo("info",info);
        }
        if(CmUtil.isNotEmpty(startTime)){
            Date startDate=new Date();
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//这里说明一下,日期类型的数据,从前端接过来,需要把接过来的日期字符串,转换
//成java日期类型,然后,拼在 crea.andGreaterThan("cretime",startDate);
//正常使用就可以了,一定要转成日期类型这样才起作用.
            try {
                startDate= sdf.parse(startTime);
                crea.andGreaterThan("cretime",startDate);
            }catch (Exception e){
                logger.error("SysLoginfoController:"+e.getMessage());
                e.printStackTrace();
            }

        }

        if(CmUtil.isNotEmpty(endTime)){
            Date endDate=new Date();
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            try {
                endDate= sdf.parse(endTime);
                crea.andLessThan("cretime",endDate);
            }catch (Exception e){
                logger.error("SysLoginfoController:"+e.getMessage());
                e.printStackTrace();
            }
        }

        List list =sysLoginfoService.findByCondition(con);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

 

你可能感兴趣的:(Spring,Cloud)