/trunk/springmvc/src/main/java/com/def/springmvc/dao/base/AbstractDao.java

https://gitlab.com/BGCX262/zy-myjava-svn-to-git · Java · 184 lines · 149 code · 26 blank · 9 comment · 15 complexity · 24ea2f8acbded0e2e01404450a150223 MD5 · raw file

  1. package com.def.springmvc.dao.base;
  2. import java.util.List;
  3. import java.util.Map;
  4. import javax.persistence.EntityManager;
  5. import javax.persistence.NoResultException;
  6. import javax.persistence.PersistenceContext;
  7. import javax.persistence.Query;
  8. import javax.persistence.TypedQuery;
  9. import javax.persistence.criteria.CriteriaQuery;
  10. import org.apache.commons.lang.StringUtils;
  11. import com.def.springmvc.entity.base.IdEntity;
  12. import com.def.springmvc.util.CollectionUtil;
  13. import com.def.springmvc.util.Page;
  14. /**
  15. *
  16. * @author <a href="mailto:haozhonghu@hotmail.com">gavin</a>
  17. * @since 0.1.0
  18. * @create May 16, 2011 - 3:50:49 PM
  19. * <a href="http://www.opensourceforce.org">开源力量</a>
  20. */
  21. public abstract class AbstractDao<E extends IdEntity>
  22. implements BaseDao<E> {
  23. protected EntityManager entityManager;
  24. protected Class<E> entityClass;
  25. public AbstractDao(Class<E> entityClass) {
  26. this.entityClass = entityClass;
  27. }
  28. @PersistenceContext
  29. public void setEntityManager(EntityManager entityManager) {
  30. this.entityManager = entityManager;
  31. }
  32. public <PK> E get(PK id) {
  33. return (E) entityManager.getReference(entityClass, id);
  34. }
  35. public void save(E entity) {
  36. entityManager.persist(entity);
  37. }
  38. public void update(E entity) {
  39. entityManager.merge(entity);
  40. }
  41. public void delete(E entity) {
  42. entityManager.remove(entity);
  43. }
  44. public <PK> void delete(PK id) {
  45. E entity = get(id);
  46. entityManager.remove(entity);
  47. }
  48. protected Long count(String qlString, Object... values) {
  49. TypedQuery<Long> countQuery = entityManager.createQuery(qlString, Long.class);
  50. setParametersToQuery(countQuery, values);
  51. return countQuery.getSingleResult();
  52. }
  53. protected Long count(String qlString, Map<String, Object> paramValues) {
  54. TypedQuery<Long> countQuery = entityManager.createQuery(qlString, Long.class);
  55. setParametersToQuery(countQuery, paramValues);
  56. return countQuery.getSingleResult();
  57. }
  58. protected E findOne(String qlString, Object... values) {
  59. TypedQuery<E> query = entityManager.createQuery(qlString, entityClass);
  60. setParametersToQuery(query, values);
  61. try {
  62. return query.getSingleResult();
  63. } catch(NoResultException e) {
  64. return null;
  65. }
  66. }
  67. protected E findOne(String qlString, Map<String, Object> paramValues) {
  68. TypedQuery<E> query = entityManager.createQuery(qlString, entityClass);
  69. setParametersToQuery(query, paramValues);
  70. return query.getSingleResult();
  71. }
  72. protected List<E> findList(String qlString, Object... values) {
  73. TypedQuery<E> query = entityManager.createQuery(qlString, entityClass);
  74. setParametersToQuery(query, values);
  75. return query.getResultList();
  76. }
  77. protected <X> List<X> findList(String qlString, Class<X> retType, Object... values) {
  78. TypedQuery<X> query = entityManager.createQuery(qlString, retType);
  79. setParametersToQuery(query, values);
  80. return query.getResultList();
  81. }
  82. protected Page<E> findPage(Page<E> page, CriteriaQuery<E> criteriaQuery) {
  83. TypedQuery<E> query = entityManager.createQuery(criteriaQuery);
  84. Object[] values = getParameterValues(query);
  85. org.hibernate.Query nativeQuery = query.unwrap(org.hibernate.Query.class);
  86. String qlString = nativeQuery.getQueryString();
  87. return findPage(page, qlString, values);
  88. }
  89. @SuppressWarnings("unchecked")
  90. protected Page<E> findPage(Page<E> page, String qlString, Object...values) {
  91. String countString = getCountQuery(qlString);
  92. if(page.getAutoCount()) {
  93. Long totalCount = count(countString, values);
  94. page.setTotalCount(totalCount);
  95. }
  96. qlString = appendOrders(page, qlString);
  97. Query query = entityManager.createQuery(qlString);
  98. setParametersToQuery(query, values);
  99. query.setFirstResult(page.getFirst()-1);
  100. query.setMaxResults(page.getPageSize());
  101. List<Object> resultList = query.getResultList();
  102. List<E> tmp = CollectionUtil.newArrayList();
  103. for(Object result : resultList) {
  104. if(result!=null && result instanceof Object[]) {
  105. for(Object value : (Object[])result) {
  106. if(value!=null && value.getClass().isAssignableFrom(entityClass)) {
  107. tmp.add((E)value);
  108. }
  109. }
  110. } else {
  111. tmp.add((E)result);
  112. }
  113. }
  114. return page.setResult(tmp);
  115. }
  116. private void setParametersToQuery(Query query, Object...values) {
  117. // set parameters to query
  118. for(int i=1; i<=values.length; i++) {
  119. query.setParameter(i, values[i-1]);
  120. }
  121. }
  122. private Object[] getParameterValues(TypedQuery<?> query) {
  123. List<Object> values = CollectionUtil.newArrayList();
  124. int size = query.getParameters().size();
  125. for(int p = 1; p<=size; p++) {
  126. values.add(query.getParameterValue(p));
  127. }
  128. return values.toArray();
  129. }
  130. private String getCountQuery(String queryStr) {
  131. String countQueryStr = queryStr;
  132. // clean order by or group by
  133. countQueryStr = "FROM " + StringUtils.substringAfter(countQueryStr, "FROM");
  134. countQueryStr = StringUtils.substringBefore(countQueryStr, "ORDER BY");
  135. countQueryStr = StringUtils.substringBefore(countQueryStr,"GROUP BY");
  136. countQueryStr = "SELECT COUNT(*) " + countQueryStr;
  137. return countQueryStr;
  138. }
  139. private String appendOrders(Page<E> page, String qlString) {
  140. if(!page.getOrderList().isEmpty()) {
  141. StringBuffer buffer = new StringBuffer(qlString);
  142. if(!StringUtils.contains(qlString, "ORDER BY")) {
  143. buffer.append(" ORDER BY");
  144. } else {
  145. buffer.append(",");
  146. }
  147. List<String> orderList = page.getOrderList();
  148. for(int i=0; i<orderList.size(); i++) {
  149. buffer.append(" " + orderList.get(i));
  150. if(i + 1 < orderList.size()) {
  151. buffer.append(",");
  152. }
  153. }
  154. return buffer.toString();
  155. }
  156. return qlString;
  157. }
  158. }