首页 > 编程语言 > 详细

SpringBoot——Mybatis踩坑日记

时间:2020-12-17 22:16:11      阅读:31      评论:0      收藏:0      [点我收藏+]

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   //开启日志

 

SpringBoot——Mybatis踩坑日记

原文:https://www.cnblogs.com/sheng-se/p/14151997.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!