mybatis中如何實(shí)現(xiàn)一個(gè)標(biāo)簽執(zhí)行多個(gè)sql語句
mybatis一個(gè)標(biāo)簽執(zhí)行多個(gè)sql語句
在刪除標(biāo)簽中想要添加另外三個(gè)sql實(shí)現(xiàn)主鍵重新排序的功能,這樣一個(gè) delete 標(biāo)簽里面一共是需要執(zhí)行四個(gè)sql語句,測試的時(shí)候一直報(bào)錯(cuò)說語法有問題,但在數(shù)據(jù)庫中測試sql又是成功的。
問題
其實(shí)這個(gè)語法是沒有問題的,報(bào)錯(cuò)提示語法錯(cuò)誤的主要原因是因?yàn)榕渲貌粚?duì),導(dǎo)致這樣編寫的格式在mybatis中不被承認(rèn),所以才會(huì)語法錯(cuò)誤。
解決
在配置數(shù)據(jù)源的地方添加 allowMultiQueries=true 即可,表示允許多條sql語句執(zhí)行(記得在前面使用&隔開)。
mybatis+mysql中一個(gè)標(biāo)簽內(nèi)寫兩條sql語句報(bào)錯(cuò)
<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>
報(bào)錯(cuò)如下
### 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時(shí)可以一個(gè)標(biāo)簽內(nèi)寫多條語句,只不過需要mybatis在連接mysql時(shí)在url處加上allowMultiQueries=true
如下,在末尾加上即可:
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild? useSSL=false&useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot之端口設(shè)置和contextpath的配置方式
這篇文章主要介紹了springboot之端口設(shè)置和contextpath的配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01如何把spring boot項(xiàng)目部署到tomcat容器中
本文給大家分享如何把spring boot項(xiàng)目部署到tomcat容器中,本文給大家介紹的非常詳細(xì),需要的朋友參考下2017-04-04淺談Java中向上造型向下造型和接口回調(diào)中的問題
這篇文章主要介紹了淺談Java中向上造型向下造型和接口回調(diào)中的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08Spring?Boot如何監(jiān)控SQL運(yùn)行情況?
Druid是Java語言中最好的數(shù)據(jù)庫連接池,下面這篇文章主要給大家介紹了關(guān)于Spring?Boot如何監(jiān)控SQL運(yùn)行情況的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04