BaseDAOImpl

import java.util.LinkedHashMap;
import java.util.List;


import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


import com.doit.dagama.repo.BaseDAO;
import com.doit.dagama.repo.QueryResult;


@Transactional
public class BaseDAOImpl<T> implements BaseDAO<T> {


@Autowired
protected EntityManagerFactory emf;



@PersistenceContext
protected EntityManager em;


public EntityManager getEntityManager() {
return emf.createEntityManager();
}


@Override
public void save(Object entity) {
emf.createEntityManager().persist(entity);
}


@Override
public void update(Object entity) {
emf.createEntityManager().merge(entity);
}


@Override
public void delete(Class<T> entityClass, Object entityId) {
delete(entityClass, new Object[] { entityId });
}


@Override
public void delete(Class<T> entityClass, Object[] entityIds) {
for (Object id : entityIds) {
em.remove(em.getReference(entityClass, id));
}


}


@Override
public T find(Class<T> entityClass, Object entityId) {
return em.find(entityClass, entityId);
}


@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
public QueryResult<T> getScrollData(Class<T> entityClass,
int firstindex, int maxresult) {
QueryResult qr = new QueryResult<T>();
String entityname = getEntityName(entityClass);
Query query = em.createQuery("select o from " + entityname + " o ");
query.setFirstResult(firstindex).setMaxResults(maxresult);
qr.setResultlist(query.getResultList());
query = em.createQuery("select count(o) from " + entityname + " o ");
qr.setTotalrecord((Long) query.getSingleResult());
return qr;
}


@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public QueryResult<T> getScrollData(Class<T> entityClass,
int firstindex, int maxresult, String wheresql,
Object[] queryParams, LinkedHashMap<String, String> orderby) {
QueryResult qr = new QueryResult<T>();
String entityname = getEntityName(entityClass);
Query query = em.createQuery("select o from " + entityname + " o "
+ (wheresql == null ? "" : " where " + wheresql)
+ buildOrderby(orderby));
setQueryParams(query, queryParams);
query.setFirstResult(firstindex).setMaxResults(maxresult);
List<T> list = query.getResultList();
qr.setResultlist(list);
qr.setTotalrecord(list.size());
return qr;
}


protected void setQueryParams(Query query, Object[] queryParams) {
if (null != queryParams && 0 < queryParams.length) {
for (int i = 0; i < queryParams.length; i++) {
query.setParameter(i + 1, queryParams[i]);
}
}
}


@Override
public QueryResult<T> getScrollData(Class<T> entityClass,
int firstindex, int maxresult, LinkedHashMap<String, String> orderby) {
return getScrollData(entityClass, firstindex, maxresult, null, null,
orderby);
}


protected String buildOrderby(LinkedHashMap<String, String> orderby) {
StringBuffer sb = new StringBuffer();
if (null != orderby && 0 != orderby.size()) {
sb.append(" order by ");
for (String key : orderby.keySet()) {
sb.append("o.").append(key).append(" ")
.append(orderby.get(key)).append(",");
}
sb.deleteCharAt(sb.length() - 1);
}
return sb.toString();
}


/**
* 获取实体的名称

* @param <T>
* @param entityClass
*            实体类
* @return
*/
protected <T> String getEntityName(Class<T> entityClass) {
String entityname = entityClass.getSimpleName();
Entity entity = entityClass.getAnnotation(Entity.class);
if (entity.name() != null && !"".equals(entity.name())) {
entityname = entity.name();
}
return entityname;
}


}

你可能感兴趣的:(BaseDAOImpl)