1. 参数中直接加入%%
param.setUsername("%string%");
param.setPassword("%11%");
<select id="selectPersons" resultType="user" parameterType="user"> select id,sex,age,username,password from person where true <if test="username!=null"> AND username LIKE #{username}</if> <if test="password!=null">AND password LIKE #{password}</if> </select>
2. bind标签
<select id="selectPersons" resultType="user" parameterType="user"> <bind name="pattern" value="'%' + _parameter.username + '%'" /> select id,sex,age,username,password from person where username LIKE #{pattern} </select>
3. CONCAT
where username LIKE concat(concat('%',#{username}),'%')
4、经过搜索,发现要加上一个concat字段,要先将字符串拼接后,才能实现模糊查询。
改成这个样子后,模糊查询功能实现。
5、 <select id="findByTitle" parameterType="String" resultType="MessPublish"> select ID,Title,MessContent,CreateTime,TypeID,CorpID,UserName,UserID,ModifyTime,CheckState,CheckTime,CheckID,CheckRemark,MessFujian from Mes_publish <where> <if test="title!=null and title!='' "> and Title like '%'+#{title}+'%' </if> </where> </select> <select id="findNotPassTitle" parameterType="String" resultType="MessPublish"> select ID,Title,MessContent,CreateTime,TypeID,CorpID,UserName,UserID,ModifyTime,CheckState,CheckTime,CheckID,CheckRemark,MessFujian from Mes_publish <where> <if test="title!=null and title!='' "> and Title like '%'+#{title}+'%' </if> and checkState in (0,2); </where> </select>