How to write fuzzy query in mybatis configuration xml file

Since I do not want to manually add % for the fuzzy query when using the ssm framework, and modify the fields, it is easy to make mistakes, so the %% of the fuzzy query is written in xml.

Common writing methods include splicing,

AND user_name like '%#{user_name}%'

In this way, if the parameter is a space, the query is wrong

Supplementary splicing

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

This way the query is not complete,

Finally, make sure that CONCAT('%', #{user_name}, '%') can query the space normally and solve the above problem.

Full demo:

</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> 

A good memory is not as good as a bad writing, in case one day you can't forget it well.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326200239&siteId=291194637