MyBatis基本使用
声明:基于《基于Maven工程下的MyBatis框架+MySQL+连接池的数据查询操作》与《基于Maven工程下的MyBatis基本使用之SQL传单/多参、多表关联查询》进一步拓展,相关配置文件、数据文件可阅以上两篇。
数据插入<insert>,使用<selectKey>进行回填自动生成主键值
1<!--需要明确编写获取最新主键的SQL语句--> 2<insert id="insert" parameterType="com.imooc.mybatis.entity.Goods" flushCache="true"> 3 INSERT INTO t_goods(title, sub_title, original_cost, current_price, discount, is_free_delivery, category_id) 4 VALUES (#{title} , #{subTitle} , #{originalCost}, #{currentPrice}, #{discount}, #{isFreeDelivery}, #{categoryId}) 5 <!--selectKey用来回填自动生成的主键属性,last_insert_id()函数用于获取当前连接最后产生的主键ID--> 6 <selectKey resultType="Integer" keyProperty="goodsId" order="AFTER"> 7 select last_insert_id() 8 </selectKey> 9 </insert>
1@Test 2 public void testInsert() throws Exception { 3 SqlSession session = null; 4 try{ 5 session = MyBatisUtils.openSession(); 6 Goods goods = new Goods(); 7 goods.setTitle("测试插入商品"); 8 goods.setSubTitle("测试子标题"); 9 goods.setOriginalCost(200f); 10 goods.setCurrentPrice(100f); 11 goods.setDiscount(0.5f); 12 goods.setIsFreeDelivery(1); 13 goods.setCategoryId(43); 14 //insert()方法返回值代表本次成功插入的记录总数 15 int num = session.insert("goods.insert", goods); 16 session.commit();//提交事务数据 17 System.out.println(goods.getGoodsId()); 18 }catch (Exception e){ 19 if(session != null){ 20 session.rollback();//回滚事务 21 } 22 throw e; 23 }finally { 24 MyBatisUtils.closeSession(session); 25 } 26 }
数据插入<insert>,使用useGeneratedKeys属性进行回填自动生成主键值
这里只需要修改<insert>元素:
1<!--根据驱动生成对应SQL语句--> 2 <insert id="insertII" parameterType="com.imooc.mybatis.entity.Goods" 3 useGeneratedKeys="true" keyProperty="goodsId" keyColumn="goods_id"> 4 INSERT INTO t_goods(title, sub_title, original_cost, current_price, discount, is_free_delivery, category_id) 5 VALUES (#{title} , #{subTitle} , #{originalCost}, #{currentPrice}, #{discount}, #{isFreeDelivery}, #{categoryId}) 6 </insert>
数据更新<update>
1<update id="update" parameterType="com.imooc.mybatis.entity.Goods"> 2 UPDATE t_goods SET title = #{title} ,sub_title = #{subTitle} , 3 original_cost = #{originalCost} ,current_price = #{currentPrice} , 4 discount = #{discount} ,is_free_delivery = #{isFreeDelivery} , 5 category_id = #{categoryId} 6 WHERE goods_id = #{goodsId} 7 </update>
1@Test 2 public void testUpdate() throws Exception { 3 SqlSession session = null; 4 try{ 5 session = MyBatisUtils.openSession(); 6 Goods goods = session.selectOne("goods.selectById", 739); 7 goods.setTitle("更新测试商品"); 8 int num = session.update("goods.update" , goods); 9 session.commit();//提交事务数据 10 }catch (Exception e){ 11 if(session != null){ 12 session.rollback();//回滚事务 13 } 14 throw e; 15 }finally { 16 MyBatisUtils.closeSession(session); 17 } 18 }
数据删除<detele>
1<!--根据主键删除--> 2<delete id="delete" parameterType="Integer"> 3 delete from t_goods where goods_id = #{value} 4</delete>
1@Test 2 public void testDelete() throws Exception { 3 SqlSession session = null; 4 try{ 5 session = MyBatisUtils.openSession(); 6 int num = session.delete("goods.delete" , 739); 7 session.commit();//提交事务数据 8 }catch (Exception e){ 9 if(session != null){ 10 session.rollback();//回滚事务 11 } 12 throw e; 13 }finally { 14 MyBatisUtils.closeSession(session); 15 } 16 }
