1. 程式人生 > >MyBatis+MySql Insert操作失敗解決辦法

MyBatis+MySql Insert操作失敗解決辦法

初次接觸MyBatis,遇到資料庫使用MySql時Insert失敗的問題(select時正常)。在日誌中複製SQL語句是可以執行的,不知道什麼原因,懷疑是事務問題。

google後看到一哥們也遇到了此問題,不過他找到了一個解決辦法,下面是他原話(他的症狀和我遇到的一樣,不過我不知道哪裡有問題,他知道是資料庫引擎的問題~):

The Engine in MySql is innoDB. Which retrieves the data from the table successfully, but i cant insert the data, i cant get any exception or error.

which works fine in MyISAM type.

有他這段資訊就好辦了,下面是我的2個解決辦法:

方法1:把資料庫表都改成了MyISAM,果然資料插入成功了!

方法2:資料庫引擎不變

搜了一些資料,看到說 innoDB是自動提交事務的,而MyBatis管理事務時預設不提交。

資料地址:

openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit)方法的原型。

 看到openSession有autoCommit這個引數,那這樣呼叫(openSession(true))就OK了。測試成功!