功能实现:
使用mybatisPlus 根据传入对象(查询条件)根据在对象中加入的注解查询条件 完成对传入对象的查询
主要使用方法:
1. mybatisPlus 构造器的使用 2. java反射机制的使用 3.java注解的实现
流程图展示:
1. 构造器实现的主要类:
package com.pig4cloud.pigx.common.data.util; import cn.hutool.core.util.ReflectUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.pig4cloud.pigx.common.data.annotation.DynamicQuery; import java.lang.reflect.Field; import java.lang.reflect.Method; public class CreateQuery { public static QueryWrapper getQueryWrapper(Object queryObj) { int len; String fieldName2; String first; Object endValue; Method endMethod; Class<?> clazz = queryObj.getClass(); //得到查询类的类 Field[] fields = clazz.getDeclaredFields();//获取类成员变量 QueryWrapper queryWrapper = new QueryWrapper(); for (Field field : fields) {//遍历所有的类成员变量 DynamicQuery query = field.getAnnotation(DynamicQuery.class); //得到成员变量上的注解 if (query == null) continue; Object queryValue = ReflectUtil.getFieldValue(queryObj, field); // 得到注解类的值 if (queryValue != null) { String dbField = query.dbField(); if (dbField.equals("")) { dbField = CamelUtil.underscoreName(field.getName()); //将驼峰命名法命名为数据库命名法 } switch (query.value()) { // case eq: queryWrapper.eq(dbField, queryValue); break; case ne: queryWrapper.ne(dbField, queryValue); break; case gt: queryWrapper.gt(dbField, queryValue); break; case ge: queryWrapper.ge(dbField, queryValue); break; case lt: queryWrapper.lt(dbField, queryValue); break; case le: queryWrapper.le(dbField, queryValue); break; case like: queryWrapper.like(dbField, queryValue); break; case notLike: queryWrapper.notLike(dbField, queryValue); break; case between: len = field.getName().length(); fieldName2 = field.getName().substring(0, len - 2); first = fieldName2.substring(0, 1); fieldName2 = fieldName2.substring(1, len - 2); fieldName2 = first.toUpperCase() + fieldName2 + "End"; endMethod = ReflectUtil.getMethod(queryObj.getClass(), fieldName2); try { endValue = endMethod.invoke(queryObj); } catch (Exception e) { e.printStackTrace(); continue; } if (endValue != null) queryWrapper.between(dbField, queryValue, endValue); break; case notBetween: len = field.getName().length(); fieldName2 = field.getName().substring(0, len - 2); first = fieldName2.substring(0, 1); fieldName2 = fieldName2.substring(1, len - 2); fieldName2 = first.toUpperCase() + fieldName2 + "End"; endMethod = ReflectUtil.getMethod(queryObj.getClass(), fieldName2); try { endValue = endMethod.invoke(queryObj); } catch (Exception e) { e.printStackTrace(); continue; } if (endValue != null) queryWrapper.notBetween(dbField, queryValue, endValue); break; case likeLeft: queryWrapper.likeLeft(dbField, queryValue); break; case likeRight: queryWrapper.likeRight(dbField, queryValue); break; case isNull: queryWrapper.isNull(dbField); break; case inNotNull: queryWrapper.isNotNull(dbField); break; case in: queryWrapper.in(dbField, queryValue); break; case notIn: queryWrapper.notIn(dbField, queryValue); break; default: break; } } } return queryWrapper; } }
2. 通过反射获得成员变量的值类实现:
3. 将驼峰命名字段转换为数据库db字段类实现: