标签:
操作系统:Win7
编译器:VS2010
Spring.Core.dll
Spring.Data.dll
Common.Logging.dll
namespace Dao { public interface IAccountDao { void Create(string name, string userName); void Delete(string userName); } }
namespace Dao { public interface IUserDao { void Create(string name, int age); void Delete(string name); DataSet Get(string name); } }
namespace Dao { public class AccountDao : AdoDaoSupport, IAccountDao { public void Create(string name, string userName) { AdoTemplate.ExecuteNonQuery(CommandType.Text, String.Format("INSERT INTO T_Account (UserName, AccountName) VALUES (‘{0}‘, ‘{1}‘)", userName, name)); } public void Delete(string userName) { AdoTemplate.ExecuteNonQuery(CommandType.Text, String.Format("DELETE FROM T_Account WHERE UserName = ‘{0}‘", userName)); } } }
namespace Dao { public class UserDao : AdoDaoSupport, IUserDao { public void Create(string name, int age) { AdoTemplate.ExecuteNonQuery(CommandType.Text, string.Format("INSERT INTO T_User (UserName, UserAge) VALUES (‘{0}‘, {1})", name, age)); } public void Delete(string name) { AdoTemplate.ExecuteNonQuery(CommandType.Text, string.Format("DELETE FROM T_User WHERE UserName = ‘{0}‘", name)); } public DataSet Get(string name) { return AdoTemplate.DataSetCreate(CommandType.Text, string.Format("SELECT * FROM T_User WHERE UserName = ‘{0}‘", name)); } } }
namespace Service { public interface IUserService { void SaveData(string name, int age, string accountName); void DeleteData(string name); DataSet Get(string name); } }
namespace Service { public class UserService : IUserService { public IUserDao UserDao { get; set; } public IAccountDao AccountDao { get; set; } [Transaction] public void SaveData(string name, int age, string accountName) { UserDao.Create(name, age); AccountDao.Create(accountName, name); } [Transaction] public void DeleteData(string name) { UserDao.Delete(name); throw new Exception("测试数据是否回滚"); AccountDao.Delete(name); } [Transaction(ReadOnly = true)] public DataSet Get(string name) { return UserDao.Get(name); } } }
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="spring"> <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/> <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/> <section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core"/> </sectionGroup> </configSections> <spring> <parsers> <parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data"/> <parser type="Spring.Transaction.Config.TxNamespaceParser, Spring.Data"/> </parsers> <context> <!--<resource uri="assembly://Dao/Dao/Dao.xml"/>--> <resource uri="config://spring/objects"></resource> </context> <objects xmlns="http://www.springframework.net" xmlns:db="http://www.springframework.net/database" xmlns:tx="http://www.springframework.net/tx"> <db:provider id="DbProvider" provider="SqlServer-1.1" connectionString="Server=(local);Database=CIS;Uid=sa;Pwd=woaini;Trusted_Connection=False"/> <object id="userDao" type="Dao.UserDao, Dao"> <property name="AdoTemplate" ref="adoTemplate"/> </object> <object id="accountDao" type="Dao.AccountDao, Dao"> <property name="AdoTemplate" ref="adoTemplate"/> </object> <object id="userService" type="Service.UserService, Service"> <property name="UserDao" ref="userDao"/> <property name="AccountDao" ref="accountDao"/> </object> <object id="adoTemplate" type="Spring.Data.Core.AdoTemplate, Spring.Data"> <property name="DbProvider" ref="DbProvider"/> <property name="DataReaderWrapperType" value="Spring.Data.Support.NullMappingDataReader, Spring.Data"/> </object> <!--事务管理器--> <object id="transactionManager" type="Spring.Data.Core.AdoPlatformTransactionManager, Spring.Data"> <property name="DbProvider" ref="DbProvider"/> </object> <!--事务切面--> <tx:attribute-driven/> </objects> </spring> </configuration>
namespace SpringNetTrans { class Program { static void Main(string[] args) { AdoTransaction(); Console.ReadKey(); } public static void AdoTransaction() { IApplicationContext ctx = ContextRegistry.GetContext(); IUserService service = (IUserService)ctx.GetObject("userService"); service.SaveData("Kimisme", 27, "1233456"); } public static void AdoDeleteTransaction() { IApplicationContext ctx = ContextRegistry.GetContext(); IUserService service = (IUserService)ctx.GetObject("userService"); service.DeleteData("Kimisme"); } } }
标签:
原文地址:http://www.cnblogs.com/2star/p/5350930.html