老师,我的没有添加成功

来源:5-1 MyBatis数据插入操作

Mr__Gao

2019-11-09 17:23:26

MyBatisTestor

@Test
public void testInsert() throws Exception {
    SqlSession session=null;
    try {
        session=MyBatisUtils.openSession();
        Goods goods=new Goods();
        goods.setTitle("测试商品");
        goods.setSubTitle("测试子标题");
        goods.setOriginalCost(200f);
        goods.setCurrentPrice(100f);
        goods.setDiscount(0.5f);
        goods.setIsFreeDelivery(1);
        goods.setCategoryId(43);
        System.out.println(goods);
        //insert()方法返回值代表本次成功插入的记录总数
        int num=session.insert("goods.insert",goods);
        session.commit();//提交事务数据
        System.out.println(goods.getGoodsId());
    } catch (Exception e) {
        if (session!=null){
            session.rollback();
        }
        throw e;
    } finally {
        MyBatisUtils.closeSession(session);
    }
}

goods.xml

<insert id="insert" parameterType="com.imooc.mybatis.entity.Goods" >
    insert into t_goods(title, sub_title, original_cost, current_price, discount, is_free_delivery)
    values(#{title},#{subTitle},#{originalCost},#{currentPrice},#{discount},#{isFreeDelivery})
</insert>


写回答

2回答

好帮手慕柯南

2019-11-09

同学你好!

请问同学是查询所有的数据没有看到吗?这是因为数据库中数据太多了,没有显示全,同学可以改一下商品的标题,根据条件查询一下,就可以看到了,比如:

添加语句:

http://img.mukewang.com/climg/5dc69acc0922123504380135.jpg

查询数据库:

http://img.mukewang.com/climg/5dc69b0d09deca7f06470296.jpg

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~

0
hr__Gao
h 看到了 谢谢老师
h019-11-09
共1条回复

好帮手慕柯南

2019-11-09

同学你好!

你看你的代码老师测试时,num的值是1。说明添加成功了,很棒~

http://img.mukewang.com/climg/5dc68b1a09128bb409560354.jpg

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~

0
hr__Gao
h 可是在数据表里边没有显示出来啊
h019-11-09
共1条回复

0 学习 · 8016 问题

查看课程