Spring+JPA 通用DAO及实现

接口:

import java.util.LinkedHashMap; import com.itcast.bean.QueryResult; public interface DAO { /** * 获取记录总数 * @param entityClass 实体类 * @return */ public long getCount(Class entityClass); /** * 清除一级缓存的数据 */ public void clear(); /** * 保存实体 * @param entity 实体id */ public void save(Object entity); /** * 更新实体 * @param entity 实体id */ public void update(Object entity); /** * 删除实体 * @param entityClass 实体类 * @param entityid 实体id */ public void delete(Class entityClass, Object entityid); /** * 删除实体 * @param entityClass 实体类 * @param entityids 实体id数组 */ public void delete(Class entityClass, Object[] entityids); /** * 获取实体 * @param * @param entityClass 实体类 * @param entityId 实体id * @return */ public T find(Class entityClass, Object entityId); /** * 获取分页数据 * @param * @param entityClass 实体类 * @param firstindex 开始索引 * @param maxresult 需要获取的记录数 * @return */ public QueryResult getScrollData(Class entityClass, int firstindex, int maxresult , String wherejpql, Object[] queryParams,LinkedHashMap orderby); public QueryResult getScrollData(Class entityClass, int firstindex, int maxresult , String wherejpql, Object[] queryParams); public QueryResult getScrollData(Class entityClass, int firstindex, int maxresult , LinkedHashMap orderby); public QueryResult getScrollData(Class entityClass, int firstindex, int maxresult); public QueryResult getScrollData(Class entityClass); }

实现类:

import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.util.LinkedHashMap; import javax.persistence.EmbeddedId; 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.itcast.bean.QueryResult; @Transactional public abstract class DaoSupport implements DAO{ @PersistenceContext protected EntityManager em; public void clear(){ em.clear(); } public void delete(Class entityClass,Object entityid) { delete(entityClass, new Object[]{entityid}); } public void delete(Class entityClass,Object[] entityids) { for(Object id : entityids){ em.remove(em.getReference(entityClass, id)); } } @Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED) public T find(Class entityClass, Object entityId) { return em.find(entityClass, entityId); } public void save(Object entity) { em.persist(entity); } @Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED) public long getCount(Class entityClass) { return (Long)em.createQuery("select count("+ getCountField(entityClass) +") from "+ getEntityName(entityClass)+ " o").getSingleResult(); } public void update(Object entity) { em.merge(entity); } @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); } @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); } @Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED) public QueryResult getScrollData(Class entityClass, int firstindex, int maxresult) { return getScrollData(entityClass,firstindex,maxresult,null,null,null); } @Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED) public QueryResult getScrollData(Class entityClass) { return getScrollData(entityClass, -1, -1); } @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("+ getCountField(entityClass)+ ") 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 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(); Entity entity = entityClass.getAnnotation(Entity.class); if(entity.name()!=null && !"".equals(entity.name())){ entityname = entity.name(); } return entityname; } protected String getCountField(Class clazz){ String out = "o"; try { PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(clazz).getPropertyDescriptors(); for(PropertyDescriptor propertydesc : propertyDescriptors){ Method method = propertydesc.getReadMethod(); if(method!=null && method.isAnnotationPresent(EmbeddedId.class)){ PropertyDescriptor[] ps = Introspector.getBeanInfo(propertydesc.getPropertyType()).getPropertyDescriptors(); out = "o."+ propertydesc.getName()+ "." + (!ps[1].getName().equals("class")? ps[1].getName(): ps[0].getName()); break; } } } catch (Exception e) { e.printStackTrace(); } return out; } }

 beans.xml:

你可能感兴趣的:(Spring)