Mybatis執(zhí)行update失敗的解決
Mybatis執(zhí)行update失敗
今天在進行分布式重構(gòu)項目的時候碰到一個問題,在執(zhí)行sql的時候只有update不能成功,其他語句都可以正常執(zhí)行,報錯如下: 版本:org.mybatis:mybatis:3.4.5
接口
@UpdateProvider(type = ManagerProvider.class, method = "updateManager") int updateManager(Manager manager) throws Exception;
報錯信息
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的時候都默認生成key,然后注入,所以在執(zhí)行update的時候會報錯。
解決辦法
在接口上增加注解 @Options(useGeneratedKeys = false),不讓mybatis自動注入key,問題解決。
@UpdateProvider(type = ManagerProvider.class, method = "updateManager") @Options() // @Options(useGeneratedKeys = false) int updateManager(Manager manager) throws Exception;
最后說明一點,版本:org.mybatis:mybatis:3.4.4這個版本沒有此類問題。
Mybatis插入(更新)失敗 卻不報錯
問題描述
mybatis進行數(shù)據(jù)插入或更新操作,方法成功執(zhí)行,數(shù)據(jù)庫中卻不存在新數(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();
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
JAVA簡單工廠模式(從現(xiàn)實生活角度理解代碼原理)
本文主要介紹了JAVA簡單工廠模式(從現(xiàn)實生活角度理解代碼原理)的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03
Jenkins Host key verification failed問題解決
這篇文章主要介紹了Jenkins Host key verification failed問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-11-11
SpringBoot使用Mybatis-Generator配置過程詳解
這篇文章主要介紹了SpringBoot使用Mybatis-Generator配置過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-02-02
spring boot之SpringApplication 事件監(jiān)聽
這篇文章主要介紹了spring boot之SpringApplication 事件監(jiān)聽,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03

