mybait-plus实现动态自定义查询条件

实现一个list接口满足大部分的前端查询要求

1、创建一个枚举

/*** 自定义查询的枚举*/
public enum WhereEnum {EQ(1),// =NE(2),//<>LIKE(3),// likt %%NOT_LIKE(4), //not like %%GT(5), // >GE(6),// >=LT(7),// <LE(8),// <=LIKE_LEFT(9), // like %xLIKE_RIGHT(10),// like x%IS_NULL(11),// xx is nullIS_NOT_NULL(12),// xx is not nullIN(13),// in ()NOT_IN(14),// not in ()ORDER_BY_ASE(15),// order by ascORDER_BY_DESC(16),// order by descSELECT(17), // select xx ,xx 只查询某些字段;private final int value;WhereEnum(int value) {this.value = value;}public int value() {return this.value;}}

2、创建注解

import org.springframework.stereotype.Indexed;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface Where {/*** 字段名* @return*/String field() default "";/***  字段查询的类型* @return*/WhereEnum whereEnum() default WhereEnum.EQ ;}

3、创建vo类

@Data
public class SupervisionWhere {@Where(field = "id")private String id;/*** 如果是自定义查询语句,连表查询,可以直接加前缀*/@Where(field = "a.TITLE",whereEnum = WhereEnum.LIKE)private String title;@Where(field = "END_DATE",whereEnum = WhereEnum.GE)private Date startDate;@Where(field = "END_DATE",whereEnum = WhereEnum.LE)private Date endDate;@Where(whereEnum = WhereEnum.ORDER_BY_ASE)private String orderByAsc;@Where(whereEnum = WhereEnum.ORDER_BY_DESC)private String orderByDesc;private Integer pageNo;/*** 默认每页20条*/private Integer pageSize = 20;}

4、创建工具类

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ObjectUtils;
import java.lang.reflect.Field;
import java.util.List;/*** 动态自定义查询条件工具类*/
public class WhereUtil {/*** 拼接查询条件** @param queryWrapper 条件对象* @param obj          数据实体* @return void 返回参数说明* @exception/throws*/public static void convertQuery(QueryWrapper queryWrapper, Object obj) throws IllegalAccessException {Class clazz = obj.getClass();// 反射遍历属性for (Field field : clazz.getDeclaredFields()) {// 抑制Java对修饰符的检查field.setAccessible(true);// 获取属性值Object fieldValue =  field.get(obj);// 如果是null的跳过if (ObjectUtils.isEmpty(fieldValue)) {continue;}// 判断是不是空字符串if (fieldValue instanceof String) {if (StringUtils.isEmpty(fieldValue.toString().trim())) {continue;}}// 查询注解Where whereAnnotation = AnnotationUtils.getAnnotation(field, Where.class);if(ObjectUtils.isEmpty(whereAnnotation)){continue;}// 获取字段名String fieldName = whereAnnotation.field();// 获取枚举WhereEnum whereEnum = whereAnnotation.whereEnum();// 拼接查询条件switch (whereEnum) {case EQ:queryWrapper.eq(fieldName, fieldValue);break;case NE:queryWrapper.ne(fieldName, fieldValue);break;case LIKE:queryWrapper.like(fieldName, fieldValue);break;case NOT_LIKE:queryWrapper.notLike(fieldName, fieldValue);break;case GT:queryWrapper.gt(fieldName, fieldValue);break;case GE:queryWrapper.ge(fieldName, fieldValue);break;case LT:queryWrapper.lt(fieldName, fieldValue);break;case LE:queryWrapper.le(fieldName, fieldValue);break;case LIKE_LEFT:queryWrapper.likeLeft(fieldName, fieldValue);break;case LIKE_RIGHT:queryWrapper.likeRight(fieldName, fieldValue);break;case IS_NULL:queryWrapper.isNull(fieldValue);break;case IS_NOT_NULL:queryWrapper.isNotNull(fieldValue);break;case IN:queryWrapper.in(fieldName, (List)fieldValue);break;case NOT_IN:queryWrapper.notIn(fieldName, (List)fieldValue);break;case ORDER_BY_ASE:queryWrapper.orderByAsc(fieldValue);break;case ORDER_BY_DESC:queryWrapper.orderByDesc(fieldValue);break;case SELECT:if (fieldValue instanceof String) {queryWrapper.select(((String) fieldValue).split(","));}break;default:break;}}}
}

5、接口调用

@GetMapping(value = "/supervision")public Result<?> list(SupervisionWhere supervision) throws IllegalAccessException {QueryWrapper<Supervision> queryWrapper = new QueryWrapper<>();// 拼接查询条件WhereUtil.convertQuery(queryWrapper,supervision);// 如果有页码,则进行分页查询,默认每页 20 条,可传参修改if (supervision.getPageNo() != null ){Page<Supervision> page = new Page(supervision.getPageNo(), supervision.getPageSize());return Result.OK(service.page(page, queryWrapper));}// 没有侧进行普通查询return Result.OK(service.list(queryWrapper));}

这样就可以了

mybait-plus实现动态自定义查询条件相关推荐

  1. java自定义sql查询条件_mybatis-plus QueryWrapper自定义查询条件的实现

    mybatis-plus框架功能很强大,把很多功能都集成了,比如自动生成代码结构,mybatis crud封装,分页,动态数据源等等,附上官网链接https://mp.baomidou.com/,gi ...

  2. LINQ to SQL 运行时动态构建查询条件

    原文地址:http://msdn.microsoft.com/zh-cn/dd567295.aspx 在进行数据查询时,经常碰到需要动态构建查询条件.使用LINQ实现这个需求可能会比以前拼接SQL语句 ...

  3. oracle数据库动态拼接查询条件解决方案

    在项目中遇到需要动态拼接查询条件的需求,现将解决方案列于下. 一. select * from table t where ('$(param)' is null or t.filed = '$(pa ...

  4. java中动态查询条件,Java实现动态添加查询条件

    今天遇到一个问题,就是需要根据前端页面发送的条件查询数据库记录,但是前端发送的条件是不确定的.如果使用mybatis的xml方法可以使用if标签灵活的添加判断条件,但是现在我使用的就是单纯的sql. ...

  5. JEECG中datagrid方法自定义查询条件

    自定义加添加查询条件的用法: CriteriaQuery cq = new CriteriaQuery(EquipmentEntity.class, dataGrid); //查询条件组装器 org. ...

  6. mybatis循环Map动态添加查询条件

    使用Map存储键值对,用于动态的拼接where条件后的 列和值 dao层接口: public List<Map<String,Object>> getData(@Param(& ...

  7. sap abap中动态指定查询条件

    有时候,我们事先并不知道where 后面要跟什么东西,只有在运行中才能确定,这就有点象其它语言中拼凑sql语句一样,abap也是支持的,用起来也很方便. data:c_cond(100) type c ...

  8. layui自定义查询条件html页面,Layui的数据表格+springmvc实现搜索功能的例子_飛雲_前端开发者...

    如下所示: 主要在前端页面加: 搜索ID: userid content 搜索 在 reload:function () { var keyWord=$("#keyWord").v ...

  9. mongorepository查询条件_MongoDB动态条件之分页查询

    一.使用QueryByExampleExecutor 1. 继承MongoRepositorypublic interface StudentRepository extends MongoRepos ...

  10. php是根据html中的值查询数据条件_FleaPHP框架数据库查询条件($conditions)写法总结...

    本文实例讲述了FleaPHP框架数据库查询条件($conditions)写法.分享给大家供大家参考,具体如下: 在FleaPHP中,凡是用到数据库查询的函数,都需要查询条件参数$conditions, ...

最新文章

  1. SAP SD 以PDF格式显示BILLING的输出格式
  2. win7编程接口的一些变化
  3. 关于Visual C++ 2008中wprintf和wprintf_s输出中文乱码问题
  4. 《NodeJS开发指南》第五章微博实例开发总结
  5. 【Android NDK 开发】Kotlin 语言中使用 NDK ( 创建支持 Kotlin 的 NDK 项目 | Kotlin 语言中使用 NDK 要点 | 代码示例 )
  6. 很多人调用接口会阻塞吗_锻炼的真相你知道吗?这些你可能都想了解,很多人都会问的2点...
  7. 7th思妙想 Fun事连连,今天范式7岁啦!
  8. 竞赛发布|100万奖金寻DT时代“最强大脑”!
  9. centos7 禁止ip访问_centos7.6版本限制某个IP访问指定端口
  10. 程序员为什么老得快_这段 Python 代码让程序员赚 300W,公司已确认!网友:神操作!...
  11. geoserver三维_集团公司自主研发三维GIS平台产品——GeniusWorld 2.0 C版本发布
  12. HTML+JS好例子集锦
  13. 定位首款弹幕K歌软件 阿里鲸鸣未来究竟能够走多远?
  14. 搭建微信订阅号后台服务
  15. html网页肯德基设计代码作业,Illustrator设计一幅肯德基广告单页制作教程
  16. 用html编写一个红绿灯,红绿灯.html
  17. Virut.ce-感染型病毒分析报告
  18. AI推理服务平台升级,阿里云机器学习PAI推出新规格
  19. 中兴校招c语言在线笔试题,中兴2017校招软件在线笔试题
  20. Word2010如何从正文开始设置页眉页码?

热门文章

  1. p标签里文字不换行,以省略号结束
  2. 算法学习:最小生成树
  3. c语言 树的遍历,c语言构造树及树的三种遍历
  4. centos下mysql备份数据库命令_[CentOS]下mysql数据库常用命令总结
  5. OpenCV-图像处理(32、点多边形测试)
  6. 科罗拉多州立大学计算机科学专业,科罗拉多州立大学有哪些专业_专业排名(QS世界排名)...
  7. 计算机二级考试c语言 上机,计算机等级考试二级C语言上机题[2]
  8. 用计算机进行实时自动采集,《大学计算机基础》基础部分练习题_附件1
  9. java nio 下载网页_JavaNIO 下载网络文件保存本地报java.nio.file.AccessDeniedException:无权限操作...
  10. 小程序marker 气泡怎么用_小程序直播怎么用,看这里!