天天看点

记一次mybatis代码格式错误

在一次查询数据库的代码测试中,发现mybatis组装出来的sql有问题

<select id="getById" resultType="java.util.Map">
        select * from TTypeConfig
        <where>
            <if test="id != null and id != ''">
                and ID = #{id}
            </if>
            order by TypeSeq
        </where>
    </select>
           
select * from TTypeConfig WHERE order by TypeSeq
           

出现在了where后面没有查询条件的勤快,明明用的是<where>标签,说好会自动识别添加或者去除的

然后后面发现是我蠢了,语法写错了

把order by 写到了<where>标签里面去了,这样mybatis识别<where>标签中有内容就在sql中把WHERE加上了,然后就导致查询出错了

写下来,防止以后自己再犯