方法1:在其它地方對其進行相關處理,語句與正常的查詢無異
在sqlMap中與正常的無異,如下所示:
<select id="getNovaUserInfoByNickLike" resultMap="novaUserInfoMap">
??<include refid="selectNovaUserInfoAll"/>??
?? where
??nick like #{nick} order by createdAt desc?
?</select>
?
public List<NovaUserInfo> getNovaUserInfoByNickLike(@Param(value = "nick")String nick);
在Dao的接口中與正常查詢無異:
public List<NovaUserInfo> getNovaUserInfoByNickLike(String nick);
在Dao的實現中要做一些簡單的操作:
@Override
public List<NovaUserInfo> getNovaUserInfoByNickLike(String nick,PageQuery pageQuery) {
??return this.novaUserInfoMapper.getNovaUserInfoByNickLike("%"+nick+"%");
?}
?
ibatis中配置模糊查詢:
<select id="getNovaUserInfoByNickLike" resultMap="novaUserInfoMap">
??<include refid="selectNovaUserInfoAll"/>??
?? where
??nick like '%$sname$%' order by createdAt desc?
?</select>
?
select sname,sid from student where sname like '%$sname$%';
?
方法2:直接在sql語句中寫,其它的不變
第一種方法
<select id="selectPersons" resultType="person" parameterType="person">
? <bind name="pattern" value="'%' + _parameter.username + '%'" />
? select id,sex,age,username,password?
? from person
? where username LIKE #{pattern}
</select>
?
第二種方法
<select id="getNovaUserInfoByNickLike" resultMap="novaUserInfoMap">
??<include refid="selectNovaUserInfoAll"/>??
?? where
??nick like CONCAT('%','?#{nick}','%'?) order by createdAt desc?
?</select>