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())
5、通用json字段处理
第一:在@TableName 注解设置autoResultMap=true(此处不设置,可以正常insert/update,但是查询时不对)
第二:字段上@TableField 注解设置typeHandler=FastjsonTypeHandler.class
17 /** 18 * @date 2021/07/21 16:59:23 19 **/ 20 @Getter 21 @Setter 22 @TableName(value = "community_post", autoResultMap = true) 23 public class CommunityPostDO { 24 25 /** 26 * 主键id 27 * 28 **/ 29 @TableId(value = "id", type = IdType.AUTO) 30 private Long id; 31 61 /** 62 * 内容 63 * 64 **/ 65 @TableField(value = "content") 66 private String content; 67 68 /** 69 * 图片 70 * 71 **/ 72 @TableField(value = "pics", typeHandler = FastjsonTypeHandler.class) 73 private List<CommunityPostPicEntity> pics; 74 75 /** 76 * 视频 77 * 78 **/ 79 @TableField(value = "videos", typeHandler = StringArrayJsonHandler.class) 80 private List<String> videos; 138 }
6、逻辑删除配置
自定义sql不会走此配置,要自己写delete_flag = 0
1 mybatis-plus.mapper-locations=classpath:mybatis/*/*.xml 2 mybatis-plus.config-location=classpath:mybatis/mybatis-config.xml 3 mybatis-plus.type-handlers-package=com.odbpo.app.mybatis.handler 4 mybatis-plus.global-config.db-config.select-strategy=not_empty 5 mybatis-plus.global-config.db-config.id-type=auto 6 mybatis-plus.global-config.db-config.logic-delete-field=deleteFlag 7 mybatis-plus.global-config.db-config.logic-delete-value=1 8 mybatis-plus.global-config.db-config.logic-not-delete-value=0 9 mybatis-plus.global-config.db-config.insert-strategy=not_null
posted on 2021-08-02 11:42 Iversonstear 阅读(211) 评论(0) 编辑 收藏 举报