數據庫表中存在特殊符號時,通過 mybatis 進行更新操作可能會遇到問題。例如,如果將特殊符號作為參數值傳入,可能會觸發語法錯誤。
例如,以下 mybatis 更新語句可能會導致問題:
<update id="update"> update d_table set separator = #{separator, jdbctype=varchar} where id = #{id, jdbctype=bigint} </update>
如果 separator 傳入的值是特殊符號,如 ? 或 !,則會引發錯誤。
### error updating database. cause: java.sql.sqlsyntaxerrorexception: you have an error in your sql syntax; check the manual that corresponds to your mysql server version for the right syntax to use near 'separator = '!' ...
解決方案
問題在于,separator 本身是一個關鍵詞。解決方法是使用反引號(”)將列名括起來,從而避免關鍵字沖突:
<update id="update"> UPDATE d_table SET `separator` = #{separator, jdbcType=VARCHAR} WHERE id = #{id, jdbcType=BIGINT} </update>
使用反引號后,即使參數值包含特殊符號,也能正常更新數據庫。
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END