# MyBatis 数据更新与删除

### 数据更新

在 MyBatis\src\main\resources\mappers\goods.xml 文件 下的 \<mapper namespace="goods" >标签下添加下面标签：

```xml
    <update id="update" parameterType="indi.chester.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>
```

在 MyBatis\src\test\java\MyBatisTest.java 添加下面这个方法：

```java
    @Test
    public void testUpdate() throws Exception{
        SqlSession sqlSession=null;
        try {
            sqlSession=MyBatisUtils.openSession();
            Goods goods=sqlSession.selectOne("goods.selectById",902);
            goods.setTitle("更新测试商品");
            int num =sqlSession.update("goods.update", goods);
            System.out.println(num);
            sqlSession.commit();
        }catch (Exception e){
            if (sqlSession!=null){
                sqlSession.rollback();
            }
            throw e;
        }finally {
            MyBatisUtils.closeSession(sqlSession);
        }
    }
```

运行结果:

```
10:02:20:869 [main]  DEBUG goods.selectById -==>  Preparing: select * from t_goods WHERE goods_id=?  
10:02:20:915 [main]  DEBUG goods.selectById -==> Parameters: 902(Integer) 
10:02:20:943 [main]  DEBUG goods.selectById -<==      Total: 1 
10:02:20:950 [main]  DEBUG goods.update -==>  Preparing: UPDATE babytun.t_goods SET title=?, sub_title=?, original_cost=?, current_price=?, discount=?, is_free_delivery=?, category_id=? WHERE goods_id=?  
10:02:20:951 [main]  DEBUG goods.update -==> Parameters: 更新测试商品(String), MyBatis测试商品子标题(String), 200.0(Float), 100.0(Float), 0.5(Float), 1(Integer), 43(Integer), 902(Integer) 
10:02:20:954 [main]  DEBUG goods.update -<==    Updates: 1 
1
```

### 数据删除

在 MyBatis\src\main\resources\mappers\goods.xml 文件 下的 \<mapper namespace="goods" >标签下添加下面标签：

```xml
    <delete id="delete" parameterType="Integer">
        DELETE FROM  t_goods WHERE  goods_id=#{value }
    </delete>

```

在 MyBatis\src\test\java\MyBatisTest.java 添加下面这个方法：

```java
    @Test
    public void testDelete() throws Exception{
        SqlSession sqlSession=null;
        try {
            sqlSession=MyBatisUtils.openSession();
            int num =sqlSession.delete("goods.delete",903);

            sqlSession.commit();
        }catch (Exception e){
            if (sqlSession!=null){
                sqlSession.rollback();
            }
            throw e;
        }finally {
            MyBatisUtils.closeSession(sqlSession);
        }
    }
```

运行结果:

```
10:05:49:228 [main]  DEBUG goods.delete -==>  Preparing: DELETE FROM babytun.t_goods WHERE goods_id=?  
10:05:49:280 [main]  DEBUG goods.delete -==> Parameters: 903(Integer) 
10:05:49:283 [main]  DEBUG goods.delete -<==    Updates: 0 
10:05:49:284 [main]  DEBUG o.a.i.t.jdbc.JdbcTransaction -Committing JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@79e2c065 [wrapping: com.mysql.jdbc.JDBC4Connection@3a93b025]] 
0
```
