mybatis中如何實現(xiàn)一個標簽執(zhí)行多個sql語句
mybatis一個標簽執(zhí)行多個sql語句
在刪除標簽中想要添加另外三個sql實現(xiàn)主鍵重新排序的功能,這樣一個 delete 標簽里面一共是需要執(zhí)行四個sql語句,測試的時候一直報錯說語法有問題,但在數(shù)據(jù)庫中測試sql又是成功的。
問題
其實這個語法是沒有問題的,報錯提示語法錯誤的主要原因是因為配置不對,導(dǎo)致這樣編寫的格式在mybatis中不被承認,所以才會語法錯誤。
解決
在配置數(shù)據(jù)源的地方添加 allowMultiQueries=true 即可,表示允許多條sql語句執(zhí)行(記得在前面使用&隔開)。
mybatis+mysql中一個標簽內(nèi)寫兩條sql語句報錯
<update id="bindStu"> update edu_student set user_username=#{user_username} where stu_id = #{stu_id}; update edu_user set stu_id=#{stu_id} where user_username=#{user_username}; </update>
報錯如下
### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'update edu_user set stu_id='2' where user_username='111'' at line 2
### The error may exist in com/yue/mapper/UserMapper.xml ### The error may involve
defaultParameterMap ### The error occurred while setting parameters ### SQL: update
edu_student set user_username=? where stu_id = ?; update edu_user set stu_id=? where
user_username=?; ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
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 'update edu_user set stu_id='2' where
user_username='111'' at line 2 ; bad SQL grammar []; nested exception is
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'update edu_user set stu_id='2' where user_username='111'' at line 2
經(jīng)查閱資料后發(fā)現(xiàn)
mybatis+mysql時可以一個標簽內(nèi)寫多條語句,只不過需要mybatis在連接mysql時在url處加上allowMultiQueries=true
如下,在末尾加上即可:
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild? useSSL=false&useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot之端口設(shè)置和contextpath的配置方式
這篇文章主要介紹了springboot之端口設(shè)置和contextpath的配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01淺談Java中向上造型向下造型和接口回調(diào)中的問題
這篇文章主要介紹了淺談Java中向上造型向下造型和接口回調(diào)中的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08