自定義注解動態拼接查詢條件
引入依賴
<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());