mybaits-plus 一些写法

1、不管condition是否满足,后面跟着的入参如果是表达式的都会去执行,所以要进行判空处理npe

1 lambdaQuery().setEntity(ObjectUtils.copy(param, EvaluationDO.class))
2                 .ge(Objects.nonNull(param.getRateGe()), EvaluationDO::getRate, Objects.isNull(param.getRateGe())? null: Math.toIntExact(EvaluationRateUtil.mul100(param.getRateGe())))

 

2、处理模糊查询,匹配两个字段

lambdaQuery().setEntity(qryDO)
                .in(!Objects.isNull(param.getIds()), CommunityPostDO::getId, param.getIds())
                .like(StringUtils.isNotBlank(param.getTitleLike()), CommunityPostDO::getTitle, param.getTitleLike())
                .and(StringUtils.isNotBlank(param.getKeywords()), (queryWrapper -> queryWrapper.like(CommunityPostDO::getTitle, param.getKeywords())
                        .or()
                        .like(CommunityPostDO::getContent, param.getKeywords())))
                .ge(Objects.nonNull(param.getPublishTimeStart()), CommunityPostDO::getGmtCreated, param.getPublishTimeStart())
                .le(Objects.nonNull(param.getPublishTimeEnd()), CommunityPostDO::getGmtCreated, param.getPublishTimeEnd())

 

3、排序

1 .orderByDesc(Objects.equals(CommunitySortTypeEnum.HOT.getType(), param.getSortType()) ?
2                         CommunityPostDO::getHot :
3                         Objects.equals(CommunitySortTypeEnum.LATEST.getType(), param.getSortType()) ?
4                                 CommunityPostDO::getGmtCreated :
5                                 CommunityPostDO::getGmtModified)

 

4、大于小于,ge/le

1 lambdaQuery().setEntity(qryDO)
2                 .ge(Objects.nonNull(param.getPublishTimeStart()), CommunityPostDO::getGmtCreated, param.getPublishTimeStart())
3                 .le(Objects.nonNull(param.getPublishTimeEnd()), CommunityPostDO::getGmtCreated, param.getPublishTimeEnd())

 

mybaits-plus 一些写法

上一篇:几百行代码写个Mybatis,原理搞的透透的!


下一篇:10.1 socket 编程