日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring事务环境搭建

發(fā)布時(shí)間:2024/4/13 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring事务环境搭建 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Spring事務(wù)里面主要是分為兩種事務(wù),一種是叫做編程事務(wù),編程事務(wù)我們叫做手動(dòng)事務(wù),自己去begin,commit,rollback回滾,這個(gè)是編程事務(wù)的,而起我剛才也講過的,聲明事務(wù)本質(zhì)是使用編程事務(wù)和反射機(jī)制包裝起來的,明天我們會(huì)手寫一下聲明式事務(wù),你們之前有沒有用過JDBC的,你們看一下,在這個(gè)地方,編程事務(wù)實(shí)現(xiàn),你們用過JDBC的話就知道,它是需要獲取JDBC的事務(wù)模板之后,然后你才去做begin和commit,我不知道你們有沒有印象,我跟你們講一下,框架會(huì)給我們提供一個(gè)事務(wù)接口的,這一點(diǎn)我要和你們講一下,本質(zhì)上都是通過編程事務(wù)封裝起來的,在這邊我就講個(gè)例子,講個(gè)什么例子呢,把環(huán)境搭建起來,搭建一個(gè)數(shù)據(jù)庫的環(huán)境,這邊我直接把代碼copy過來,我先把環(huán)境搭建起來CREATE TABLE IF NOT EXISTS `t_users`(`NAME` INT UNSIGNED AUTO_INCREMENT,`age` VARCHAR(100) NOT NULL )ENGINE=InnoDB DEFAULT CHARSET=utf8; <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><dependencies><!-- https://mvnrepository.com/artifact/javassist/javassist --><dependency><groupId>javassist</groupId><artifactId>javassist</artifactId><version>3.12.1.GA</version></dependency><!-- 引入Spring-AOP等相關(guān)Jar --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>3.0.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>3.0.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>3.0.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>3.0.6.RELEASE</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>1.6.1</version></dependency><dependency><groupId>aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.5.3</version></dependency><dependency><groupId>cglib</groupId><artifactId>cglib</artifactId><version>2.1_2</version></dependency><!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --><dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.2</version></dependency><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.37</version></dependency></dependencies></project> <?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:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 這里表示掃包范圍 因?yàn)槲覀兪鞘褂米⒔獾?--><context:component-scan base-package="com.learn"></context:component-scan><!-- 這里表示開啟事務(wù)的注解 你如果想要事務(wù)的話,你必須開啟一個(gè)事務(wù)注解,--><aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!-- 開啟事物注解 --><!-- 1. 數(shù)據(jù)源對象: C3P0連接池 --><!-- 第一步我們加載C3P0數(shù)據(jù)源 DBCP和C3P0的區(qū)別講一下,數(shù)據(jù)庫的連接池,--><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.jdbc.Driver"></property><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/day20"></property><property name="user" value="root"></property><property name="password" value="123456"></property></bean><!-- 2. JdbcTemplate工具類實(shí)例 --><!-- 這里要引用到我的數(shù)據(jù)源 --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><!-- 3.配置事務(wù) --><bean id="dataSourceTransactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean> </beans> package com.learn.dao;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository;/*** 在UserDao里面我們寫一個(gè)什么代碼呢* 是關(guān)于數(shù)據(jù)庫的* * @author Leon.Sun**/ @Repository public class UserDao {@Autowiredprivate JdbcTemplate jdbcTemplate;/*** 表示我有一個(gè)add方法* 有兩個(gè)參數(shù)* 叫name和age* * * @param name* @param age*/public void add(String name, Integer age) {String sql = "INSERT INTO t_users(NAME, age) VALUES(?,?);";int updateResult = jdbcTemplate.update(sql, name, age);System.out.println("updateResult:" + updateResult);}} package com.learn.service.impl;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import com.learn.dao.UserDao; import com.learn.service.UserService;@Service public class UserServiceImpl implements UserService {@Autowiredprivate UserDao userDao;public void add() {/*** 假設(shè)20歲*/userDao.add("test001", 20);/*** 我在這邊打個(gè)斷點(diǎn)大家看一下* 沒有事務(wù)為什么不提交到數(shù)據(jù)庫里面去呢* 走完之后立即提交* 因?yàn)檫@邊沒有事務(wù)控制的* 這邊我去斷點(diǎn)調(diào)試一下* 這段代碼走完之后數(shù)據(jù)庫里面就有了* 你有了之后后面報(bào)個(gè)錯(cuò)* 你已經(jīng)提交到數(shù)據(jù)庫了就回滾不了了* 就會(huì)產(chǎn)生數(shù)據(jù)庫不一致的問題* */System.out.println("開始報(bào)錯(cuò)了..........................");/*** 我寫一段代碼int i = 1/0* 這個(gè)時(shí)候我來問一下你們* 會(huì)插入幾條數(shù)據(jù)* test001會(huì)成功* test002會(huì)失敗* 只有一條成功了* 我們看一下數(shù)據(jù)庫里面* 因?yàn)楫?dāng)他走到test002的時(shí)候* 這個(gè)時(shí)候已經(jīng)報(bào)錯(cuò)了* 肯定不會(huì)往下繼續(xù)執(zhí)行了* 這個(gè)程序直接中斷了* 后面的代碼就不用走了* 那這樣的代碼肯定不合理* 我們要用事務(wù)來保證事務(wù)的一致性問題* 是不是應(yīng)該全部失敗* 而不是一個(gè)成功一個(gè)失敗* 是不是這樣的* 這個(gè)代碼肯定有錯(cuò)* 在這邊我們可以用到事務(wù)* 在這邊我們可以采用事務(wù)了* 把全部整個(gè)都做回滾* 怎么做呢* 在這邊我就要詳細(xì)給你們講了* 首先在這邊我要問一下* int i = 1/0;這行走完了之后* 這個(gè)數(shù)據(jù)會(huì)不會(huì)添加到數(shù)據(jù)庫里面去* 你們說一下* int i = 1/0這行走完的時(shí)候數(shù)據(jù)會(huì)不會(huì)添加到數(shù)據(jù)庫里面去* 肯定會(huì)的* * */int i = 1/0;/*** 中間打印一個(gè)分隔符*/System.out.println("####################################");/*** 再來一個(gè)test002是21歲*/userDao.add("test002", 21);}} package com.learn.test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.learn.service.UserService;public class Test001 {public static void main(String[] args) {ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");UserService userService = (UserService) applicationContext.getBean("userServiceImpl");userService.add();}} 手寫Spring事務(wù)框架 編程事務(wù)實(shí)現(xiàn) 概述 所謂編程式事務(wù)指的是通過編碼方式實(shí)現(xiàn)事務(wù),即類似于JDBC編程實(shí)現(xiàn)事務(wù)管理。管理使用TransactionTemplate或者 直接使用底層的PlatformTransactionManager。對于編程式事務(wù)管理,spring推薦使用TransactionTemplate。 案例 使用編程事務(wù)實(shí)現(xiàn)手動(dòng)事務(wù) 使用編程事務(wù)實(shí)現(xiàn),手動(dòng)事務(wù) begin、commit、rollback

?

總結(jié)

以上是生活随笔為你收集整理的spring事务环境搭建的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。