Spring整合JPA(XML及Annotation方式)

基本目标:

  1. 完成JPA引擎的管理(XML或Annotation方式)
  2. 向“数据访问层”注入EntityManager
  3. 对EntityManager进行线程绑定的管理,自动完成打开和关闭
  4. 声明式事务管理

基于配置方式的操作步骤:

EntityManagerFactory的生成

  1. 利用FactoryBean 配置
<bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        
    </bean>
  1. 准备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>
  1. 为entityManagerFactory注入JpaVendorAdapter及DataSource,同时指定“实体类”扫描路径
      <!-- entityManagerFactory 内部 -->
      <property name="dataSource" ref="ds" />
      <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
      <property name="packagesToScan" value="pojo" />

此时,EntityManagerFactory即可以使用了(@PersistenceUnit对EntityManagerFactory进行注解式注入)

  1. 配置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>
  1. 为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方式操作步骤:

  1. EntityManagerFactory的生成
        @Bean
    public LocalContainerEntityManagerFactoryBean entityManageFactory() {
        LocalContainerEntityManagerFactoryBean emfb = new LocalContainerEntityManagerFactoryBean();
        
        return emfb;
    }
  1. 准备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;
    }
  1. 为entityManagerFactory注入JpaVendorAdapter及DataSource,同时指定“实体类”扫描路径
        emfb.setDataSource(getDataSource());
        emfb.setJpaVendorAdapter(getHibernateJpaVendorAdapter());
        emfb.setPackagesToScan("pojo"); 

此时,EntityManagerFactory即可以使用了(@PersistenceUnit对EntityManagerFactory进行注解式注入)

  1. 配置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);
  1. 为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对方法进行注解配置,表明其具有事务

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。