首页 > 其他分享 >015.更新与删除操作

015.更新与删除操作

时间:2022-11-09 00:35:06浏览次数:58  
标签:Exception goods 删除 更新 session 015 null throws MyBatisUtils

1.更新

1.1 在goods.xml中编写

 <update id="update" parameterType="com.imooc.mybatis.entity.Goods">
        UPDATE t_goods
        SET title            = #{title},
            sub_title        = #{subTitle},
            original_cost    = #{originalCost},
            current_price    = #{currentPrice},
            discount         = #{discount},
            is_free_delivery = #{isFreeDelivery},
            category_id      = #{categoryId}
        WHERE goods_id = #{goodsId}
    </update>

1.2 在测试用例中编写

 /**
     * 更新数据
     *
     * @throws Exception
     */
    @Test
    public void testUpdate() throws Exception
    {
        SqlSession session = null;
        try
        {
            session = MyBatisUtils.openSession();
            Goods goods = session.selectOne("goods.selectById", 740);
            goods.setTitle("更新测试商品123");
            int num = session.update("goods.update", goods);
            session.commit();//提交事务数据
        }
        catch (Exception e)
        {
            if (session != null)
            {
                session.rollback();//回滚事务
            }
            throw e;
        }
        finally
        {
            MyBatisUtils.closeSession(session);
        }
    }

 

2.删除

2.1 在goods.xml中编写

   <!--delete from t_goods where goods_id in (1920,1921)-->
    <delete id="delete" parameterType="Integer">
        delete
        from t_goods
        where goods_id = #{value}
    </delete>

2.2 在测试用例中编写

 /**
     * 删除数据
     * @throws Exception
     */
    @Test
    public void testDelete() throws Exception {
        SqlSession session = null;
        try{
            session = MyBatisUtils.openSession();
            int num = session.delete("goods.delete" , 740);
            session.commit();//提交事务数据
        }catch (Exception e){
            if(session != null){
                session.rollback();//回滚事务
            }
            throw e;
        }finally {
            MyBatisUtils.closeSession(session);
        }
    }

 

标签:Exception,goods,删除,更新,session,015,null,throws,MyBatisUtils
From: https://www.cnblogs.com/LLL0617/p/16871807.html

相关文章

  • 19. 删除链表的倒数第 N 个结点
    19.删除链表的倒数第N个结点给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。示例1:输入:head=[1,2,3,4,5],n=2输出:[1,2,3,5]示例2:输入:hea......
  • 输入n组测试数据(1<=n<=10),每组数据中第一行输入一个整数m,表示下行有m个数据输入,判断每
    输入:一行输入一个整数n接下去每组数据中,第一个输入整数m下一行输入m个整数输出:输出每组大于6000的数的个数#include<stdio.h>main(){intn,m,i,j,a,s=0;scanf("......
  • 配置sshd_使用CA签名证书登录_更新sshd服务端的通讯密钥
    配置sshd_使用CA签名证书登录_更新sshd服务端的通讯密钥转载注明来源:本文链接来自osnosn的博客,写于2022-11-06.用CA签名证书登录请参考【SSH证书登录教程】,这个......
  • 问题 M: 零基础学C/C++158——删除中间的*
    思路很简单,但实现起来有点麻烦。将前面2题融合(前两题我就觉得没必要放了哈哈哈哈),保留前面与后面的*都改成删除即可。你会发现我的代码是前两个的融合。要学会融会贯通鸭:......
  • Mysql常见错误处理(持续更新)
    ERROR1290(HY000):TheMySQLserverisrunningwiththe--skip-grant-tablesoptionsoitcannotexecutethisstatement​mysqladmin: connect to server at '......
  • 5种从JavaScript 数组中删除项目的方式
    英文| https://javascript.plainenglish.io/how-to-remove-an-item-from-a-javascript-array-in-5-ways-2932b2686442翻译|杨小二有很多方法可以从JavaScript数组中删......
  • VS2015快捷键大全
    在学习C基础总结了笔记,并分享出来。有问题请及时联系博主:​​Alliswell_WP​​,转载请注明出处。目录:零、VS常用快捷键一、VS2015快捷键大全一、VS2008快捷键大全三、VS2005......
  • Java文件的创建、删除
    1.如何创建文件?  创建文件对象相关构造器newFile(Stringpathname)//根据路径构造一个File对象newFile(Fileparent,Stringchild)//根据父目录文件+子路径构建......
  • Linux:Ubuntu更新源操作
    1.备份源文件#切换目录到apt下cd/etc/apt#备份源文件sources.listcp/etc/apt/sources.list/etc/apt/sources.list.backup2.登录国内公有源地址​​阿里源​​​​......
  • mysql更新某日期为随机日期
    过去的五百天内:SELECTdate_sub(now(),interval(SELECTFLOOR(RAND()*500))DAY)============================================================思路:使用date_add()......