模糊查询在mybatis配置xml文件中的写法

由于不希望在使用ssm框架时,手动添加为模糊查询添加%,对字段又修改,容易出错,所以模糊查询的%%写在xml里面。

常见写法有    拼接方式,

AND user_name like '%#{user_name}%'

这种方式如果参数为空格,查询错误

拼接方式补充

AND user_name like ”%‘#{user_name}’%“

这种方式查询不完整,

最后确定  CONCAT('%', #{user_name}, '%')  能对空格正常查询,解决上面的问题。

完整示范:

</select>


<select id="test1" parameterType="java.util.Map" resultType="java.util.Map">
select * from e_user 
<where>
<if test="user_name!=null and user_name!='' ">
and user_name like CONCAT('%', #{user_name}, '%')
</if>
</where>

</select> 

好记性不如烂笔头,万一哪天忘了不好。

猜你喜欢

转载自baobeituping.iteye.com/blog/2395812