1. Java
Code :
// 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 ); void updatefoo (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, '123 ')"; 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) {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) {// 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) CTX. getbean ("fooservice"); // fooservice. getfoo (""); Foo F1 = new Foo (); f1.setid (13); f1.setname ("Wu xx2222222222"); fooservice. insertfoo (F1); system. out. println ("done ");}}2. Configure applicationcontext. XML in spring
<? 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.sprin Gframework.org/schema/tx/spring-tx-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "& gt; <! -- This is the 1st configuration method for increasing transaction processing. 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 --> <! -- This is the 2nd configuration method for adding transaction processing <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. basicdatas Ource "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 ))
Run
Program When commenting on the transactions configured by spring, you can insert a record. If spring transactions are configured, you cannot insert records.