SpringBoot——Mybatis踩坑日記
阿新 • • 發佈:2020-12-17
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 //開啟日誌