Mybatis自动提交失败:Setting autocommit to false on JDBC Connection
作者:互联网
报错:
今天在使用Mybatis进行添加数据的时候,发现自己的SQL语句没有问题!但是数据就是添加不到数据库?经过一番搜索发现,Mybatis默认情况下开启的是手动提交, 我们需要开启自动提交!
方法:
- 使用SqlSession对象调用commit()方法;
@Before
public void init() throws Exception {
input = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(input);
sqlSession =factory.openSession();
userDao = sqlSession.getMapper(IUserDao.class);
sqlSession.commit();
}
- openSession()方法里加上 true参数
@Before
public void init() throws Exception {
input = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(input);
sqlSession =factory.openSession(true);
userDao = sqlSession.getMapper(IUserDao.class);
}
标签:autocommit,JDBC,false,openSession,factory,sqlSession,Mybatis,input,Resources 来源: https://www.cnblogs.com/geq2020/p/13531530.html