动态 SQL ——之—— bind

本文深入探讨了MyBatis中的bind元素用法,通过具体的SQL示例展示了如何使用bind元素来动态创建变量并绑定到SQL上下文中,适用于不同的查询条件。

bind 元素可以从 OGNL 表达式中创建一个变量并将其绑定到上下文

此博文 <深入了解MyBatis参数,深入了解mybatis> 解释的相当到位,谢谢博主

另外博主的博客写的也是很好的。有机会多多进去学习 博文地址

 

此博文 《关于mybatis like》,作者在提供了解决方案(我没有遇到过)。

SELECT * FROM TNotific
<where>
	<if test="method != null">
		<bind name="pattern" value="'%' + method + '%'"/>
		and Method like #{pattern}
	</if>
	<if test="statusOfread != null">
		and StatusOfread = #{statusOfread,jdbcType=VARCHAR}
	</if>
	<if test="bizCaseId != null">
		and BizCaseId = #{bizCaseId,jdbcType=VARCHAR}
	</if>
</where>

入参

String statusOfread = "0";    String method = "U";   String bizCaseId = null;

执行SQL

SELECT * FROM TNotific WHERE Method like ? and StatusOfread = ? 
==> Parameters: %U%(String), 0(String)


SELECT * FROM TNotific 
<where>
	<if test="tNotific.method != null">
		<bind name="pattern" value="'%' + tNotification.method + '%'"/>
		and Method like #{pattern}
	</if>
	<if test="tNotific.statusOfRead != null">
		and StatusOfread = #{tNotific.statusOfRead,jdbcType=VARCHAR}
	</if>
	<if test="tNotification.bizCaseId != null">
		and BizCaseId = #{tNotific.bizCaseId,jdbcType=VARCHAR}
	</if>
</where>

入参

TNotific tNotific = new TNotific();
	tNotific.setMethod("U");
	tNotific.setStatusOfRead("0");
	tNotific.setBizCaseId("10000000012028");

SQL 执行结果

SELECT * FROM TNotific WHERE Method like ? and StatusOfread = ? and BizCaseId = ? 
==> Parameters: %U%(String), 0(String), 10000000012028(String)

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值