Encountered a problem: Use mybaties to insert data into the database. Idea shows that the insertion is successful, but there is no data change in the database?

Encountered a problem: Use mybaties to insert data into the database. Idea shows that the insertion is successful, but there is no data change in the database?

There are several possible reasons:

  1. No transaction committed: When using MyBatis for database operations, transactions need to be submitted manually. You can call the method to commit the transaction after inserting data is complete  commit() . If the transaction is not committed, the data in the database is not actually saved.

  2. Configuration issues: Please make sure your database connection is configured correctly and connected to the correct database. You can check whether the database URL, user name, password and other information in the configuration file are correct.

  3. SQL statement error: Please check whether your insert statement is correct. There may be syntax errors or data type mismatches that cause the insertion to fail. You can try to manually execute the same SQL statement to verify.

  4. Database auto-commit settings: Some databases are auto-commit by default, that is, modifications will be automatically saved to the database each time a SQL statement is executed. If your database is in auto-commit mode, it may be that other code modified the data but did not submit it in time, resulting in no visible changes.

  5. Database transaction isolation level: If you perform an insert operation with transactions enabled, it may be due to the transaction isolation level. You can try to set the transaction isolation level to READ_COMMITTED, or disable transactions to verify whether data can be inserted normally.

Hopefully the above tips will help you find the problem and solve it. If the problem persists, please provide more details and code to better help you resolve it.

First time code:

public class InsertDataTest {
    public static void main(String[] args) {
        Emp emp =new Emp("102","王二浪","男",29,"23232323232323","武当山","2000-10-20");
        MyBatisUtil util = new MyBatisUtil();
        SqlSession sqlSession = util.getSqlSession();
        int insertData = sqlSession.insert("insertData", emp);

        System.out.println(insertData);
        
    }
}

result:

There is no problem with the console result output, the trend is output, and the expected effect is achieved. However, when returning to the database, the data is not submitted. Finally, we find that when we first configured the transaction manager, we configured it as shown below, so it is turned on. For the transaction, we did not submit it, so the transaction will not be submitted;

 <!--
                transactionManager 事务管理器
                type的值有JDBC和MANAGED
                    JDBC – 这个配置直接使用了 JDBC 的提交和回滚设施,它依赖从数据源获得的连接来管理事务作用域,
                    也就是给我们来手动管理事务的提交。
            -->
            <transactionManager type="JDBC"/>

Correct code:

public class InsertDataTest {
    public static void main(String[] args) {
        Emp emp =new Emp("102","王二浪","男",29,"23232323232323","武当山","2000-10-20");
        MyBatisUtil util = new MyBatisUtil();
        SqlSession sqlSession = util.getSqlSession();
        int insertData = sqlSession.insert("insertData", emp);

        System.out.println(insertData);
        if (sqlSession != null) {
            sqlSession.commit();
            sqlSession.close();
        }
    }
}

Guess you like

Origin blog.csdn.net/m0_64231944/article/details/132844478