Ibatis插入数据库并获取插入的数据ID

使用的是 IBatis.DataMapper.1.6.2

首先看下映射文件配置:

<insert id="InsertProject" resultClass="IStrong.Config.Tool.Models.ProjectModel">
  insert into STRONG_Project (name, areacode, customer,         remark)values(#Name#,#AreaCode#,#Customer#,#Remark#)
      <selectKey type="post" resultClass="int" property="Id"> 
           SELECT CAST(@@IDENTITY as int) as Id
      </selectKey>
</insert>



然后是我们的代码:

public object CreateProject(ProjectModel project)
{
       ISqlMapper instance = Mapper.Instance();
       int id = (int)instance.Insert("InsertProject", new ProjectModel
      {
         Name = project.Name,
         Remote = project.Remote,
         Remark = project.Remark,
         AreaCode = project.AreaCode,
         Customer = project.Customer
       });
       return id;
}



配置里面的   SELECT CAST(@@IDENTITY as int) as Id 这个Id必须是代码里面的ProjectModel类的一个属性, instance.Insert() 方法里的第二个参数不能使匿名类, 除非不需要查找你插入的ID。


文章链接: http://www.wyfans.com/1234.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值