Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the failure of Mybatis to execute update

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report