When using MySQL+ibatis for insert test, an error is reported: The information is as follows:
Test insert com.ibatis.common.jdbc.exception.NestedSQLException: --- The error occurred in com/study/ibatis/Student.xml. --- The error occurred while applying a parameter map. --- Check the addStudent-InlineParameterMap. --- Check the statement (update failed). --- Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY' at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeUpdate(MappedStatement.java:107) at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.insert(SqlMapExecutorDelegate.java:393) at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.insert(SqlMapSessionImpl.java:82) at com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.insert(SqlMapClientImpl.java:58) at com.study.ibatis.StudentDaoImpl.addStudent(StudentDaoImpl.java:33) but com.study.ibatis.TestIbatis.main (TestIbatis.java: 14 ) Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY' at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) at com.mysql.jdbc.Util.getInstance(Util.java:381) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734) at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:995) at com.ibatis.sqlmap.engine.execution.SqlExecutor.executeUpdate(SqlExecutor.java:80) at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.sqlExecuteUpdate(MappedStatement.java:216) at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeUpdate(MappedStatement.java:94) ... 5 more false
The meaning of the error is: Repeatedly input the “1” key as “primary” to enter the database, and when querying the table tbl_student, three records were found, one of which was a primary key. Considering that when I was testing, the default primary key in the database was 1. So delete the record whose primary key is 1 in the database. Then the test passed.
Summary: I set the primary key in ibatis to the default 1. As a result, the test starts directly from the default 1. Then an error is reported, and the default value of the primary key is set to null to solve the problem.
Read More:
- [Solved] Error occurred during connect to primary: exception 3000301: Connection could not be established
- How to Solve Logstash error: failed to execute action
- [Solved] Mybatis Error: CannotFindDataSourceException: dynamic-datasource can not find primary datasource
- How to Solve AOP error in Spring
- How to Solve Error: IncompleteElementException: Could not find result map…
- How to Solve golang test Error: # command-line-arguments [command-line-arguments.test]
- How to Solve Pycharm SQL Union Error
- How to Solve Springmvc Error: org.springframework.web.servlet.DispatcherServlet
- How to Solve Starrocks Various Error
- How to Solve Jetpack room use error
- How to Solve “Error db.num is null” when starting Nacos
- How to Solve Error: Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
- How to Solve Application failed to start error
- How to Solve Error: Type mismatch: cannot convert from Object to Car
- How to Solve Azkaban Error Chung during uploading files to DB
- How to Solve elasticsearch-7.15.1 operation errors
- How to Solve Sqlyog error 2058
- How to Solve elasticSearch8.1.2 Install Error in Win10
- How to Solve Error: Failed to load ApplicationContext