/**
* 查询参数工具类
*
* @author Nemo
*
*/
public class QueryParamUtils {
private static final Logger log = LoggerFactory.getLogger(QueryParamUtils.class);
/**
*
*
* @param queryBean
* @param exampleAutoGenClz
*/
public static void copyParamsToExample(BaseQueryBean queryBean, Object exampleObj) {
try {
Field[] queryFields = ReflectionUtils.getAllDeclaredFields(queryBean.getClass(), BaseQueryBean.class);
if (queryFields.length == 0) {
return;
}
Object criteriaObj = exampleObj.getClass().getMethod("createCriteria").invoke(exampleObj);
for (Field f : queryFields) {
QueryBind qt = f.getAnnotation(QueryBind.class);
if ((qt != null && exampleObj.getClass().equals(qt.exampleClazz()))
|| (qt == null || Void.class.equals(qt.exampleClazz()))) {
invokeCriteriaMethod(criteriaObj, f, queryBean);
}
}
} catch (Exception e) {
log.error("Exception:", e);
}
}
/**
* 把从前端获取到的查询条件,赋值到对应的自动生成example class,以便重用mbg自动生成的mapping文件,减少where查询条件的手写
*
* @param queryBean
* @param exampleAutoGenClz mbg自动生成的Example class
*/
public static void copyParamsToExampleClz(BaseQueryBean queryBean, Object exampleAutoGenClz) {
Field[] queryFields = ReflectionUtils.getAllDeclaredFields(queryBean.getClass(), BaseQueryBean.class);
if (queryFields.length == 0) {
return;
}
try {
Object criteriaObj = exampleAutoGenClz.getClass().getMethod("createCriteria").invoke(exampleAutoGenClz);
setOrderPageParams(queryBean, exampleAutoGenClz);
for (Field f : queryFields) {
invokeCriteriaMethod(criteriaObj, f, queryBean);
}
} catch (Exception e) {
log.error("Exception:", e);
}
}
/**
* 传递分页及排序参数
*
* @param queryBean
* @param exampleObj
*/
private static void setOrderPageParams(BaseQueryBean queryBean, Object exampleObj) {
try {
exampleObj.getClass().getMethod("setOrderByClause", String.class).invoke(exampleObj, queryBean.getOrderBy());
if (queryBean.isPagable()) {
exampleObj.getClass().getMethod("setPageSize", int.class).invoke(exampleObj, queryBean.getPageSize());
exampleObj.getClass().getMethod("setStartIndex", int.class).invoke(exampleObj, queryBean.getStartIndex());
}
} catch (Exception e) {
log.error("Exception:", e);
}
}
/**
* 把QueryBean从前端获取到的查询条件,赋值到属性为mbg各种自动生成的example class,以便重用mbg自动生成的mapping文件,减少where查询条件的手写
*
* @param queryBean
* @param exampleWrapper
*/
public static void copyParamsToExampleWrapper(BaseQueryBean queryBean, ExampleWrapper exampleWrapper) {
exampleWrapper.setOrderBy(queryBean.getOrderBy());
exampleWrapper.setPageSize(queryBean.getPageSize());
exampleWrapper.setStartIndex(queryBean.getStartIndex());
Field[] wrapperFields = ReflectionUtils.getAllDeclaredFields(exampleWrapper.getClass());
Field[] queryFields = ReflectionUtils.getAllDeclaredFields(queryBean.getClass(), BaseQueryBean.class);
if (queryFields.length == 0 || wrapperFields.length == 0) {
return;
}
Map<Class<?>, Object> innerExampleCriteriaObjMap = new HashMap<>();
Map<Class<?>, Object> innerExampleExampleObjMap = new HashMap<>();
Map<Class<?>, String> innerExampleObjFieldMap = new HashMap<>();
try {
// exampleWrapper中包含的自动生成的example class
for (Field f : wrapperFields) {
f.setAccessible(true);
if (f.getType().getName().endsWith("Example")) {
Object innerExampleObj = f.getType().newInstance();
Object criteriaObj = innerExampleObj.getClass().getMethod("createCriteria").invoke(innerExampleObj);
innerExampleCriteriaObjMap.put(f.getType(), criteriaObj);
innerExampleExampleObjMap.put(f.getType(), innerExampleObj);
innerExampleObjFieldMap.put(f.getType(), f.getName());
}
}
Set<Class<?>> criteriaClassSet = new HashSet<>(); // 保存此次查询中查询条件不为空的example class
// 解析queryBean中的查询条件,并调用mbg自动生成的example class中的查询条件
for (Field f : queryFields) {
QueryBind qt = f.getAnnotation(QueryBind.class);
if (qt == null) {
continue;
}
Class<?> exampleClazz = qt.exampleClazz();
if (innerExampleCriteriaObjMap.containsKey(exampleClazz)) {
Object obj = innerExampleCriteriaObjMap.get(exampleClazz);
boolean hasQuery = invokeCriteriaMethod(obj, f, queryBean);
if (hasQuery) {
criteriaClassSet.add(exampleClazz);
}
}
}
// 去除此次查询中查询条件为空的example obj, 以便后台可完成动态查询
Iterator<Class<?>> it = innerExampleCriteriaObjMap.keySet().iterator();
while (it.hasNext()) {
Class<?> c = it.next();
if (!criteriaClassSet.contains(c)) {
it.remove();
}
}
for (Class<?> exampleClassKey : innerExampleCriteriaObjMap.keySet()) {
Method setterMethod =
exampleWrapper.getClass().getMethod(
"set" + StringUtils.capitalize(innerExampleObjFieldMap.get(exampleClassKey)), exampleClassKey);
log.debug("setterMethod:{}", setterMethod);
if (setterMethod != null) {
setterMethod.invoke(exampleWrapper, innerExampleExampleObjMap.get(exampleClassKey));
}
}
} catch (Exception e) {
log.error("Exception:", e);
}
}
/**
*
* @param criteriaObj
* @param f
* @param queryBean
*/
private static boolean invokeCriteriaMethod(Object criteriaObj, Field f, Object queryBean) {
f.setAccessible(true);
QueryBind qt = f.getAnnotation(QueryBind.class);
// 此域不做为查询条件拼装到sql
if (null != qt && QueryBindType.NEEDLESS.equals(qt.type())) {
return false;
}
StringBuilder criteriaMethodName = new StringBuilder();
criteriaMethodName.append("and");
if (qt == null || StringUtils.isEmpty(qt.property())) {
criteriaMethodName.append(StringUtils.capitalize(f.getName()));
} else {
criteriaMethodName.append(StringUtils.capitalize(qt.property()));
}
try {
Object fieldValue = f.get(queryBean);
// 若ExampleQuery实现类中属性类型为List
boolean shouldInvoke = false;
if (List.class.isAssignableFrom(f.getType())) {
criteriaMethodName.append("In");
} else if (fieldValue instanceof String && StringUtils.isBlank((String)fieldValue)) {
fieldValue = null;
} else if (fieldValue != null) {
if (qt == null || qt.type() == null || QueryBindType.EQ.equals(qt.type())) {
criteriaMethodName.append("EqualTo");
} else if (QueryBindType.LIKE.equals(qt.type())) {
criteriaMethodName.append("Like");
fieldValue = "%" + fieldValue + "%";
} else if (QueryBindType.GT.equals(qt.type())) {
criteriaMethodName.append("GreatThan");
} else if (QueryBindType.GTE.equals(qt.type())) {
criteriaMethodName.append("GreaterThanOrEqualTo");
} else if (QueryBindType.LT.equals(qt.type())) {
criteriaMethodName.append("LessThan");
} else if (QueryBindType.LTE.equals(qt.type())) {
criteriaMethodName.append("LessThanOrEqualTo");
} else if (QueryBindType.NEQ.equals(qt.type())) {
criteriaMethodName.append("NotEqualTo");
} else if (QueryBindType.IN.equals(qt.type())) {
criteriaMethodName.append("In");
} else if (QueryBindType.NOT_IN.equals(qt.type())) {
criteriaMethodName.append("NotIn");
}
}
if (qt != null) {
if (QueryBindType.IS_NOT_NULL.equals(qt.type())) {
criteriaMethodName.append("IsNotNull");
fieldValue = null;
shouldInvoke = true;
} else if (QueryBindType.IS_NULL.equals(qt.type())) {
criteriaMethodName.append("IsNull");
fieldValue = null;
shouldInvoke = true;
}
}
// invoke the criteria method of auto-generated example class
if (fieldValue == null && shouldInvoke) {
Method criteriaMethod = criteriaObj.getClass().getMethod(criteriaMethodName.toString());
if (criteriaMethod != null) {
criteriaMethod.invoke(criteriaObj);
return true;
}
} else if (fieldValue != null) {
Method criteriaMethod =
criteriaObj.getClass().getMethod(criteriaMethodName.toString(), ClassUtils.primitiveToWrapper(f.getType()));
log.debug("criteriaMethod:{}", criteriaMethod);
if (criteriaMethod != null) {
criteriaMethod.invoke(criteriaObj, fieldValue);
return true;
}
}
} catch (Exception e) {
log.error("Exception:", e);
}
return false;
}