Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the failure of Mybatis to execute update

Shulou Source: shulou.com Published: 2022-06-03 19:59:40 09月20日 Update

This article introduces the relevant knowledge of "how to solve the failure of Mybatis to implement update". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Mybatis failed to execute update

Today, when I was working on a distributed refactoring project, I encountered a problem. When executing sql, only update can not succeed. Other statements can be executed normally. The error is as follows: version: org.mybatis:mybatis:3.4.5

Interface @ UpdateProvider (type = ManagerProvider.class, method = "updateManager") int updateManager (Manager manager) throws Exception

Error message

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: disallowed operation

Uncategorized SQLException for SQL []; SQL state [99999]; error code [17090]; disallowed operation; nested exception is java.sql.SQLException: disallowed operation

Reason

Because mybatis generates key by default when executing both insert and update, and then injects it, it will report an error when executing update.

Solution.

Add @ Options (useGeneratedKeys = false) to the interface to prevent mybatis from automatically injecting key, and the problem is solved.

@ UpdateProvider (type = ManagerProvider.class, method = "updateManager") @ Options () / / @ Options (useGeneratedKeys = false) int updateManager (Manager manager) throws Exception

Finally, the version: there is no such problem with this version of org.mybatis:mybatis:3.4.4.

Mybatis insert (update) failed but did not report error description of the problem

Mybatis performs data insertion or update operation, and the method executes successfully, but there is no new data in the database. The original code is as follows:

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 ();} solution

After inserting or updating the statement, add commit with the following code:

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)); / * add commit*/ sqlSession.commit (); sqlSession.close () } this is the end of the content of "how to solve the failure of Mybatis to execute update". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Time problems data versions updates success code content interfaces more knowledge statements practicality learning and then information distribution methods reasons originally Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Docker Shulou Technology Apple Shulou Information