1. 先配置 PersistenceUnit ,在 persistence.xml 中
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="CallBackUnit" transaction-type="RESOURCE_LOCAL">
<!-- 配置JPA的实现提供者 -->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.aaa.callback.dao.entity.TPredialQueue</class>
<class>com.aaa.callback.dao.entity.TPredialQueueHistory</class>
</persistence-unit>
</persistence>
2. 在 applicationContext.xml 中配置entityManagerFactory
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd ">
<!-- 配置JPA实体管理器工厂 -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="CallBackUnit" />
<property name="persistenceXmlLocation" value="classpath:persistence.xml" />
</bean>
</beans>
3. 配置事务管理器,在 2的配置文件中加入
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory">
<ref bean="entityManagerFactory" />
</property>
</bean>
上面是配置好了事务管理器,但是要让其起作用还得配置以下的内容
4. 配置advice
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" propagation="REQUIRED" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
注:这里的 get*是指以get为开头的方法的事件为只读事务,
5. 配置aop
<aop:config>
<aop:pointcut expression="execution(public * com.aaa.callback.service..*.*(..))" id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
通过以上步骤之后,基本功能就实现了。
当然,还要加入 hibernate.properties的配置
#####################################################################
####Hibernate 属性参数,也可以删除该文件在persistence.xml文件中定义
#####################################################################
#是否显示SQL语句
hibernate.show_sql=true
#是否格式化SQL语句
hibernate.format_sql=true
#数据库更新策略(none,validate,create,create-drop,update)
hibernate.hbm2ddl.auto=update
#自动探测实体(class,hbm)
hibernate.archive.autodetection=class
#Hibernate 连接设置
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8
hibernate.connection.username=root
hibernate.connection.password=123456
#C3P0连接池配置
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=300
hibernate.c3p0.max_statements=50
hibernate.c3p0.acquire_increment=1
hibernate.c3p0.idle_test_period=3000
#是否使用查询缓存
hibernate.cache.use_query_cache=true
#是否使用二级缓存
hibernate.cache.use_second_level_cache=false
#二级缓存实现提供者
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
#Hibernate方言设置,生成特定数据库和优化的SQL语句
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect