国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Java事務(wù)管理學(xué)習(xí)之Spring和Hibernate詳解

Java事務(wù)管理學(xué)習(xí)之Spring和Hibernate詳解

2020-09-02 09:51oscar999 Java教程

這篇文章主要給大家介紹了Java事務(wù)管理學(xué)習(xí)之Spring和Hibernate的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們可以參考借鑒,下面來一起看看吧。

環(huán)境與版本

本文出來之前的一篇文章中的hibernate的相關(guān)lib 外

Java事務(wù)管理之Hibernate

還需要加入spring的lib 包和如下的一些依賴包

      org.aopalliance

      org.aspectj

      org.apache.commons

Spring 的版本是Spring 4.1.5。

依賴包也可以到Spring 官方網(wǎng)站下載到 ,名字類似 spring-framework-3.0.2.RELEASE-dependencies

理論知識

Spring和Hibernate整合后,通過Hibernate API進(jìn)行數(shù)據(jù)庫操作時(shí)發(fā)現(xiàn)每次都要opensession,close,beginTransaction,commit,這些都是重復(fù)的工作,我們可以把事務(wù)管理部分交給spring框架完成。

使用spring管理事務(wù)后在dao中不再需要調(diào)用beginTransaction和commit,也不需要調(diào)用session.close() ,使用API  sessionFactory.getCurrentSession()來替代sessionFactory.openSession()

* 如果使用的是本地事務(wù)(jdbc事務(wù))

<property name="hibernate.current_session_context_class">thread</property>

* 如果使用的是全局事務(wù)(jta事務(wù))

<property name="hibernate.current_session_context_class">jta</property>

 

Spring中Propagation類的事務(wù)屬性詳解:

     PROPAGATION_REQUIRED:支持當(dāng)前事務(wù),如果當(dāng)前沒有事務(wù),就新建一個(gè)事務(wù)。這是最常見的選擇。

     PROPAGATION_SUPPORTS:支持當(dāng)前事務(wù),如果當(dāng)前沒有事務(wù),就以非事務(wù)方式執(zhí)行。

     PROPAGATION_MANDATORY:支持當(dāng)前事務(wù),如果當(dāng)前沒有事務(wù),就拋出異常。

     PROPAGATION_REQUIRES_NEW:新建事務(wù),如果當(dāng)前存在事務(wù),把當(dāng)前事務(wù)掛起。

     PROPAGATION_NOT_SUPPORTED:以非事務(wù)方式執(zhí)行操作,如果當(dāng)前存在事務(wù),就把當(dāng)前事務(wù)掛起。

     PROPAGATION_NEVER:以非事務(wù)方式執(zhí)行,如果當(dāng)前存在事務(wù),則拋出異常。

     PROPAGATION_NESTED:支持當(dāng)前事務(wù),如果當(dāng)前事務(wù)存在,則執(zhí)行一個(gè)嵌套事務(wù),如果當(dāng)前沒有事務(wù),就新建一個(gè)事務(wù)。

Spring 可以使用xml方式進(jìn)行配置或是使用注解聲明的方式進(jìn)行事務(wù)的管理。

xml 方式配置事務(wù)代碼實(shí)例

代碼結(jié)構(gòu)如下:

Java事務(wù)管理學(xué)習(xí)之Spring和Hibernate詳解

applicationContext.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?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:context="http://www.springframework.org/schema/context"
 xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="
  http://www.springframework.org/schema/util
  http://www.springframework.org/schema/util/spring-util-3.1.xsd
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.1.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
 
 <context:component-scan base-package="com.oscar999.trans.sprhib" />
 <context:property-placeholder location="classpath:/com/oscar999/trans/sprhib/config.properties" />
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close">
  <!-- Connection Info -->
  <property name="driverClassName" value="${jdbc.driver}" />
  <property name="url" value="${jdbc.url}" />
  <property name="username" value="${jdbc.username}" />
  <property name="password" value="${jdbc.password}"></property>
 </bean>
 
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource"></property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.format_sql">true</prop>
    <prop key="hibernate.jdbc.batch_size">20</prop>
    <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
    <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
    <prop key="jdbc.use_streams_for_binary">true</prop>
   </props>
  </property>
  <property name="packagesToScan">
   <list>
    <value>com.oscar999.trans.sprhib.model</value>
   </list>
  </property>
 </bean>
 
 <!-- Transaction -->
 <bean id="transactionManager"
  class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
   <tx:method name="save*" propagation="REQUIRED" />
   <tx:method name="*" read-only="true" />
  </tx:attributes>
 </tx:advice>
 <aop:config proxy-target-class="true">
  <aop:pointcut expression="execution(* com.oscar999.trans.sprhib.dao.ProductDAOImpl.*(..))" id="pointcut" />
  <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
 </aop:config>
 
</beans>

config.properties

?
1
2
3
4
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@12.6.18.43:1521:orcl
jdbc.username=
jdbc.password=oracle

Product.Java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
 * @Title: Product.java
 * @Package com.oscar999.trans.hibernate
 * @Description:
 * @author XM
 * @date Feb 15, 2017 1:44:47 PM
 * @version V1.0
 */
package com.oscar999.trans.sprhib.model;
 
import java.io.Serializable;
 
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
 
/**
 * @author XM
 *
 */
@Entity
@Table(name = "TEST_PRODUCT")
public class Product implements Serializable {
 
 public Product() {
 }
 
 @Id
 @Column(name = "ID")
 private Integer id;
 
 @Column(name = "NAME")
 private String name;
 
 @Column(name = "PRICE")
 private String price;
 
 private static final long serialVersionUID = 1L;
 
 public Integer getId() {
  return id;
 }
 
 public void setId(Integer id) {
  this.id = id;
 }
 
 public String getName() {
  return name;
 }
 
 public void setName(String name) {
  this.name = name;
 }
 
 public String getPrice() {
  return price;
 }
 
 public void setPrice(String price) {
  this.price = price;
 }
 
}

ProductDAOImpl.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
 * @Title: ProductDAOImpl.java
 * @Package com.oscar999.trans.sprhib
 * @Description:
 * @author XM
 * @date Feb 15, 2017 4:15:09 PM
 * @version V1.0
 */
package com.oscar999.trans.sprhib.dao;
 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
 
import com.oscar999.trans.sprhib.model.Product;
 
/**
 * @author XM
 *
 */
@Repository
public class ProductDAOImpl {
 
 @Autowired
 private SessionFactory sessionFactory;
 
 public Product findProductById(int id) {
  Session session = sessionFactory.getCurrentSession();
  Product product = (Product) session.get(Product.class, id);
  return product;
 }
  
 public Product saveProduct(Product product) {
  Session session = sessionFactory.getCurrentSession();
  session.save(product);
  return product;
 }
}

ProductServiceImpl.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.oscar999.trans.sprhib;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import com.oscar999.trans.sprhib.dao.ProductDAOImpl;
import com.oscar999.trans.sprhib.model.Product;
 
@Service
public class ProductServiceImpl {
 
 @Autowired
 private ProductDAOImpl productdao;
 
 public void findProduct(int id) {
  Product product = productdao.findProductById(id);
  if (product != null) {
   System.out.println(product.getName());
  }
 }
 
 public void saveProduct() {
  Product product = new Product();
  product.setId(2);
  product.setName("product2");
  product.setPrice("price2");
  productdao.saveProduct(product);
 
 }
}

TestMain.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
 * @Title: TestMain.java
 * @Package com.oscar999.trans.sprhib
 * @Description:
 * @author XM
 * @date Feb 15, 2017 3:54:54 PM
 * @version V1.0
 */
package com.oscar999.trans.sprhib;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
/**
 * @author XM
 *
 */
public class TestMain {
 
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/oscar999/trans/sprhib/applicationContext.xml");
  ProductServiceImpl productService = applicationContext.getBean(ProductServiceImpl.class);
  //productService.findProduct(1);
  productService.saveProduct();
 }
 
}

說明如下:

get 可以不需要transaction

插入或是更新如果沒有的話, 就不會(huì)更新成功

聲明方式配置事務(wù)

需要在xml配制中設(shè)置<tx:annotation-driven transaction-manager="transactionManager">

事物注解方式: @Transactional

當(dāng)標(biāo)于類前時(shí),標(biāo)示類中所有方法都進(jìn)行事物處理,以下代碼在service層進(jìn)行事務(wù)處理(給Service層配置事務(wù)是比較好的方式,因?yàn)橐粋€(gè)Service層方法操作可以關(guān)聯(lián)到多個(gè)DAO的操作。在Service層執(zhí)行這些Dao操作,多DAO操作有失敗全部回滾,成功則全部提交。)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@Service
 
 
 @Transactional
 
 
 public class UserServiceImpl implements UserService {
 
 
  @Autowired
 
 
  private UserDao userDao;
 
 
 
  public User getUserById(int id) {
 
 
   return userDao.findUserById(id);
 
 
  }
 
 
 }

當(dāng)類中某些方法不需要事物時(shí):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@Service
 
 
 @Transactional
 
 
 public class UserServiceImpl implements UserService {
 
 
  @Autowired
 
 
  private UserDao userDao;
 
 
 
  @Transactional(propagation = Propagation.NOT_SUPPORTED)
 
 
  public User getUserById(int id) {
 
 
   return userDao.findUserById(id);
 
 
  }

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家學(xué)習(xí)或者使用java能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對服務(wù)器之家的支持。

原文鏈接:http://blog.csdn.net/oscar999/article/details/55261446

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美日韩一区二区电影 | 国产91精品一区二区绿帽 | 狠狠干欧美 | 激情欧美一区二区三区中文字幕 | 一区视频| 国产精品久久久久久福利一牛影视 | 日韩精品www | 免费观看国产精品 | 中文字幕在线日韩 | 亚洲精品资源在线观看 | 久久com | 精品国产欧美一区二区三区成人 | 日韩一区二区三区福利视频 | 欧美日本精品 | 免费在线一区二区 | 香蕉久久久| 中文字幕在线观看免费 | 国产 高清 在线 | 中文字幕视频在线观看 | 日日夜夜精品免费视频 | 日韩福利在线 | 最近的中文字幕在线看视频 | 99精品欧美一区二区蜜桃免费 | 久热精品免费 | 久久久久.com | 黄色片视频免费在线观看 | 亚洲视频自拍 | 国产一级毛片国语一级 | 亚洲精品视频在线播放 | 日韩电影在线一区 | 久久精品亚洲精品 | 综合久久久久 | 亚洲精品三级 | 中文字幕亚洲欧美日韩在线不卡 | 精品国产乱码一区二区三区四区 | 国产精品国产自产拍高清av | 精品国产一区二区三区日日嗨 | 免费观看h片 | 可以免费在线观看av的网站 | 国产精品成人3p一区二区三区 | 亚洲精品视频免费在线观看 |