关于insert返回值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | package com.imooc.mybatis; import com.imooc.dto.GoodsDTO; import com.imooc.entity.Goods; import com.imooc.entity.Student; import com.imooc.mybatis.utils.MyBatisUtils; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import javax.xml.ws.RequestWrapper; import java.io.IOException; import java.io.Reader; import java.sql.Connection; import java.util.*; public class MyBatisTestor { private Object Goods; @Test public void testSqlSessionFactory() throws IOException { //利用Reader加载classpath下的mybatis-config.xml核心配置文件 Reader reader = Resources.getResourceAsReader( "mybatis-config.xml" ); //初始化SqlSessionFactory对象,同时解析mybatis-config.xml文件 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); System.out.println( "SessionFactory加载成功" ); } @Test public void testMyBatisUtils(){ SqlSession sqlSession = null ; try { sqlSession=MyBatisUtils.openSession(); Connection connection=sqlSession.getConnection(); System.out.println(connection); } catch (Exception e){ e.printStackTrace(); } finally { MyBatisUtils.closeSession(sqlSession); } } @Test public void testSelectAll(){ SqlSession session= null ; try { session=MyBatisUtils.openSession(); List<Goods> list=session.selectList( "goods.selectAll" ); for (Goods g:list ) { System.out.println(g); } } catch (Exception e){ e.printStackTrace(); } finally { MyBatisUtils.closeSession(session); } } @Test public void testSelectAllStudent(){ SqlSession session= null ; try { session=MyBatisUtils.openSession(); List<Student> list=session.selectList( "student.selectAll" ); for (Student s:list ) { System.out.println(s); } } catch (Exception e){ e.printStackTrace(); } finally { MyBatisUtils.closeSession(session); } } @Test public void testSelectById(){ SqlSession session= null ; try { session = MyBatisUtils.openSession(); Goods = session.selectOne( "goods.selectById" , 1602 ); System.out.println(Goods); } catch ( Exception e){ e.printStackTrace(); } finally { MyBatisUtils.closeSession(session); } } @Test public void selectGoodsMap(){ SqlSession session= null ; try { session = MyBatisUtils.openSession(); List<Map> list = session.selectList( "goods.selectGoodsMap" ); for (Map map : list ) { System.out.println(map); } // System.out.println(Goods); } catch (Exception e){ e.printStackTrace(); } finally { MyBatisUtils.closeSession(session); } } @Test public void selectGoodsDTO(){ SqlSession session= null ; try { session = MyBatisUtils.openSession(); List<GoodsDTO> list = session.selectList( "goods.selectGoodsDTO" ); for (GoodsDTO g : list ) { System.out.println(g); } // System.out.println(Goods); } catch (Exception e){ e.printStackTrace(); } finally { MyBatisUtils.closeSession(session); } } @Test public void insert(){ SqlSession session= null ; try { session = MyBatisUtils.openSession(); Goods goods = new Goods(); goods.setTitle( "test" ); goods.setSubTitle( "test123" ); goods.setOriginalCost(200f); goods.setCurrentPrice(100f); goods.setDiscount( 0 .5f); goods.setIsFreeDelivery( 1 ); goods.setCategoryId( 43 ); int num=session.insert( "goods.insert" ,goods); System.out.println(num); System.out.println(goods.getGoodsId()); session.commit(); } catch (Exception e){ e.printStackTrace(); if (session!= null ){ session.rollback(); } } finally { MyBatisUtils.closeSession(session); } } @Test public void insertStudent(){ SqlSession session = null ; try { session=MyBatisUtils.openSession(); Student student = new Student(); student.setName( "楠楠" ); student.setRegNo( 20171208 ); student.setAge( 26 ); student.setGrade( "2013" ); student.setMajor( "哲学系" ); student.setSex( "男" ); int num = session.insert( "goods.insertstudent" , student); System.out.println(num); session.commit(); } catch (Exception e) { e.printStackTrace(); if (session != null ) { session.rollback(); } } finally { session.close(); } } } |
int num = session.insert("goods.insertstudent", student);
我想问一下怎么判断除了在数据表里面查看,还有什么方法可以知道是否插入成功,上面这个方法的返回值1可以证明插入成功吗,如果可以的话,不是要运行session.commit()才提交吗?
32
收起
正在回答 回答被采纳积分+1
2回答
芝芝兰兰
2019-12-05 18:12:23
同学你好。先要弄明白,执行和真正的提交数据库是两个过程。
1、即便是执行到commit才提交事务,也不是说直到commit执行数据库才执行插入操作。在commit之前,插入实际上可以看做是已经执行了。
commit前的数据操作,同学可以想象是在内存的一块缓冲区域中进行的。在真正的提交后,才会把这个更改存储到数据库中。
2、也就是说,在提交事务时,会检查这个插入是否成功,并且保证它成功插入一条。事务提交可以理解是一个核对的过程。
num是在内存中执行完sql返回,而事务提交之后才会将执行后的数据写在数据库中。但事务回滚是由捕获异常触发的,所以可以根据是否发生异常和num的值两点来判断是否成功插入。
如果解答了同学的疑问,望采纳~
祝学习愉快~
3. Java 数据库开发与实战应用
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧