java Spring 2.5.6 SimpleJdbcDaoSupport Transaction 事務驗證樣本

來源:互聯網
上載者:User
1.java代碼:

//Foo.javapackage x.y.service;public class Foo {private int id;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}//FooService.javapackage x.y.service;//the service interface that we want to make transactionalpublic interface FooService {Foo getFoo(String fooName);Foo getFoo(String fooName, String barName);void insertFoo(Foo foo);void updateFoo(Foo foo);}//FooServiceImpl.javapackage x.y.service;import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;import org.springframework.transaction.annotation.Transactional;public class FooServiceImpl extends SimpleJdbcDaoSupport implements FooService {@Overridepublic Foo getFoo(String fooName) {String sql = "insert into Foo values(5,'5555555555555555')";int cnt2=this.getSimpleJdbcTemplate().update(sql);System.out.println("affected2:"+cnt2);return null;}@Overridepublic Foo getFoo(String fooName, String barName) {// TODO Auto-generated method stubreturn null;}@Override@Transactionalpublic void insertFoo(Foo foo) {String sql = "insert into Foo values("+foo.getId()+",'"+foo.getName()+"')";int cnt=this.getSimpleJdbcTemplate().update(sql);int cnt2=this.getSimpleJdbcTemplate().update(sql);System.out.println("affected:"+cnt);System.out.println("affected2:"+cnt2);}@Overridepublic void updateFoo(Foo foo) {// TODO Auto-generated method stub}}//Test.javapackage x.y.service;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {/** * @param args */public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("applicatinContext.xml", Test.class);FooService fooService = (FooService) ctx.getBean("fooService");//fooService.getFoo("");Foo f1=new Foo();f1.setId(13);f1.setName("吳xx2222222222");fooService.insertFoo(f1);System.out.println("done");}}
2.Spring配置 applicationContext.xml
<?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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><!--此為增加 交易處理 的第1種配置方法 this is the service object that we want to make transactional --><!-- enable the configuration of transactional behavior based on annotations --><tx:annotation-driven transaction-manager="txManager" /><bean id="fooService" class="x.y.service.FooServiceImpl"><property name="dataSource" ref="dataSource" /></bean><!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean below) --><tx:advice id="txAdvice" transaction-manager="txManager"><!-- the transactional semantics... --><tx:attributes><!-- all methods starting with 'get' are read-only --><tx:method name="get*" read-only="true" /><!-- other methods use the default transaction settings (see below) --><tx:method name="*" /></tx:attributes></tx:advice><!-- ensure that the above transactional advice runs for any execution of an operation defined by the FooService interface --><!--  此為增加 交易處理 的第2種配置方法<aop:config><aop:pointcut id="fooServiceOperation"expression="execution(* x.y.service.FooService.*(..))" /><aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation"/> </aop:config>--><!-- don't forget the DataSource --><!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@rj-t42:1521:elvis" /> <property name="username" value="scott" /> <property name="password" value="tiger" /> </bean> --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /><property name="url"value="jdbc:sqlserver://192.168.10.10:1433;databaseName=Test" /><property name="username" value="sa" /><property name="password" value="123" /></bean><!-- similarly, don't forget the PlatformTransactionManager --><bean id="txManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><!-- other <bean/> definitions here --></beans>
3.sql
create table Foo(id bigint primary key,name nvarchar(50))
運行程式,當注釋spring配置的事務時,可以插入一條記錄,而配置好spring事務,不能插入記錄。
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.