码迷,mamicode.com
首页 > 编程语言 > 详细

Spring.Net学习笔记(7)-事务

时间:2016-04-03 23:37:35      阅读:546      评论:0      收藏:0      [点我收藏+]

标签:

一、开发环境

操作系统:Win7

编译器:VS2010

二、涉及程序集

Spring.Core.dll

Spring.Data.dll

Common.Logging.dll

三、开发过程

1.项目结构

技术分享

2.IAccountDao.cs

namespace Dao
{
    public interface IAccountDao
    {
        void Create(string name, string userName);

        void Delete(string userName);
    }
}

3.IUserDao.cs

namespace Dao
{
    public interface IUserDao
    {
        void Create(string name, int age);

        void Delete(string name);

        DataSet Get(string name);
    }
}

4.AccountDao.cs

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));
        }
    }
}

5.UserDao.cs

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));
        }
    }
}

6.IUserService.cs

namespace Service
{
    public interface IUserService
    {
        void SaveData(string name, int age, string accountName);

        void DeleteData(string name);

        DataSet Get(string name);
    }
}

7.UserService.cs

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);
        }
    }
}

8.App.config

技术分享
<?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>
View Code

9.控制台文件

技术分享
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");
        }
    }
}
View Code

Spring.Net学习笔记(7)-事务

标签:

原文地址:http://www.cnblogs.com/2star/p/5350930.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!