在做查询时,我们一般会有使用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>

 

相关文章:

  • 2021-05-10
  • 2021-09-14
  • 2021-12-18
  • 2021-08-07
  • 2022-12-23
  • 2021-11-08
  • 2022-12-23
  • 2021-11-23
猜你喜欢
  • 2021-06-03
  • 2022-01-24
  • 2022-12-23
  • 2022-01-07
  • 2022-12-23
  • 2021-07-09
相关资源
相似解决方案