首页 技术 正文
技术 2022年11月10日
0 收藏 625 点赞 3,625 浏览 2203 个字

项目引用ibatis包:

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

项目目录结构:

IBatisNet:让insert操作返回新增记录的主键值

项目中使用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 = ,
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);
}
}
}

测试返回结果:

IBatisNet:让insert操作返回新增记录的主键值

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,495
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,909
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,741
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,496
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,134
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298