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)