歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Spring注入JPA+JPA事務管理

Spring注入JPA+JPA事務管理

日期:2017/3/1 9:09:27   编辑:Linux編程

本例實現的是Spring注入JPA 和 使用JPA事務管理。JPA是sun公司開發的一項新的規范標准。在本質上來說,JPA可以看作是Hibernate的一個子集;然而從功能上來說,Hibernate是JPA的一種實現。

在web開發的過程中,使用hibernate進行數據庫連接、事務等的管理。當然也可以使用JPA替換Hibernate是實現這些功能。

一、使用Spring來注入JPA

之前,在

  • ssh(sturts2_spring_hibernate) 框架搭建之hibernate1 http://www.linuxidc.com/Linux/2016-09/134985.htm
  • ssh(sturts2_spring_hibernate) 框架搭建之spring http://www.linuxidc.com/Linux/2016-09/134984.htm
  • ssh(sturts2_spring_hibernate) 框架搭建之struts2 http://www.linuxidc.com/Linux/2016-09/134983.htm
  • SSH(Sturts2+Spring+Hibernate) 框架搭建之JPA代替Hibernate http://www.linuxidc.com/Linux/2016-11/137185.htm

中已經進行了hibernate的搭建和後期的是實現。了解到了,獲取hibernate中的sessionFactory有兩種方式。

    一種是自己手動獲取src目錄下hibernate.cfg.xml配置文件:(在dao層面的代碼)

        SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();

    另外一種是使用spring注入,並且可以刪除src目錄下的配置文件:

      在spring的配置文件applicationContext.xml文件中進行sessionFactory和dataSource的bean的配置:之後在dao層面的實現類中定義一個屬性為SessionFactory,使用spring注入

同理,使用JPA也有這兩中方式來是實現。先前,本人博客中已進行了JPA第一種的實現(手動書寫代碼讀取JPA配置文件):http://www.linuxidc.com/Linux/2016-11/137185.htm,所以在此,進行第二種使用spring注入的方式是實現獲取EntityManagerFactory。

1、首先:在spring配置文件創建一個bean標簽,目的創建一個EntityManagerFactory,這裡不同於sessionFactory的bean,因為這裡讀取的是src目錄下的META-INF目錄下的persistence.xml 即JPA的配置文件,所以在下面設置的是持久化單元名"myJpa",對應JPA配置文件中的單元名字。

      <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">  
                <property name="persistenceUnitName" value="myJpa"/>  
          </bean>  

      JPA的配置文件 :persistence.xml,這裡的具體不做過多的解釋,如果有疑問,請上網查找資料。

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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_2_0.xsd">

    <persistence-unit name="myJpa" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>entity.Product</class>
<properties> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> <property name="hibernate.connection.username" value="root"/> <property name="hibernate.connection.password" value="123456"/> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/ProductDB"/> <property name="hibernate.show_sql" value="true"></property> <property name="hibernate.format_sql" value="true"></property> <property name="hibernate.connection.autocommit" value="false"/> <property name="hibernate.hbm2ddl.auto" value="update"/> </properties> </persistence-unit> </persistence>

2、最後:在dao層面設置sessionFactory成員屬性。

  

在這裡,可以使用spring的注解,或者是使用bean注入的方式,這裡使用的spring的注入進行注入。使用spring注解需要在spring配置文件中添加一個注解的解析器:,這個@Qualifier("entityManagerFactory")注解中的entityManagerFactory對應的是spring配置文件中JPA配置的entityManagerFactory的id屬性值。

二、添加JPA的事務管理

1、首先建立在之前使用spring注入JPA的基礎之上,再加入JPA的事務管理。好處 不需要手動開啟、提交事務和關閉資源,因為這些都是重復的代碼,做的事情都是同一樣的。所以可以交給事務來管理。

首先:在spring配置文件applicationContext.xml中添加事務管理,如下 

            <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">  
                     <property name="entityManagerFactory" ref="entityManagerFactory"/>  
            </bean>  

            <tx:annotation-driven transaction-manager="txManager"/> 

          其次:在dao層面加上注解@Transactional。這個注解需要加載類上。並且在這個dao類上加上一個屬性:之後就可以直接使用這個entityManager進行CURD操作。

這個屬性: 的定義是建立在JPA的事務管理之上的,當web容器讀取spring配置文件中的entityManagerFactory後會自動創建一個entityManager,然後根據在dao類中定義的這個屬性進行注入,和上面的定義EntityManagerFactory不同。具體的可以自己進行嘗試。 

Hibernate3.1.2_中文文檔PDF http://www.linuxidc.com/Linux/2016-02/128462.htm

Hibernate學習入門教程 http://www.linuxidc.com/Linux/2015-08/121498.htm

在Hibernate中開啟日志 http://www.linuxidc.com/Linux/2015-07/120499.htm

Hibernate+JUnit測試實體類生成數據庫表 http://www.linuxidc.com/Linux/2015-07/120161.htm

Hibernate整體理解 http://www.linuxidc.com/Linux/2014-07/104405.htm

Hibernate的映射機制 http://www.linuxidc.com/Linux/2014-12/110265.htm

Hibernate 的詳細介紹:請點這裡
Hibernate 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved