Spring+JPA 通用DAO及實現(xiàn)
收藏接口:
view plaincopy to clipboardprint?import java.util.LinkedHashMap;
import com.itcast.bean.QueryResult;
public interface DAO {
/**
* 獲取記錄總數(shù)
* @param entityClass 實體類
* @return
*/
public <T> long getCount(Class<T> entityClass);
/**
* 清除一級緩存的數(shù)據(jù)
*/
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 <T> void delete(Class<T> entityClass, Object entityid);
/**
* 刪除實體
* @param entityClass 實體類
* @param entityids 實體id數(shù)組
*/
public <T> void delete(Class<T> entityClass, Object[] entityids);
/**
* 獲取實體
* @param <T>
* @param entityClass 實體類
* @param entityId 實體id
* @return
*/
public <T> T find(Class<T> entityClass, Object entityId);
/**
* 獲取分頁數(shù)據(jù)
* @param <T>
* @param entityClass 實體類
* @param firstindex 開始索引
* @param maxresult 需要獲取的記錄數(shù)
* @return
*/
public <T> QueryResult<T> getScrollData(Class<T> entityClass, int firstindex, int maxresult
, String wherejpql, Object[] queryParams,LinkedHashMap<String, String> orderby);
public <T> QueryResult<T> getScrollData(Class<T> entityClass, int firstindex, int maxresult
, String wherejpql, Object[] queryParams);
public <T> QueryResult<T> getScrollData(Class<T> entityClass, int firstindex, int maxresult
, LinkedHashMap<String, String> orderby);
public <T> QueryResult<T> getScrollData(Class<T> entityClass, int firstindex, int maxresult);
public <T> QueryResult<T> getScrollData(Class<T> entityClass);
}
實現(xiàn)類:
view plaincopy to clipboardprint?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 <T> void delete(Class<T> entityClass,Object entityid) {
delete(entityClass, new Object[]{entityid});
}
public <T> void delete(Class<T> entityClass,Object[] entityids) {
for(Object id : entityids){
em.remove(em.getReference(entityClass, id));
}
}
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public <T> T find(Class<T> entityClass, Object entityId) {
return em.find(entityClass, entityId);
}
public void save(Object entity) {
em.persist(entity);
}
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public <T> long getCount(Class<T> 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 <T> QueryResult<T> getScrollData(Class<T> entityClass,
int firstindex, int maxresult, LinkedHashMap<String, String> orderby) {
return getScrollData(entityClass,firstindex,maxresult,null,null,orderby);
}
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public <T> QueryResult<T> getScrollData(Class<T> 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 <T> QueryResult<T> getScrollData(Class<T> entityClass, int firstindex, int maxresult) {
return getScrollData(entityClass,firstindex,maxresult,null,null,null);
}
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public <T> QueryResult<T> getScrollData(Class<T> entityClass) {
return getScrollData(entityClass, -1, -1);
}
@SuppressWarnings("unchecked")
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public <T> QueryResult<T> getScrollData(Class<T> entityClass, int firstindex, int maxresult
, String wherejpql, Object[] queryParams,LinkedHashMap<String, String> orderby) {
QueryResult qr = new QueryResult<T>();
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<queryParams.length; i++){
query.setParameter(i+1, queryParams[i]);
}
}
}
/**
* 組裝order by語句
* @param orderby
* @return
*/
protected String buildOrderby(LinkedHashMap<String, String> 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 <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;
}
protected <T> String getCountField(Class<T> 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:
view plaincopy to clipboardprint?<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:component-scan base-package="com.itcast"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="itcast"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!-- Activates @Transactional for DefaultImageDatabase -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>