Mybatis執(zhí)行update失敗的解決
Mybatis執(zhí)行update失敗
今天在進(jìn)行分布式重構(gòu)項(xiàng)目的時(shí)候碰到一個(gè)問題,在執(zhí)行sql的時(shí)候只有update不能成功,其他語句都可以正常執(zhí)行,報(bào)錯(cuò)如下: 版本:org.mybatis:mybatis:3.4.5
接口
@UpdateProvider(type = ManagerProvider.class, method = "updateManager") int updateManager(Manager manager) throws Exception;
報(bào)錯(cuò)信息
Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase, Hana]
org.springframework.jdbc.UncategorizedSQLException: Error getting generated key or setting result to parameter object. Cause: java.sql.SQLException: 不允許的操作;uncategorized SQLException for SQL []; SQL state [99999]; error code [17090]; 不允許的操作; nested exception is java.sql.SQLException: 不允許的操作
原因
由于mybatis在執(zhí)行insert和update的時(shí)候都默認(rèn)生成key,然后注入,所以在執(zhí)行update的時(shí)候會(huì)報(bào)錯(cuò)。
解決辦法
在接口上增加注解 @Options(useGeneratedKeys = false),不讓mybatis自動(dòng)注入key,問題解決。
@UpdateProvider(type = ManagerProvider.class, method = "updateManager") @Options() // @Options(useGeneratedKeys = false) int updateManager(Manager manager) throws Exception;
最后說明一點(diǎn),版本:org.mybatis:mybatis:3.4.4這個(gè)版本沒有此類問題。
Mybatis插入(更新)失敗 卻不報(bào)錯(cuò)
問題描述
mybatis進(jìn)行數(shù)據(jù)插入或更新操作,方法成功執(zhí)行,數(shù)據(jù)庫(kù)中卻不存在新數(shù)據(jù),原本代碼如下:
@Test
public void test7() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
EmployeeMapperDynamicSql mapper = sqlSession.getMapper(EmployeeMapperDynamicSql.class);
Employee employee = new Employee(null,"gfdhg","456dfgngh@qq.com","female",null);
mapper.addEmps(Collections.singletonList(employee));
sqlSession.close();
}
解決方案
在插入或更新語句后,增添commit,代碼如下:
@Test
public void test7() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
EmployeeMapperDynamicSql mapper = sqlSession.getMapper(EmployeeMapperDynamicSql.class);
Employee employee = new Employee(null,"gfdhg","456dfgngh@qq.com","female",null);
mapper.addEmps(Collections.singletonList(employee));
/*添加commit*/
sqlSession.commit();
sqlSession.close();
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
JAVA簡(jiǎn)單工廠模式(從現(xiàn)實(shí)生活角度理解代碼原理)
本文主要介紹了JAVA簡(jiǎn)單工廠模式(從現(xiàn)實(shí)生活角度理解代碼原理)的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03
Jenkins Host key verification failed問題解決
這篇文章主要介紹了Jenkins Host key verification failed問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
java讀取某個(gè)文件夾下的所有文件實(shí)例代碼
這篇文章主要介紹了java讀取某個(gè)文件夾下的所有文件實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03
SpringBoot使用Mybatis-Generator配置過程詳解
這篇文章主要介紹了SpringBoot使用Mybatis-Generator配置過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
spring boot之SpringApplication 事件監(jiān)聽
這篇文章主要介紹了spring boot之SpringApplication 事件監(jiān)聽,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-03-03

