SpringBoot——Mybatis踩坑日记

20.12.17——动态SQL报错

问题描述:手动写了一段SQL如下,控制台报错,找不到原因

 

   <select id="mybatisQueryBrandByPage" resultType="Brand">
        SELECT id,name,image,letter FROM tb_brand
        <where>
            <if test="key!=null and key!=''">
                name like concat("%",#{key},"%") or letter =#{key}
            </if>
            <if test="sortBy!=null and sortBy!=''">
               ORDER BY ${sortBy}
            </if>
            <choose>
               <when test="desc==true"> DESC</when>
               <otherwise> ASC</otherwise>
            </choose>
        </where>
       
    </select>              

问题原因:

key为空时会生成如下SQL,SELECT id,name,image,letter FROM tb_brand WHERE OREDER BY.......

解决方法:

配置yml文件,开启mybatis日志,查看mybatis生成的SQL和执行的细节,找到问题原因,配置如下:

mybatis:
  type-aliases-package: com.leyou.*.pojo
  mapper-locations: classpath*:mapper/*Mapper.xml
  configuration:
    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl   //开启日志

 

posted @ 2020-12-17 21:17  别无所求---  阅读(140)  评论(0编辑  收藏  举报