教育改变生活
标题: MyBatis Like 模糊查询 [打印本页]
作者: 却尘 时间: 4 天前
标题: MyBatis Like 模糊查询
MyBatis Like 模糊查询有几种方式方式1:$ 这种方式,简单,但是无法防止SQL注入,所以不推荐使用
LIKE '%${name}%'
方式2:#
LIKE"%"#{name}"%"
方式3:字符串拼接
AND name LIKECONCAT(CONCAT('%',#{name},'%'))
方式4:bind标签
<select id="searchStudents" resultType="com.example.entity.StudentEntity" parameterType="com.example.entity.StudentEntity"> <bind name="pattern1" value="'%' + _parameter.name + '%'" />
<bind name="pattern2" value="'%' + _parameter.address + '%'" />
SELECT * FROM test_student
<where>
<if test="age != null and age != '' and compare != null and compare != ''">
age ${compare} #{age}
</if>
<if test="name != null and name != ''">
AND name LIKE #{pattern1}
</if>
<if test="address != null and address != ''">
AND address LIKE #{pattern2}
</if>
</where>
ORDER BY id
</select>
方式5:java代码里写
param.setUsername("%CD%");在 java 代码中传参的时候直接写上
<if test="username!=null"> AND username LIKE #{username}</if>
然后 mapper 里面直接写 #{} 就可以了
欢迎光临 教育改变生活 (http://bbs.goldoar.com/) |
Powered by Discuz! X3.2 |