天天看點

記一次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加上了,然後就導緻查詢出錯了

寫下來,防止以後自己再犯