| | |
| | | package com.moral.common.util; |
| | | |
| | | import com.moral.common.bean.PageBean; |
| | | import lombok.extern.log4j.Log4j; |
| | | import tk.mybatis.mapper.entity.Example; |
| | | import java.beans.PropertyDescriptor; |
| | |
| | | private final static String CRITERIA_SPLIT = "\\|\\|\\|"; |
| | | private final static String CONDITION_SPLIT = "\\|\\|"; |
| | | private static Map<String, Method> criteriaMethodMap = null; |
| | | private static void setOrderByClause(Example example,String orderByClause) throws UnsupportedEncodingException, NoSuchFieldException { |
| | | orderByClause = URLDecoder.decode(orderByClause,"UTF-8"); |
| | | String[] orderBys = orderByClause.split(CRITERIA_SPLIT); |
| | | for(String orderBy : orderBys){ |
| | | String[] items = orderBy.split(CONDITION_SPLIT); |
| | | if(items.length == 2){ |
| | | String property = items[0]; |
| | | String sort = items[1]; |
| | | if(isPropertyOfClass(example.getEntityClass(),property)){ |
| | | |
| | | public static Example generateExample(Class clzz, String params) { |
| | | Example example = new Example(clzz); |
| | | if("asc".equals(sort)){ |
| | | example.orderBy(property).asc(); |
| | | }else{ |
| | | example.orderBy(property).desc(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | public static Example generateExample(Class clzz, PageBean page) { |
| | | Example example = null; |
| | | try { |
| | | String params = page.getQueryParams(); |
| | | String orderByClause = page.getOrderByClause(); |
| | | example = new Example(clzz); |
| | | if(!StringUtils.isNullOrEmpty(orderByClause)){ |
| | | setOrderByClause(example,orderByClause); |
| | | } |
| | | if(StringUtils.isNullOrEmpty(params)){ return example;} |
| | | params = URLDecoder.decode(params, "UTF-8"); |
| | | if (!StringUtils.isNullOrEmpty(params) && params.startsWith("or|")) { |
| | | String[] criteria = params.trim().split(OR_SPLIT); |
| | | Example.Criteria criteriaOfExample = example.or(); |
| | | 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) { |