Mybatis实现添加操作

Mybatis实现添加操作

1 普通的添加

返回值为void 通过是否有异常报错来看添加是否成功

接口

/**
 * 添加
 */
void add(Brand brand);

sql映射

<!--添加-->
<insert id="add">
    insert into tb_brand (brand_name, company_name, ordered, description, status)
    values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});
</insert>

测试

 @Test
    public void testAdd() throws IOException {
//        设置参数
        int status = 1;
        String companyName = "深圳市腾讯计算机系统有限公司";
        String brandName = "腾讯";
        String description = "一切以用户价值为依归";
        int ordered = 100;

//        封装对象
        Brand brand = new Brand();
        brand.setStatus(status);
        brand.setCompanyName(companyName);
        brand.setBrandName(brandName);
        brand.setDescription(description);
        brand.setOrdered(ordered);

//    1.获取SQLSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//        2. 获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
//        设置为自动提交事务
//        SqlSession sqlSession = sqlSessionFactory.openSession(true);
//        3. 获取Mapper接口的代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//        4. 执行方法
        brandMapper.add(brand);
//        5. 提交事务
        sqlSession.commit();
//        6. 释放资源
        sqlSession.close();
    }

image-20220815182550976

这里的测试案例使用了一行新的代码

sqlSession.commit();

这是提交事务。因为当SQL执行完之后,只是在MyBatis执行完了,它不会自动提交事务,所以在数据库还是看不到。所以加上这一行让他自动提交事务。或者

SqlSession sqlSession = sqlSessionFactory.openSession(true)

这样也可以自动提交事务

2 主键返回

在一些场景中,我们添加完数据后,需要返回这条数据的主键值

只需要在sql配置的时候添加两个属性

<insert id="add" useGeneratedKeys="true" keyProperty="id">
    insert into tb_brand (brand_name, company_name, ordered, description, status)
    values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});
</insert>

useGeneratedKeys设置为 true, keyProperty里面放的是主键的名称

测试

    @Test
    public void testAdd() throws IOException {
//        设置参数
        int status = 1;
        String companyName = "深圳市腾讯计算机系统有限公司";
        String brandName = "腾讯";
        String description = "一切以用户价值为依归";
        int ordered = 100;

//        封装对象
        Brand brand = new Brand();
        brand.setStatus(status);
        brand.setCompanyName(companyName);
        brand.setBrandName(brandName);
        brand.setDescription(description);
        brand.setOrdered(ordered);

//    1.获取SQLSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//        2. 获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
//        设置为自动提交事务
//        SqlSession sqlSession = sqlSessionFactory.openSession(true);
//        3. 获取Mapper接口的代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//        4. 执行方法
        brandMapper.add(brand);
        Integer id = brand.getId();
        System.out.println(id);
//        5. 提交事务
        sqlSession.commit();
//        6. 释放资源
        sqlSession.close();
    }

image-20220815193144022

image-20220815193155157

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不会挂科i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值