Chinaunix首页 | 论坛 | 博客
  • 博客访问: 488369
  • 博文数量: 111
  • 博客积分: 3146
  • 博客等级: 中校
  • 技术积分: 939
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-07 11:23
个人简介

Nathing

文章分类

全部博文(111)

文章存档

2016年(2)

2015年(1)

2014年(31)

2012年(2)

2011年(9)

2010年(36)

2009年(30)

我的朋友

分类: Java

2010-05-27 22:45:24

package com.my.shop.dao;
import java.util.LinkedHashMap;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.my.shop.util.QueryResult;
/**
 * 一般在数据获取的时候不需要开启事务
 * @author Administrator
 *
 */
@Transactional
public abstract class DaoSupport implements DAO
{
 
 @PersistenceContext protected EntityManager em;
 
 @Override
 public void delete(Class entityClass, Object entityId)
 {
  //em.remove(em.getReference(entityClass, entityId));
  delete(entityClass, new Object[]{entityId});
 }
 @Override
 public void delete(Class entityClass, Object[] entityIds)
 {
  for(Object id : entityIds)
  {
   em.remove(em.getReference(entityClass, id));
  }
 }
 @Override
 @Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
 public T find(Class entityClass, Object entityId)
 {
  return em.find(entityClass, entityId);
 }
 @Override
 public void save(Object entity)
 {
  em.persist(entity);
 }
 @Override
 public void update(Object entity)
 {
  em.merge(entity);
 }
 
 @Override
 @Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
 public QueryResult getScrollData(Class entityClass,
   int firstIndex, int maxResult, LinkedHashMap orderby)
 {
  return getScrollData(entityClass, firstIndex, maxResult, null, null, orderby);
 }
 @Override
 @Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
 public QueryResult getScrollData(Class entityClass,
   int firstIndex, int maxResult, String wherejpql,
   Object[] queryParams)
 {
  return getScrollData(entityClass, firstIndex, maxResult, wherejpql, queryParams, null);
 }
 @Override
 @Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
 public QueryResult getScrollData(Class entityClass,
   int firstIndex, int maxResult)
 {
  return getScrollData(entityClass, firstIndex, maxResult, null, null, null);
 }
 @Override
 @Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
 public QueryResult getScrollData(Class entityClass)
 {
  return getScrollData(entityClass, -1, -1);
 }
 @Override
 @SuppressWarnings("unchecked")
 @Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
 public QueryResult getScrollData(Class entityClass, int firstIndex, int maxResult, String wherejpql, Object[] queryParams, LinkedHashMap orderby)
 {
  QueryResult qr = new QueryResult();
  String entityName = getEntityName(entityClass);
  Query query = em.createQuery("select o from "+ entityName +" o " + (wherejpql==null ? "" : "where " + wherejpql) + buildOrderby(orderby));
  setQueryParams(query, queryParams);
  if(firstIndex != -1 && maxResult != -1)
  {
   query.setFirstResult(firstIndex).setMaxResults(maxResult);
  }
  qr.setResultList(query.getResultList());
  
  query = em.createQuery("select count(o) from "+ entityName +" o " + (wherejpql==null ? "" : "where " + wherejpql));
  setQueryParams(query, queryParams);
  qr.setTotalRecord((Long)query.getSingleResult());
  
  return qr;
 }
 
 protected void setQueryParams(Query query, Object[] queryParams)
 {
  if(queryParams != null && queryParams.length>0)
  {
   for(int i=0; i   {
    query.setParameter(i+1, queryParams[i]);
   }
  }
 }
 
 /**
  * 组装order by语句
  * @param orderby
  * @return
  */
 protected String buildOrderby(LinkedHashMap orderby)
 {
  StringBuffer orderbyql = new StringBuffer("");
  if(orderby != null && orderby.size()>0)
  {
   orderbyql.append(" order by ");
   for(String key : orderby.keySet())
   {
    orderbyql.append("o.").append(key).append(" ").append(orderby.get(key)).append(",");
   }
   orderbyql.deleteCharAt(orderbyql.length()-1);
  }
  
  return orderbyql.toString();
 }
 
 /**
  * 获取实体的名称
  * @param
  * @param entityClass
  * @return
  */
 protected String getEntityName(Class entityClass)
 {
  String entityName = entityClass.getSimpleName();
  //获得entityClass的注解的信息
  Entity entity = entityClass.getAnnotation(Entity.class);
  if(entity.name() != null && !"".equals(entity.name()))
  {
   entityName = entity.name();
  }
  return entityName;
 }
}
阅读(1771) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~