基于Maven工程下的MyBatis基本使用之数据插入【回填】、修改与删除

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 }
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

基于Maven工程下的MyBatis基本使用之SQL传单/多参、多表关联查询

MyBatis基本使用声明:基于《基于Maven工程下的MyBatis框架MySQL连接池的数据查询操作》进一步拓展,相关配置文件、数据文件可阅上篇。SQL传单/多参在goods.xml新增两个<select:<!单参数传参,使用paramterType指定的数据类型即可,SQL中value提取参数<selectid"sel

基于Maven工程下的MyBatis框架+MySQL+连接池的数据查询操作

具体操作项目结构引入项目依赖pom.xml<?xmlversion"1.0"encoding"UTF8"?<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchemainstance"xsi