011.更新与删除操作

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-29 21:08  李林林  阅读(26)  评论(0编辑  收藏  举报