iBatis 动态SQL (Dynamic SQL)

英文来自此处

3.9. Dynamic SQL

在ADO里常常遇到的问题就是动态SQL拼接。典型的解决方案是通过if-else语句拼接字符串。iBATIS DataMapper API则提供了一个相对优雅的方式来映射元素。看这个例子:

<select id="dynamicGetAccountList" cacheModel="account-cache" parameterClass="Account" resultMap="account-result" >
  select * from ACCOUNT
    <isGreaterThan prepend="and" property="Id" compareValue="0">
       where ACC_ID = #Id#
    </isGreaterThan>
  order by ACC_LAST_NAME
</select>

当参数Id > 0的场合,最终的SQL是:

select * from ACCOUNT where ACC_ID = ? order by ACC_LAST_NAME

当参数Id <= 0的场合,最终的SQL是:

select * from ACCOUNT order by ACC_LAST_NAME

以下面这个例子说明一下动态的标签:

<statement id="someName" parameterClass="Account" resultMap="account-result" >
  select * from ACCOUNT
  <dynamic prepend="where">
    <isGreaterThan prepend="and" property="id" compareValue="0">
      ACC_ID = #id#
    </isGreaterThan>
    <isNotNull prepend="and" property="lastName">
      ACC_LAST_NAME = #lastName#
    </isNotNull>
  </dynamic>
order by ACC_LAST_NAME
</statement>
</select>
idlastName结果
1nullselect * from ACCOUNT where ACC_ID = #id# order by ACC_LAST_NAME
1not nullselect * from ACCOUNT where ACC_ID = #id# and ACC_LAST_NAME = #lastName# order by ACC_LAST_NAME (子句的and被where覆盖了)
0nullselect * from ACCOUNT order by ACC_LAST_NAME
0not nullselect * from ACCOUNT where ACC_LAST_NAME = #lastName# order by ACC_LAST_NAME

二元标签属性

属性
prepend – 直接加到SQL语句中,可被覆盖 (可选项)
property – 被比较属性 (必须项)
compareProperty – 要比较的对象(可选项)
compareValue – 要比较的值 (可选项)

二元标签

这里写图片描述


一元标签属性

属性
prepend – 直接加到SQL语句中,可被覆盖 (可选项)
property – 被比较属性 (必须项)

一元标签

这里写图片描述


判空标签属性

属性
prepend – 直接加到SQL语句中,可被覆盖 (可选项)

判空标签

这里写图片描述


迭代标签属性

属性
prepend – 直接加到SQL语句中,可被覆盖 (可选项)
property – 实现了IList接口的可被迭代属性 (必须项)
open – 开始符,通常是左括号 (可选项)
close – 结束符,通常是右括号 (可选项)
conjunction – 迭代结果的连接符, 通常是AND and OR (可选项)

迭代标签

这里写图片描述
(注意:不要遗漏#UserNameList[]#里面的方括号)


  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 C# 代码实现 IBatis.NET 动态批量更新数据的示例: 1. 定义 SQL 语句 在 SQL Mapper 文件中定义更新语句,参考上面的示例,并加入动态 SQL。 ```xml <update id="updateBatch"> update user <dynamic prepend="set"> <isNotEmpty prepend="," property="name"> name = #name# </isNotEmpty> <isNotEmpty prepend="," property="age"> age = #age# </isNotEmpty> <isNotEmpty prepend="," property="address"> address = #address# </isNotEmpty> </dynamic> where id in <foreach open="(" close=")" separator="," collection="list" item="item"> #{item.id} </foreach> </update> ``` 其中,动态 SQL 部分根据传入的参数来决定是否更新对应的字段。 2. 编写代码 ```csharp List<User> userList = new List<User>(); // 假设有三条数据需要更新 userList.Add(new User { id = 1, name = "John", age = 25, address = "New York" }); userList.Add(new User { id = 2, name = "Lucy", age = 28, address = "London" }); userList.Add(new User { id = 3, name = "Tom", age = 30 }); // 获取 SqlMapper 实例 ISqlMapper sqlMapper = Mapper.Instance(); // 开始事务 sqlMapper.BeginTransaction(); try { // 调用 SQL 语句更新数据 sqlMapper.Update("updateBatch", userList); // 提交事务 sqlMapper.CommitTransaction(); } catch(Exception ex) { // 回滚事务 sqlMapper.RollBackTransaction(); throw ex; } ``` 其中,User 类为要更新的数据类型,Mapper.Instance() 获取 SqlMapper 实例,updateBatch 为定义的 SQL 语句 ID。在 try-catch 块中执行 SQL 语句,如果出现异常则回滚事务,否则提交事务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值