015.更新与删除操作

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);
        }
    }

 

posted @ 2022-11-09 00:21  李林林  阅读(21)  评论(0编辑  收藏  举报