问题描述:
mybatis进行数据插入或更新操作,方法成功执行,数据库中却不存在新数据,原本代码如下:
@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();
}
解决方案:
在插入或更新语句后,增添commit,代码如下:
@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));
/*添加commit*/
sqlSession.commit();
sqlSession.close();
}