Mybatis 模糊查询
方式一
说明: ${} 方式不会预编译,有SQL注入的风险
<select id="selectUserByName" resultMap="userAndRole">
select * from users
<where>
<if test="status != null">
user_state=#{status}
</if>
<if test="username !=null and username!=''">
and user_name like '%${username}%'
</if>
</where>
</select>
方式二
Mybatis bind 标签 + #{}
<select id="selectUserByName" resultMap="userAndRole">
select * from users
<where>
<if test="status != null">
user_state=#{status}
</if>
<if test="username !=null and username!=''">
<bind name="name" value="'%'+username+'%'" />
and user_name like #{name}
</if>
</where>
</select>
方式三
Mysql的concat + #{}
<select id="selectUserByName" resultMap="userAndRole">
select * from users
<where>
<if test="status != null">
user_state=#{status}
</if>
<if test="username !=null and username!=''">
and user_name like concat( concat('%',#{username}),'%' )
</if>
</where>
</select>

浙公网安备 33010602011771号