记录mybatis-plus 根据自定义注解反射实现条件拼接查询
Posted 天道酬勤
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了记录mybatis-plus 根据自定义注解反射实现条件拼接查询相关的知识,希望对你有一定的参考价值。
自定义注解动态拼接查询条件
引入依赖
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> </dependency> <!--swagger2--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> </dependency>
QueryWapper自定义注解
import com.supconit.its.generator.enums.QueryWapperEnum; import org.springframework.stereotype.Indexed; import java.lang.annotation.*; @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Documented @Indexed public @interface QueryWapper { String field() default ""; QueryWapperEnum queryWapperEnum() default QueryWapperEnum.EQ ; }
QueryWapperEnum枚举类
/** * @Description 查询枚举类 * @Date 2020-07-16 16:10 */ public enum QueryWapperEnum { LIKE(1),EQ(2); private final int value; QueryWapperEnum(int value) { this.value = value; } public int value() { return this.value; } }
实体类加注解
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import io.swagger.annotations.ApiParam; import com.baomidou.mybatisplus.annotation.TableField; import com.xx.xx.generator.stereotype.QueryWapper; import com.xx.xx.generator.enums.QueryWapperEnum; @Data @ApiModel(value = "XX", description = "XX") public class PersBO { @ApiModelProperty(value = " 姓名 ") @TableField(value = "name ") @ApiParam(value = " 姓名 ",example = "",required = false) @QueryWapper(field = "name",queryWapperEnum = QueryWapperEnum.EQ ) private String name; /** * 当前页数 */ @ApiModelProperty(value = "当前页数", example = "1", required = true) private Integer current; /** * 每页条数 */ @ApiModelProperty(value = "每页条数", example = "10", required = true) private Integer size; }
QueryWrapperUtil工具类
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.supconit.its.generator.enums.QueryWapperEnum; import com.supconit.its.generator.stereotype.QueryWapper; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.util.ObjectUtils; import java.lang.reflect.Field; import static org.apache.commons.lang3.reflect.MethodUtils.invokeMethod; /** * @Description 拼接查询条件工具类 */ public class QueryWrapperUtil { /** * 拼接查询条件 * * @param queryWrapper 条件对象 * @param obj 数据实体 * @return void 返回参数说明 * @exception/throws */ public static void convertQuery(QueryWrapper queryWrapper, Object obj) { Class clazz = obj.getClass(); try { // 反射遍历属性 for (Field field : clazz.getDeclaredFields()) { // 获取属性名 String fieldname = field.getName(); // 抑制Java对修饰符的检查 field.setAccessible(true); // 获取属性值 Object fieldValue = field.get(obj); // String fieldValue = getFieldValue(obj ,field.getName()).toString(); // 查询注解 QueryWapper queryWapperAnnotation = AnnotationUtils.getAnnotation(field, QueryWapper.class); if(ObjectUtils.isEmpty(queryWapperAnnotation)){ continue; } String fieldName = queryWapperAnnotation.field(); // 获取枚举 QueryWapperEnum queryWapperEnum = queryWapperAnnotation.queryWapperEnum(); // 拼接查询条件 switch (queryWapperEnum) { case EQ: queryWrapper.eq(!ObjectUtils.isEmpty(fieldValue), fieldName, fieldValue); break; case LIKE: queryWrapper.like(!ObjectUtils.isEmpty(fieldValue), fieldName, fieldValue); break; default: break; } } } catch (IllegalAccessException e) { e.printStackTrace(); } } /** * 获取属性名 * * @exception/throws */ private static String getFieldValue(Object owner, String fieldName) { try { return invokeMethod(owner, fieldName, null).toString(); } catch (Exception e) { e.printStackTrace(); } return null; } }
Mapper类
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Mapper; import com.xx.xx.PersDTO; /** * <p> * Mapper 接口 * </p> * */ @Mapper public interface PersMapper extends BaseMapper<PersDTO> { }
实现类使用
Page<PersDTO> page = new Page(persBO.getCurrent(), persBO.getSize()); // 拼接查询条件 QueryWrapper<PersDTO> persDTOQueryWrapper = new QueryWrapper<>(); QueryWrapperUtil.convertQuery(persDTOQueryWrapper,persBO); IPage<PersDTO> iPage = persMapper.selectPage(page, persDTOQueryWrapper); page.setRecords(iPage.getRecords());
以上是关于记录mybatis-plus 根据自定义注解反射实现条件拼接查询的主要内容,如果未能解决你的问题,请参考以下文章