基本目标:
- 完成JPA引擎的管理(XML或Annotation方式)
- 向“数据访问层”注入EntityManager
- 对EntityManager进行线程绑定的管理,自动完成打开和关闭
- 声明式事务管理
基于配置方式的操作步骤:
EntityManagerFactory的生成
- 利用FactoryBean 配置
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
</bean>
- 准备DataSource及JpaVendorAdapter对象
<bean id="ds"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="password" value="123"></property>
<property name="username" value="root"></property>
</bean>
<bean id="hibernateJpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
</bean>
- 为entityManagerFactory注入JpaVendorAdapter及DataSource,同时指定“实体类”扫描路径
<!-- entityManagerFactory 内部 -->
<property name="dataSource" ref="ds" />
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
<property name="packagesToScan" value="pojo" />
此时,EntityManagerFactory即可以使用了(@PersistenceUnit
对EntityManagerFactory进行注解式注入)
- 配置JPA引擎的附加属性
<!-- entityManagerFactory 内部 -->
<property name="jpaProperties">
<props>
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
- 为Spring容器提供EntityManager对象(ThreadLocal机制)
<!-- entityManagerFactory 内部 -->
<property name="persistenceUnitName" value="entityManager"></property>
此时可以利用@PersistenceContext
对EntityManager进行注解注入
到此EntityManagerFactory配置完成
配置TransactionManager及“配置声明式事务驱动”
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" />
此时,可以使用@Transactional
对方法进行注解配置,表明其具有事务
基于Annotation方式操作步骤:
- EntityManagerFactory的生成
@Bean
public LocalContainerEntityManagerFactoryBean entityManageFactory() {
LocalContainerEntityManagerFactoryBean emfb = new LocalContainerEntityManagerFactoryBean();
return emfb;
}
- 准备DataSource及JpaVendorAdapter对象
@Bean
public DataSource getDataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource("jdbc:mysql://localhost:3306/test", "root", "123");
ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
return ds;
}
@Bean
public JpaVendorAdapter getHibernateJpaVendorAdapter() {
HibernateJpaVendorAdapter vendor = new HibernateJpaVendorAdapter();
vendor.setDatabasePlatform("org.hibernate.dialect.MySQL5Dialect");
return vendor;
}
- 为entityManagerFactory注入JpaVendorAdapter及DataSource,同时指定“实体类”扫描路径
emfb.setDataSource(getDataSource());
emfb.setJpaVendorAdapter(getHibernateJpaVendorAdapter());
emfb.setPackagesToScan("pojo");
此时,EntityManagerFactory即可以使用了(@PersistenceUnit
对EntityManagerFactory进行注解式注入)
- 配置JPA引擎的附加属性
Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.ejb.naming_strategy", "org.hibernate.cfg.ImprovedNamingStrategy");
jpaProperties.put("hibernate.show_sql", "true");
jpaProperties.put("hibernate.hbm2ddl.auto", "create");
emfb.setJpaProperties(jpaProperties);
- 为Spring容器提供EntityManager对象(ThreadLocal机制)
emfb.setPersistenceUnitName("entityManager");
此时可以利用@PersistenceContext
对EntityManager进行注解注入
到此EntityManagerFactory配置完成
配置TransactionManager及“配置声明式事务驱动”
@Bean
public JpaTransactionManager getTransactionManager() {
return new JpaTransactionManager((EntityManagerFactory) entityManageFactory().getObject());
}
@EnableTransactionManagement(proxyTargetClass = false)
public class App {
此时,可以使用@Transactional对方法进行注解配置,表明其具有事务