错误代码如下:
繁昌网站建设公司创新互联,繁昌网站设计制作,有大型网站制作公司丰富经验。已为繁昌近千家提供企业网站建设服务。企业网站搭建\成都外贸网站制作要多少钱,请找那个售后服务好的繁昌做网站的公司定做!
@Test public void testInsertOne(){ SqlSession sqlSession = MyBatisUtils.getSession(); UserInfo userInfo = new UserInfo(); userInfo.setNickname("sunny"); userInfo.setPhoneNum("18936896033"); sqlSession.insert("insertUser", userInfo); LOG.log(Level.INFO, "userId:"+userInfo.getId()); sqlSession.close(); }
原因是会话没有被提交而是被回滚了,修改代码如下:
@Test public void testInsertOne(){ SqlSession sqlSession = MyBatisUtils.getSession(); UserInfo userInfo = new UserInfo(); userInfo.setNickname("sunny"); userInfo.setPhoneNum("18936896033"); sqlSession.insert("insertUser", userInfo); sqlSession.commit(); //注意提交事物 LOG.log(Level.INFO, "userId:"+userInfo.getId()); sqlSession.close(); }
源码解读:首先看看openSession的几种方式:
SqlSession openSession() SqlSession openSession(boolean autoCommit) SqlSession openSession(Connection connection) SqlSession openSession(TransactionIsolationLevel level) SqlSession openSession(ExecutorType execType,TransactionIsolationLevel level) SqlSession openSession(ExecutorType execType) SqlSession openSession(ExecutorType execType, boolean autoCommit) SqlSession openSession(ExecutorType execType, Connection connection)
从地一个和第二个就可看出区别:
openSession()会创建一个事物,但是不会自动提交
openSession(true)会创建一个事物,并自动提交
openSession(Connection connection),不使用数据元配置,而是自定义的一个链接
openSession(TransactionIsolationLevel level)事物的隔离级别:
(NONE,READ_UNCOMMITTED,READ_COMMITTED,REPEA TABLE_READ,SERIALIZA BLE)
openSession(ExecutorType execType):
ExecutorType.SIMPLE: 这个执行器类型不做特殊的事情。它为每个语句的执行创建一个新的预处理语句。
ExecutorType.REUSE: 这个执行器类型会复用预处理语句。
ExecutorType.BATCH: 这个执行器会批量执行所有更新语句,如果 SELECT 在它们中间执行还会标定它们是 必须的,来保证一个简单并易于理解的行为。
当前名称:Mybatis配置insert时,插入数据失败
网站URL:https://www.cdcxhl.com/article2/jigdoc.html
成都网站建设公司_创新互联,为您提供网站建设、定制网站、做网站、手机网站建设、建站公司、网站维护
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联