在第一次学习mybatis的时候,使用set进行修改表中内容,修改成功,查询也能查询到修改后的内容,但所数据库数据却没有被修改
Teacher teacher = new Teacher(); teacher.setTid(1); teacher.setTname("ccc"); int i = classDao.updateTeacher(teacher); System.out.println("修改了"+i+"条记录"); List<Teacher> teachers = classDao.selectTeacher(); System.out.println(teachers);
控制台显示结果执行成功,且查询teacher表的数据结果正确。
但是进入数据库表查看时发现数据库中的数据并没有被修改。
这个原因是因为mybatis默认不是自动提交事务的, 所以其实没有修改数据库,
刚刚新增完后立即返回的结果,是从mybatis为了提高性能设置的缓存里读取的,不是从数据库读取的
解决的办法:一是在openSession() 的括号里写true, 设定自动提交事务,
二是在代码中加入session.commit() 手动提交事务
1、设定自动提交openSession( autoCommit=true)
2、手动提交事务session.commit()