标签:
public abstract class BaseEntityDAOImpl<T> { protected Class<T> entityClass; public BaseEntityDAOImpl() {
// 由于Java 方法的动态绑定getClass()调用的是子类方法
// getGenericSuperclass()返回直接父类的Type类型,并保存了泛型参数的实际类型信息。 Type genType = getClass().getGenericSuperclass(); Type[] params = ((ParameterizedType)genType).getActualTypeArguments();
// 获取实际的泛型参数的类型信息。 entityClass = (Class<T>) params[0]; }
}
public class BaseEntityDAOImpl<T> { protected Class<T> entityClass; //@PersistenceContext注解后,entityManager由Spring负责注入 @PersistenceContext protected EntityManager entityManager; public BaseEntityDAOImpl() { Type genType = getClass().getGenericSuperclass(); Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); entityClass = (Class<T>) params[0]; } public EntityManager getEntityManager(){ return entityManager; } public void setEntityManager(EntityManager entityManager){ this.entityManager=entityManager; } public void add(T t){ entityManager.persist(t); } public void update(T t){ entityManager.merge(t); } public T getById(long id){ return entityManager.find(entityClass, id); } public void deleteById(long id){ T t=getById(id); if(t!=null){ entityManager.remove(t); } } public void delete(T t){ entityManager.remove(t); } public List<T> getListByPage(int offset,int maxResult){ return (List<T>)getEntityManager().createQuery("from "+entityClass.getSimpleName()).setFirstResult(offset).setMaxResults(maxResult).getResultList(); } public List<T> getAll(){ return (List<T>)getEntityManager().createQuery("from "+entityClass.getSimpleName()).getResultList(); } }
<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="cn.cjtblog.jpatest"/> <context:property-placeholder location="classpath:jdbc.properties" /> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 对注解Jpa EntityManager的@PersistenceContext,进行注入 --> <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan" value="cn.cjtblog.jpatest" /> <property name="jpaVendorAdapter" ref="jpaVendorAdapter" /> <property name="jpaDialect" ref="jpaDialect" /> <property name="persistenceProvider" ref="persistenceProvider" /> <property name="jpaProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> </bean> <bean id="persistenceProvider" class="org.hibernate.jpa.HibernatePersistenceProvider" /> <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="database" value="MYSQL" /> <property name="showSql" value="true" /> </bean> <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" /> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> <property name="jpaDialect" ref="jpaDialect" /> </bean> </beans>
标签:
原文地址:http://www.cnblogs.com/NotOnlyAnAnswer/p/5262868.html