MyBatis_动态SQL与模糊查询(查询d_user表的数据):
d_user表:

User的bean:

userMapper.xml
<!--动态sql和模糊查询 如果name不为空的话才要这样name like #{name} and -->
<select id="getUser" parameterType="ConditionUser" resultType="User">
select * from d_user where
<if test='name != "%null%"'>
name like #{name} and
</if>
age between #{minAge} and #{maxAge}
</select>
测试用例:

查询结果:
当name = "o"时,符合条件的只有一个;
[User [id=2, name=Bob, age=13]]
当name = null时,符合条件的有两个;
[User [id=2, name=Bob, age=13], User [id=3, name=Jack, age=18]]
本文介绍在MyBatis中使用动态SQL进行条件筛选和模糊查询的方法,通过userMapper.xml中的SQL片段展示了如何根据参数动态构建SQL语句,实现对d_user表的灵活查询。
3910

被折叠的 条评论
为什么被折叠?



