| | |
| | | import lombok.extern.log4j.Log4j; |
| | | import tk.mybatis.mapper.entity.Example; |
| | | |
| | | import javax.validation.constraints.NotNull; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.lang.reflect.Field; |
| | | import java.lang.reflect.InvocationTargetException; |
| | |
| | | } |
| | | if(!StringUtils.isNullOrEmpty(params)){ |
| | | params = URLDecoder.decode(params, "UTF-8"); |
| | | if (params.startsWith(OR)) { |
| | | String[] criteria = params.trim().split(OR_SPLIT); |
| | | for (String criterion : criteria) { |
| | | // criterion为null或""跳过 |
| | | if(StringUtils.isNullOrEmpty(criterion)) { continue;}; |
| | | Example.Criteria criteriaOfExample = example.or(); |
| | | if (!StringUtils.isNullOrEmpty(criterion)) { |
| | | String[] conditions = criterion.trim().split(CRITERIA_SPLIT); |
| | | for (String condition : conditions) { |
| | | if (!StringUtils.isNullOrEmpty(condition)) { |
| | | String[] conditionItems = condition.split(CONDITION_SPLIT); |
| | | if (conditionItems != null && conditionItems.length > 1) { |
| | | //方法名 |
| | | String methodName = conditionItems[0]; |
| | | //属性名 |
| | | String propertyName = conditionItems[1]; |
| | | //参数列表 |
| | | List values = new ArrayList<Object>(); |
| | | if (conditionItems.length > 2) { |
| | | for (int index = 2; index < conditionItems.length; index++) { |
| | | values.add(conditionItems[index]); |
| | | } |
| | | } |
| | | Method method = getExampleMethod(methodName,values.size()+1); |
| | | if (method != null && (methodName.equals("andCondition")||isPropertyOfClass(clazz, propertyName))) { |
| | | invokeMethod(criteriaOfExample,method,propertyName,values); |
| | | } |
| | | |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | loadCriterias(example,params); |
| | | } |
| | | if(!isDelete) { |
| | | addDeletesToExample(example,clazz); |
| | |
| | | } |
| | | return example; |
| | | } |
| | | |
| | | private static void invokeMethod(Object obj, Method method,String propertyName,List values) throws InvocationTargetException, IllegalAccessException { |
| | | int len = values.size(); |
| | | switch (len) { |
| | | case 0: |
| | | if(method.getParameterCount()==1) { |
| | | method.invoke(obj, propertyName); |
| | | } |
| | | break; |
| | | case 1: |
| | | |
| | | if(method.getParameterCount()==2){ |
| | | method.invoke(obj,propertyName,values.get(0)); |
| | | } |
| | | break; |
| | | //between 查询 |
| | | case 2: |
| | | if(method.getParameterCount()==3){ |
| | | method.invoke(obj,propertyName,values.toArray()); |
| | | } |
| | | break; |
| | | //in 查询 |
| | | default: |
| | | if(method.getParameterCount()==2){ |
| | | method.invoke(obj,propertyName,values); |
| | | } |
| | | break; |
| | | private static void loadCriterias(Example example,String params) throws InvocationTargetException, IllegalAccessException { |
| | | if (params.startsWith(OR)) { |
| | | String[] criteria = params.trim().split(OR_SPLIT); |
| | | for (String criterion : criteria) { |
| | | // criterion为null或""跳过 |
| | | if(StringUtils.isNullOrEmpty(criterion)) { continue;}; |
| | | Example.Criteria criteriaOfExample = example.or(); |
| | | loadConditions(example,criteriaOfExample,criterion); |
| | | } |
| | | } |
| | | } |
| | | private static void loadConditions(Example example,Example.Criteria criteriaOfExample,String criterion) throws InvocationTargetException, IllegalAccessException { |
| | | String[] conditions = criterion.trim().split(CRITERIA_SPLIT); |
| | | for (String condition : conditions) { |
| | | if (!StringUtils.isNullOrEmpty(condition)) { |
| | | String[] conditionItems = condition.split(CONDITION_SPLIT); |
| | | if (conditionItems != null && conditionItems.length > 1) { |
| | | //方法名 |
| | | String methodName = conditionItems[0]; |
| | | //属性名 |
| | | String propertyName = conditionItems[1]; |
| | | //参数列表 |
| | | List values = new ArrayList<Object>(); |
| | | if (conditionItems.length > 2) { |
| | | for (int index = 2; index < conditionItems.length; index++) { |
| | | values.add(conditionItems[index]); |
| | | } |
| | | } |
| | | Method method = getExampleMethod(methodName,values.size()+1); |
| | | if (method != null && (methodName.equals("andCondition")||isPropertyOfClass(example.getEntityClass(), propertyName))) { |
| | | invokeMethod(criteriaOfExample,method,propertyName,values); |
| | | } else { |
| | | log.warn(condition+" can't be executed as condition"); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | private static boolean validatetParameters(@NotNull Method method,int len) { |
| | | //前端传递propertyName参数为string类型 |
| | | return method.getParameterCount() == (len+1) && method.getParameterTypes()[0].equals(String.class); |
| | | } |
| | | private static void invokeMethod(@NotNull Object obj,@NotNull Method method,@NotNull String propertyName,@NotNull List values) throws InvocationTargetException, IllegalAccessException { |
| | | int len = values.size(); |
| | | switch (len) { |
| | | case 0: |
| | | method.invoke(obj, propertyName); |
| | | break; |
| | | case 1: |
| | | Object value = values.get(0) == "null" ? null : values.get(0); |
| | | method.invoke(obj, propertyName,value); |
| | | break; |
| | | //between 查询 |
| | | case 2: |
| | | method.invoke(obj, propertyName, values.toArray()); |
| | | break; |
| | | //in 查询 |
| | | default: |
| | | method.invoke(obj, propertyName, values); |
| | | break; |
| | | } |
| | | } |
| | | |
| | | ; |
| | |
| | | } |
| | | } |
| | | } |
| | | if(methodTemp==null || validatetParameters(methodTemp,paramCount)){ |
| | | return null; |
| | | } |
| | | return methodTemp; |
| | | } |
| | | } |