zoukankan      html  css  js  c++  java
  • IBatisNet:让insert操作返回新增记录的主键值

    项目引用ibatis包:

    IBatisNet.Common.dll --文件版本1.6.2.0
    IBatisNet.DataAccess.dll
    IBatisNet.DataMapper.dll

    项目目录结构:

    项目中使用ibatis做数据访问层已经有好长时间了。开发小组成员反映ibatis的insert操作返回的结果是null,这一点很是不爽。 其实,大家都是希望能够把新增记录的主键值返回出来。 上上周,大家有反编译ibatis的包,查看其实现原理,后来,又尝试其他方法,都没能给实现这个功能。

    这两天,决定找点时间,来搞定这个头疼的事儿。

    结合网上搜到的东东,总结出方案是通过insert语句的selectKey子元素来返回insert命令生成的记录的主键值。

    如下是xml映射文件里insert节点:

    <insert id="InsertEntity"  parameterclass="T_Ent_Project">
      insert into  T_Ent_Project(EntId,ProjectNo,ProjectName,BudgetAmount,LockedAmount,UsedAmount,AvailableAmount,Active,CreatedBy,CreatedTime,ModifiedBy,ModifidTime )
      values(  #EntId#, #ProjectNo#, #ProjectName#, #BudgetAmount#, #LockedAmount#, #UsedAmount#, #AvailableAmount#, #Active#, #CreatedBy#, #CreatedTime#, #ModifiedBy#, #ModifidTime# )
    
      <!--通过insert语句的selectKey子元素来返回insert命令生成的记录的主键值-->
      <selectKey resultClass="int" type="post" property="ProjectId">
        select @@IDENTITY
      </selectKey>
    </insert>

    需要注意的是,上面的select @@IDENTITY是相对于MsSql来讲的,如果数据库是mysql或oracle,会有不同。

    另外,当selectKey配置不当,运行程序会出现类似如下异常:

    • Unknown selectKey type : ''
    • There is no Set member named '' in class 'T_Ent_Project'

    如下是对ibatis做的crud的测试类:

    using IBatisDemo.Domains;
    using IBatisNet.DataMapper;
    using IBatisNet.DataMapper.Configuration;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System;
    
    namespace IBatisDemo
    {
        [TestClass]
        public class IbatisCRUDTest
        {
            [TestMethod]
            public void TestCRUD()
            {
                // 初始化sqlmapper对象
                DomSqlMapBuilder builder = new DomSqlMapBuilder();//bin\
                string path = System.AppDomain.CurrentDomain.BaseDirectory + /*"bin\" +*/   "\SqlMap.config";
                ISqlMapper mapper = builder.Configure(path);
    
                // 定义实体对象
                T_Ent_Project model = new T_Ent_Project()
                {
                    ProjectName = "test mybatis",
                    EntId = 0,
                    CreatedTime = DateTime.Now
                };
    
                // insert 
                var ret = mapper.Insert("MyBatis_T_Ent_Project.InsertEntity", model);
                int newId = Convert.ToInt32(ret);
                Console.WriteLine("insert后的记录的主键值:" + newId);
    
                // select 
                var newModel = mapper.QueryForObject<T_Ent_Project>("MyBatis_T_Ent_Project.getByKey", newId);
                Assert.AreEqual(newId, newModel.ProjectId);
    
                //Update 和 Delete 都返回受影响的行数。
                model.ProjectId = newId;
                model.ProjectName += "updated";
                int updRowCount = mapper.Update("MyBatis_T_Ent_Project.UpdateEntity", model);
                Console.WriteLine("update影响行数:" + updRowCount);
    
                int delRet = mapper.Delete("MyBatis_T_Ent_Project.DeleteEntity", model.ProjectId);
                Console.WriteLine("delete影响行数:" + delRet);
            }
        }
    }

    测试返回结果:

  • 相关阅读:
    使用S7netplus存取西门子PLC字符串数据
    学习使用Nginx配置服务器
    Bootstrap4设置footer固定在底部
    ASP.NET Core MVC项目Razor页面实时编译
    在Asp.NET Core MVC项目中通过Libman安装Bootstrap
    在Asp.Net Core Web项目中使用不同的环境
    C#简单使用System.Threading.Timer
    在ASP.Net Core Web API中使用Swagger进行版本控制
    ASP.Net Core Web API解决跨域问题
    LeetCode刷题-- 搜索插入位置
  • 原文地址:https://www.cnblogs.com/buguge/p/5462914.html
Copyright © 2011-2022 走看看