问答详情
源自:4-2 动态 SQL 拼接(下)

mybatis 模糊查询like问题

映射文件中的like '%' #{symptomName} '%' 就报语法错误

<if test="symptomName != null and !&quot;&quot;.equals(symptomName.trim())">
   and symptom_name like '%' #{symptomName} '%'
</if>



Caused by: java.sql.SQLException: sql injection violation, syntax error: syntax error, error in :'name like '%' ? '%') tmp_count', expect RPAREN, actual QUES pos 174, line 6, column 39, token QUES : select count(0) from (select
         
        symptom_code,symptom_name,synonym
     
        from  western_medicine_symptone_synonym
         WHERE  symptom_name like '%' ? '%') tmp_count

提问者:慕仙4974986 2018-03-04 15:06

个回答

  • 情不知所起丶
    2018-05-29 11:10:35
    已采纳

    改成这样  and symptom_name like  concat('%' ,#{symptomName}, '%')

  • 律动的小刘海_
    2018-03-05 21:33:35

    你把下面一段放到Mysql中运行试一下。不过为什么要用select count from呢

    select count(0) from (select
             
            symptom_code,symptom_name,synonym
         
            from  western_medicine_symptone_synonym
             WHERE  symptom_name like '%' ? '%') tmp_count