最近没有怎么更新博客,我有罪!
最近用MyBatis做ORM层,所以记录下一些新的知识。
官方推荐的做法是使用bind
<if test="condition.hospitalName != null and condition.hospitalName!= ''">
<bind name="pattern" value="'%' + condition.hospitalName + '%'"/>
and e.hospital_name like #{pattern,jdbcType=VARCHAR}
</if>
可以通过如下表达式进行更严格的判定
<if test="condition.title != null and condition.title != ''">
and e.title = #{condition.title,jdbcType=VARCHAR}
</if>
<if test="condition.adeptIdList != null and condition.adeptIdList.size() != 0">
</if>
业务中有几个and
也有几个or
条件,大概的sql如下
SELECT *
FROM exp_expert e
INNER JOIN exp_expert_adept ea
ON e.id = ea.expert_id
WHERE e.as_delete = 0 AND e.expert_name LIKE '%expert%' AND e.title = 'title' AND (ea.adept_id = 1 OR ea.adept_id = 2 OR ea.adept_id = 3);
最后的OR
添加如此写:
<if test="condition.adeptIdList != null and condition.adeptIdList.size() != 0">
and
<foreach item="item" index="index" collection="condition.adeptIdList"
open="(" separator="or" close=")">
ea.adept_id = #{item}
</foreach>
</if>
活学活用了separator
总结完成,完结撒花!
最后推荐一个插件:MyBatisCodeHelper
,功能用起来太爽了。