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;
}
}
阅读(1766) | 评论(0) | 转发(0) |