主要是dao查询接口的封装看不明白,由于没有 文档package com.htsoft.core.command;import org.hibernate.Criteria;public interface CriteriaCommand {
public static final String SORT_DESC = "desc";
public static final String SORT_ASC = "asc"; public Criteria execute(Criteria paramCriteria);
}
package com.htsoft.core.command;import org.hibernate.Criteria;
import org.hibernate.criterion.Example;public class ExampleCommandImpl implements CriteriaCommand {
private Object pojoExample = null; public void setPojoExample(Object pojoEx) {
this.pojoExample = pojoEx;
} public ExampleCommandImpl(Object pojoExample) {
this.pojoExample = pojoExample;
} public Criteria execute(Criteria criteria) {
if (this.pojoExample != null) {
Example exampleRestriction = Example.create(this.pojoExample);
criteria.add(exampleRestriction);
}
return criteria;
}
}package com.htsoft.core.command;import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;
/**
* 对HQL语句中传进来的参数进行判断
* @author Administrator
*
*/
public class FieldCommandImpl implements CriteriaCommand {
private static Log logger = LogFactory.getLog(CriteriaCommand.class);
private String property;
private Object value;
private String operation;
private QueryFilter filter; public FieldCommandImpl(String property, Object value, String operation,
QueryFilter filter) {
this.property = property;
this.value = value;
this.operation = operation;
this.filter = filter;
} public String getProperty() {
return this.property;
} public void setProperty(String property) {
this.property = property;
} public Object getValue() {
return this.value;
} public void setValue(Object value) {
this.value = value;
} public String getOperation() {
return this.operation;
} public void setOperation(String operation) {
this.operation = operation;
} public Criteria execute(Criteria criteria) {
String[] propertys = this.property.split("[.]"); if ((propertys != null) && (propertys.length > 1)
&& (!"vo".equals(propertys[0]))) {
for (int i = 0; i < propertys.length - 1; ++i) {
if (!this.filter.getAliasSet().contains(propertys[i])) {
criteria.createAlias(propertys[i], propertys[i]);
this.filter.getAliasSet().add(propertys[i]);
}
} } if ("LT".equals(this.operation))
criteria.add(Restrictions.lt(this.property, this.value));
else if ("GT".equals(this.operation))
criteria.add(Restrictions.gt(this.property, this.value));
else if ("LE".equals(this.operation))
criteria.add(Restrictions.le(this.property, this.value));
else if ("GE".equals(this.operation))
criteria.add(Restrictions.ge(this.property, this.value));
else if ("LK".equals(this.operation))
criteria.add(Restrictions.like(this.property,
"%" + this.value + "%").ignoreCase());
else if ("LFK".equals(this.operation))
criteria.add(Restrictions.like(this.property, this.value + "%")
.ignoreCase());
else if ("RHK".equals(this.operation))
criteria.add(Restrictions.like(this.property, "%" + this.value)
.ignoreCase());
else if ("NULL".equals(this.operation))
criteria.add(Restrictions.isNull(this.property));
else if ("NOTNULL".equals(this.operation))
criteria.add(Restrictions.isNotNull(this.property));
else if ("EMP".equals(this.operation))
criteria.add(Restrictions.isEmpty(this.property));
else if ("NOTEMP".equals(this.operation))
criteria.add(Restrictions.isNotEmpty(this.property));
else if ("NEQ".equals(this.operation))
criteria.add(Restrictions.ne(this.property, this.value));
else {
criteria.add(Restrictions.eq(this.property, this.value));
} return criteria;
} public String getPartHql() {
String[] propertys = this.property.split("[.]");
if ((propertys != null) && (propertys.length > 1)
&& (!"vo".equals(propertys[0]))
&& (!this.filter.getAliasSet().contains(propertys[0]))) {
this.filter.getAliasSet().add(propertys[0]);
} String partHql = "";
//小于
if ("LT".equals(this.operation)) {
partHql = this.property + " < ? ";
this.filter.getParamValueList().add(this.value);
//大于
} else if ("GT".equals(this.operation)) {
partHql = this.property + " > ? ";
this.filter.getParamValueList().add(this.value);
} else if ("LE".equals(this.operation)) {
partHql = this.property + " <= ? ";
this.filter.getParamValueList().add(this.value);
} else if ("GE".equals(this.operation)) {
partHql = this.property + " >= ? ";
this.filter.getParamValueList().add(this.value);
} else if ("LK".equals(this.operation)) {
partHql = this.property + " like ? ";
this.filter.getParamValueList().add(
"%" + this.value.toString() + "%");
} else if ("LFK".equals(this.operation)) {
partHql = this.property + " like ? ";
this.filter.getParamValueList().add(this.value.toString() + "%");
} else if ("RHK".equals(this.operation)) {
partHql = this.property + " like ? ";
this.filter.getParamValueList().add("%" + this.value.toString());
} else if ("NULL".equals(this.operation)) {
partHql = this.property + " is null ";
} else if ("NOTNULL".equals(this.operation)) {
partHql = this.property + " is not null ";
} else if ((!"EMP".equals(this.operation))
&& (!"NOTEMP".equals(this.operation))) {
if ("NEQ".equals(this.operation)) {
partHql = this.property + " !=? ";
this.filter.getParamValueList().add(this.value);
} else {
partHql = partHql + this.property + " =? ";
this.filter.getParamValueList().add(this.value);
}
}
return partHql;
}
}
public static final String SORT_DESC = "desc";
public static final String SORT_ASC = "asc"; public Criteria execute(Criteria paramCriteria);
}
package com.htsoft.core.command;import org.hibernate.Criteria;
import org.hibernate.criterion.Example;public class ExampleCommandImpl implements CriteriaCommand {
private Object pojoExample = null; public void setPojoExample(Object pojoEx) {
this.pojoExample = pojoEx;
} public ExampleCommandImpl(Object pojoExample) {
this.pojoExample = pojoExample;
} public Criteria execute(Criteria criteria) {
if (this.pojoExample != null) {
Example exampleRestriction = Example.create(this.pojoExample);
criteria.add(exampleRestriction);
}
return criteria;
}
}package com.htsoft.core.command;import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;
/**
* 对HQL语句中传进来的参数进行判断
* @author Administrator
*
*/
public class FieldCommandImpl implements CriteriaCommand {
private static Log logger = LogFactory.getLog(CriteriaCommand.class);
private String property;
private Object value;
private String operation;
private QueryFilter filter; public FieldCommandImpl(String property, Object value, String operation,
QueryFilter filter) {
this.property = property;
this.value = value;
this.operation = operation;
this.filter = filter;
} public String getProperty() {
return this.property;
} public void setProperty(String property) {
this.property = property;
} public Object getValue() {
return this.value;
} public void setValue(Object value) {
this.value = value;
} public String getOperation() {
return this.operation;
} public void setOperation(String operation) {
this.operation = operation;
} public Criteria execute(Criteria criteria) {
String[] propertys = this.property.split("[.]"); if ((propertys != null) && (propertys.length > 1)
&& (!"vo".equals(propertys[0]))) {
for (int i = 0; i < propertys.length - 1; ++i) {
if (!this.filter.getAliasSet().contains(propertys[i])) {
criteria.createAlias(propertys[i], propertys[i]);
this.filter.getAliasSet().add(propertys[i]);
}
} } if ("LT".equals(this.operation))
criteria.add(Restrictions.lt(this.property, this.value));
else if ("GT".equals(this.operation))
criteria.add(Restrictions.gt(this.property, this.value));
else if ("LE".equals(this.operation))
criteria.add(Restrictions.le(this.property, this.value));
else if ("GE".equals(this.operation))
criteria.add(Restrictions.ge(this.property, this.value));
else if ("LK".equals(this.operation))
criteria.add(Restrictions.like(this.property,
"%" + this.value + "%").ignoreCase());
else if ("LFK".equals(this.operation))
criteria.add(Restrictions.like(this.property, this.value + "%")
.ignoreCase());
else if ("RHK".equals(this.operation))
criteria.add(Restrictions.like(this.property, "%" + this.value)
.ignoreCase());
else if ("NULL".equals(this.operation))
criteria.add(Restrictions.isNull(this.property));
else if ("NOTNULL".equals(this.operation))
criteria.add(Restrictions.isNotNull(this.property));
else if ("EMP".equals(this.operation))
criteria.add(Restrictions.isEmpty(this.property));
else if ("NOTEMP".equals(this.operation))
criteria.add(Restrictions.isNotEmpty(this.property));
else if ("NEQ".equals(this.operation))
criteria.add(Restrictions.ne(this.property, this.value));
else {
criteria.add(Restrictions.eq(this.property, this.value));
} return criteria;
} public String getPartHql() {
String[] propertys = this.property.split("[.]");
if ((propertys != null) && (propertys.length > 1)
&& (!"vo".equals(propertys[0]))
&& (!this.filter.getAliasSet().contains(propertys[0]))) {
this.filter.getAliasSet().add(propertys[0]);
} String partHql = "";
//小于
if ("LT".equals(this.operation)) {
partHql = this.property + " < ? ";
this.filter.getParamValueList().add(this.value);
//大于
} else if ("GT".equals(this.operation)) {
partHql = this.property + " > ? ";
this.filter.getParamValueList().add(this.value);
} else if ("LE".equals(this.operation)) {
partHql = this.property + " <= ? ";
this.filter.getParamValueList().add(this.value);
} else if ("GE".equals(this.operation)) {
partHql = this.property + " >= ? ";
this.filter.getParamValueList().add(this.value);
} else if ("LK".equals(this.operation)) {
partHql = this.property + " like ? ";
this.filter.getParamValueList().add(
"%" + this.value.toString() + "%");
} else if ("LFK".equals(this.operation)) {
partHql = this.property + " like ? ";
this.filter.getParamValueList().add(this.value.toString() + "%");
} else if ("RHK".equals(this.operation)) {
partHql = this.property + " like ? ";
this.filter.getParamValueList().add("%" + this.value.toString());
} else if ("NULL".equals(this.operation)) {
partHql = this.property + " is null ";
} else if ("NOTNULL".equals(this.operation)) {
partHql = this.property + " is not null ";
} else if ((!"EMP".equals(this.operation))
&& (!"NOTEMP".equals(this.operation))) {
if ("NEQ".equals(this.operation)) {
partHql = this.property + " !=? ";
this.filter.getParamValueList().add(this.value);
} else {
partHql = partHql + this.property + " =? ";
this.filter.getParamValueList().add(this.value);
}
}
return partHql;
}
}
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;public class SortCommandImpl implements CriteriaCommand {
private String sortName;
private String ascDesc;
private QueryFilter filter; public Criteria execute(Criteria criteria) {
String[] propertys = this.sortName.split("[.]");
if ((propertys != null) && (propertys.length > 1)) {
for (int i = 0; i < propertys.length - 1; ++i) {
if (!this.filter.getAliasSet().contains(propertys[i])) {
criteria.createAlias(propertys[i], propertys[i]);
this.filter.getAliasSet().add(propertys[i]);
}
}
}
if ("desc".equalsIgnoreCase(this.ascDesc))
criteria.addOrder(Order.desc(this.sortName));
else if ("asc".equalsIgnoreCase(this.ascDesc)) {
criteria.addOrder(Order.asc(this.sortName));
}
return criteria;
} public SortCommandImpl(String sortName, String ascDesc, QueryFilter filter) {
this.sortName = sortName;
this.ascDesc = ascDesc;
this.filter = filter;
} public String getSortName() {
return this.sortName;
} public void setSortName(String sortName) {
this.sortName = sortName;
} public String getAscDesc() {
return this.ascDesc;
} public void setAscDesc(String ascDesc) {
this.ascDesc = ascDesc;
} public int hashCode() {
return new HashCodeBuilder(-82280557, -700257973).append(this.sortName)
.append(this.ascDesc).toHashCode();
} public String getPartHql() {
return this.sortName + " " + this.ascDesc;
}
}package com.htsoft.core.command;import com.htsoft.core.util.ParamUtil;
import com.htsoft.core.web.paging.PagingBean;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;public class QueryFilter {
public static final Log logger = LogFactory.getLog(QueryFilter.class);
public static final String ORDER_DESC = "desc";
public static final String ORDER_ASC = "asc";
private HttpServletRequest request = null; private String filterName = null; private List<Object> paramValues = new ArrayList(); private List<CriteriaCommand> commands = new ArrayList(); private Set<String> aliasSet = new HashSet(); private PagingBean pagingBean = null; public String getFilterName() {
return this.filterName;
} public void setFilterName(String filterName) {
this.filterName = filterName;
} public PagingBean getPagingBean() {
return this.pagingBean;
} public QueryFilter(HttpServletRequest request) {
this.request = request;
Enumeration paramEnu = request.getParameterNames();
while (paramEnu.hasMoreElements()) {
String paramName = (String) paramEnu.nextElement(); if (paramName.startsWith("Q_")) {
String paramValue = request.getParameter(paramName);
addFilter(paramName, paramValue);
}
} Integer start = Integer.valueOf(0);
Integer limit = PagingBean.DEFAULT_PAGE_SIZE; String s_start = request.getParameter("start");
String s_limit = request.getParameter("limit");
if (StringUtils.isNotEmpty(s_start)) {
start = new Integer(s_start);
}
if (StringUtils.isNotEmpty(s_limit)) {
limit = new Integer(s_limit);
} String sort = request.getParameter("sort");
String dir = request.getParameter("dir"); if ((StringUtils.isNotEmpty(sort)) && (StringUtils.isNotEmpty(dir))) {
addSorted(sort, dir);
} this.pagingBean = new PagingBean(start.intValue(), limit.intValue());
} public void addFilter(String paramName, String paramValue) {
String[] fieldInfo = paramName.split("[_]"); Object value = null;
if ((fieldInfo != null) && (fieldInfo.length == 4)) {
value = ParamUtil.convertObject(fieldInfo[2], paramValue);
if (value != null) {
FieldCommandImpl fieldCommand = new FieldCommandImpl(
fieldInfo[1], value, fieldInfo[3], this);
this.commands.add(fieldCommand);
}
} else if ((fieldInfo != null) && (fieldInfo.length == 3)) {
FieldCommandImpl fieldCommand = new FieldCommandImpl(fieldInfo[1],
value, fieldInfo[2], this);
this.commands.add(fieldCommand);
} else {
logger.error("Query param name [" + paramName
+ "] is not right format.");
}
} public void addParamValue(Object value) {
this.paramValues.add(value);
} public List getParamValueList() {
return this.paramValues;
} public void addSorted(String orderBy, String ascDesc) {
this.commands.add(new SortCommandImpl(orderBy, ascDesc, this));
} public void addExample(Object object) {
this.commands.add(new ExampleCommandImpl(object));
} public List<CriteriaCommand> getCommands() {
return this.commands;
} public Set<String> getAliasSet() {
return this.aliasSet;
}}
package com.htsoft.core.command;import com.htsoft.core.util.ParamUtil;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;public class ReportFilter {
Map<String, Object> variables = new HashMap<String, Object>(); public ReportFilter() {
} public ReportFilter(HttpServletRequest request) {
Enumeration paramEnu = request.getParameterNames();
while (paramEnu.hasMoreElements()) {
String paramName = (String) paramEnu.nextElement(); if (paramName.startsWith("Q_")) {
String paramValue = request.getParameter(paramName);
addFilter(paramName, paramValue);
}
}
} public void addFilter(String paramName, String value) {
String[] fieldInfo = paramName.split("[_]");
if (fieldInfo.length == 3)
this.variables.put(fieldInfo[1], ParamUtil.convertObject(
fieldInfo[2], value));
} public Map<String, Object> getVariables() {
return this.variables;
} public void setVariables(Map<String, Object> variables) {
this.variables = variables;
}
}
package com.htsoft.core.dao;public interface BaseDao<T> extends GenericDao<T, Long> {
}package com.htsoft.core.dao;import com.htsoft.core.command.QueryFilter;
import com.htsoft.core.web.paging.PagingBean;
import java.io.Serializable;
import java.util.List;public interface GenericDao<T, PK extends Serializable> {
public T save(T paramT); public T merge(T paramT); public T get(PK paramPK); public void remove(PK paramPK); public void remove(T paramT); public void evict(T paramT); public List<T> getAll(); public List<T> getAll(PagingBean paramPagingBean); public List<T> getAll(QueryFilter paramQueryFilter); public List<T> findByHql(String paramString, Object[] paramArrayOfObject); public List<T> findByHql(String paramString, Object[] paramArrayOfObject,
PagingBean paramPagingBean); public List<T> findByHql(String paramString, Object[] paramArrayOfObject,
int paramInt1, int paramInt2); public void flush();
}
package com.htsoft.core.dao.impl;import com.htsoft.core.dao.BaseDao;public class BaseDaoImpl<T> extends GenericDaoImpl<T, Long> implements
BaseDao<T> {
public BaseDaoImpl(Class persistType) {
super(persistType);
}
}
[code=Java]
package com.htsoft.core.dao.impl;import com.htsoft.core.command.CriteriaCommand;
import com.htsoft.core.command.FieldCommandImpl;
import com.htsoft.core.command.QueryFilter;
import com.htsoft.core.command.SortCommandImpl;
import com.htsoft.core.dao.GenericDao;
import com.htsoft.core.web.paging.PagingBean;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.hql.ast.QueryTranslatorImpl;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;@SuppressWarnings("unchecked")
public abstract class GenericDaoImpl<T, PK extends Serializable> extends
HibernateDaoSupport implements GenericDao<T, PK> {
protected Log logger = LogFactory.getLog(GenericDaoImpl.class);
protected JdbcTemplate jdbcTemplate;
protected Class persistType;
protected Map<String, String> querys = new HashMap(); public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
} public void setPersistType(Class persistType) {
this.persistType = persistType;
} public GenericDaoImpl(Class persistType) {
this.persistType = persistType;
} public T get(PK id) {
return (T) getHibernateTemplate().get(persistType, id);
} public T save(T entity) {
getHibernateTemplate().saveOrUpdate(entity);
return entity;
} public T merge(T entity) {
getHibernateTemplate().merge(entity);
return entity;
} public void evict(T entity) {
getHibernateTemplate().evict(entity);
} public List find(final String queryString, final Object[] values,
final int firstResult, final int pageSize) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query queryObject = session.createQuery(queryString);
if (values != null) {
for (int i = 0; i < values.length; ++i) {
queryObject.setParameter(i, values[i]);
}
}
if (pageSize > 0) {
queryObject.setFirstResult(firstResult).setMaxResults(
pageSize).setFetchSize(pageSize);
}
return queryObject.list();
}
});
} public List<T> getAll() {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
String hql = "from "
+ persistType.getName();
Query query = session.createQuery(hql);
return query.list();
}
});
} public List<T> getAll(final PagingBean pb) {
final String hql = "from " + this.persistType.getName();
int totalItems = getTotalItems(hql, null).intValue();
pb.setTotalItems(totalItems);
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
query.setFirstResult(pb.getFirstResult()).setFetchSize(
pb.getPageSize().intValue());
query.setMaxResults(pb.getPageSize().intValue());
return query.list();
}
});
} public Long getTotalItems(String queryString, final Object[] values) {
int orderByIndex = queryString.toUpperCase().indexOf(" ORDER BY "); if (orderByIndex != -1) {
queryString = queryString.substring(0, orderByIndex);
} QueryTranslatorImpl queryTranslator = new QueryTranslatorImpl(
queryString, queryString, Collections.EMPTY_MAP,
(SessionFactoryImplementor) getSessionFactory());
queryTranslator.compile(Collections.EMPTY_MAP, false);
final String sql = "select count(*) from ("
+ queryTranslator.getSQLString() + ") tmp_count_t"; Object reVal = getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
SQLQuery query = session.createSQLQuery(sql);
if (values != null) {
for (int i = 0; i < values.length; ++i) {
query.setParameter(i, values[i]);
}
}
return query.uniqueResult();
}
});
return new Long(reVal.toString());
}
return (List<T>) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
if (objs != null) {
for (int i = 0; i < objs.length; ++i) {
query.setParameter(i, objs[i]);
}
}
return query.list();
}
});
} public List<T> findByHql(final String hql, final Object[] objs,
final int firstResult, final int pageSize) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
query.setFirstResult(firstResult).setMaxResults(pageSize);
if (objs != null) {
for (int i = 0; i < objs.length; ++i) {
query.setParameter(i, objs[i]);
}
}
return query.list();
}
});
} public List<T> findByHql(String hql, Object[] objs, PagingBean pb) {
int totalItems = getTotalItems(hql, objs).intValue();
pb.setTotalItems(totalItems);
return findByHql(hql, objs, pb.getFirstResult(), pb.getPageSize()
.intValue());
} public List<T> findByHql(String hql) {
return findByHql(hql, null);
} public void remove(PK id) {
getHibernateTemplate().delete(get(id));
} public void remove(T entity) {
getHibernateTemplate().delete(entity);
} public Object findUnique(final String hql, final Object[] values) {
return getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql); if (values != null) {
for (int i = 0; i < values.length; ++i) {
query.setParameter(i, values[i]);
}
}
return query.uniqueResult();
}
});
} public int getCountByFilter(final QueryFilter filter) {
Integer count = (Integer) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Criteria criteria = session
.createCriteria(GenericDaoImpl.this.persistType);
for (int i = 0; i < filter.getCommands().size(); ++i) {
CriteriaCommand command = (CriteriaCommand) filter
.getCommands().get(i);
if (!(command instanceof SortCommandImpl)) {
criteria = command.execute(criteria);
}
}
criteria.setProjection(Projections.rowCount());
return criteria.uniqueResult();
}
});
if (count == null)
return new Integer(0).intValue();
return count.intValue();
} public List getAll(final QueryFilter queryFilter) {
if (StringUtils.isNotEmpty(queryFilter.getFilterName())) {
return getAll2(queryFilter);
} int totalCounts = getCountByFilter(queryFilter); queryFilter.getPagingBean().setTotalItems(totalCounts); List resultList = (List) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Criteria criteria = session.createCriteria(persistType); queryFilter.getAliasSet().clear();
GenericDaoImpl.this.setCriteriaByQueryFilter(criteria,
queryFilter);
return criteria.list();
}
});
return resultList;
} public List getAll2(QueryFilter queryFilter) {
String hql = ((String) this.querys.get(queryFilter.getFilterName()))
.trim(); String newHql = null;
String condition = null;
String groupBy = null; int orderIndex = hql.toUpperCase().indexOf(" ORDER BY ");
int whereIndex = hql.toUpperCase().indexOf(" WHERE "); if (orderIndex < 0) {
orderIndex = hql.length();
}
if (whereIndex < 0) {
whereIndex = hql.length();
} if (whereIndex < 0) {
condition = " where 1=1 ";
} else {
condition = hql.substring(whereIndex + 7, orderIndex); this.logger.debug("condition:" + condition); Pattern groupByPattern = Pattern.compile(" GROUP BY [\\w|.]+");
Matcher m = groupByPattern.matcher(condition.toUpperCase()); if (m.find()) {
groupBy = condition.substring(m.start(), m.end());
condition = condition.replace(groupBy, " ");
}
condition = " where (" + condition + ")";
} String sortDesc = ""; for (int i = 0; i < queryFilter.getCommands().size(); ++i) {
CriteriaCommand command = (CriteriaCommand) queryFilter
.getCommands().get(i);
if (command instanceof FieldCommandImpl) {
condition = condition + " and "
+ ((FieldCommandImpl) command).getPartHql();
} else if (command instanceof SortCommandImpl) {
if (!"".equals(sortDesc)) {
sortDesc = sortDesc + ",";
}
sortDesc = sortDesc + ((SortCommandImpl) command).getPartHql();
}
} newHql = hql.substring(0, whereIndex); if (queryFilter.getAliasSet().size() > 0) {
int fromIndex = newHql.indexOf(" FROM ");
String entityAliasName = null;
if (fromIndex > 0) {
String afterFrom = newHql.substring(fromIndex + 6); String[] keys = afterFrom.split("[ ]");
if ((keys.length > 1)
&& (!keys[1].toUpperCase().equals("ORDER"))
&& (!keys[1].toUpperCase().equals("JOIN"))) {
entityAliasName = keys[1];
} if (entityAliasName == null) {
entityAliasName = "vo";
newHql = newHql.replace(keys[0], keys[0] + " "
+ entityAliasName);
} } String joinHql = "";
Iterator it = queryFilter.getAliasSet().iterator();
while (it.hasNext()) {
String joinVo = (String) it.next();
joinHql = joinHql + " join " + entityAliasName + "." + joinVo
+ " " + joinVo;
} if (!"".equals(joinHql)) {
newHql = newHql + joinHql;
}
} newHql = newHql + condition; if (groupBy != null) {
newHql = newHql + groupBy + " ";
} if (!"".equals(sortDesc))
newHql = newHql + " order by " + sortDesc;
else {
newHql = newHql + hql.substring(orderIndex);
} Object[] params = queryFilter.getParamValueList().toArray(); int totalItems = getTotalItems(newHql, params).intValue();
queryFilter.getPagingBean().setTotalItems(totalItems);
if (this.logger.isDebugEnabled()) {
this.logger.debug("new hql:" + newHql);
}
return find(newHql, params, queryFilter.getPagingBean()
.getFirstResult(), queryFilter.getPagingBean().getPageSize()
.intValue());
} public void flush() {
getHibernateTemplate().flush();
} private Criteria setCriteriaByQueryFilter(Criteria criteria,
QueryFilter filter) {
for (int i = 0; i < filter.getCommands().size(); ++i) {
criteria = ((CriteriaCommand) filter.getCommands().get(i))
.execute(criteria);
}
criteria.setFirstResult(filter.getPagingBean().getFirstResult());
criteria.setMaxResults(filter.getPagingBean().getPageSize().intValue());
return criteria;
} public void setQuerys(Map<String, String> querys) {
this.querys = querys;
}
}[/code]
上面就是基本封装的代码,由于没有任何文档和注释,不知道有没有高手能帮忙看下作者的封装思路火龙果来看下,神人来了没有
相信楼主的底层框架封装不止这些,还有你们框架的整体机制,希望楼主先了解你们框架的运行机制,再研究底层这段代码。