在做查询时,我们一般会有使用like需求
例如:
1、使用$符号:它可以进行拼接,但会有sql注入的问题
select id,name,gender,email from emp <where> <if test="id != null and id != ''"> and id = #{id} </if> <if test="name != null and name != ''"> and name like '%${name}%' </if> <where>
2、在传入name属性,就设置为‘%李白%’,然后使用#符号
select id,name,gender,email from emp <where> <if test="id != null and id != ''"> and id = #{id} </if> <if test="name != null and name != ''"> and name like #{name} </if> <where>
3、使用mybatis的bind标签
select id,name,gender,email from emp <bind name="_name" value="'%'+name+'%'"></bind> <where> <if test="id != null and id != ''"> and id = #{id} </if> <if test="name != null and name != ''"> and name like #{_name} </if> </where>