Mybatis添加和修改功能

上一章节,查询

一. 添加数据

  1. 如上图是我们平时在添加数据时展示的页面,而我们在该页面输入想要的数据后添加 提交 按钮,就会将这些数据添加到数据库中。
    在这里插入图片描述
    接下来我们就来实现添加数据的操作

  2. 编写的方法

添加方法要时刻注意事务提交问题,在Java中是关闭自动提交事务的
问题:
此时数据库是没有添加的新信息
在这里插入图片描述

解决后(含两种写法):

写法1:

   public void testAdd() throws IOException {
        //构建SqlSessionFactory
        String resource = "mybatis.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //通过Mapper代理获取相对应的接口
        SqlSession session = sqlSessionFactory.openSession(true);
        BrandMapper mapper = session.getMapper(BrandMapper.class);

        //调用方法
        Brand brand =new Brand();
        brand.setbrandName("菠萝");
        brand.setcompanyName("菠萝科技有限公司");
        brand.setOrdered("100");
        brand.setDescription("美国有苹果,中国有菠萝");
        brand.setStatus(0);
        //添加信息
        mapper.addBrand(brand);
        //释放资源
        session.close();
    }

写法2:

  public void testAdd() throws IOException {
        //构建SqlSessionFactory
        String resource = "mybatis.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //通过Mapper代理获取相对应的接口
        SqlSession session = sqlSessionFactory.openSession(true);
        BrandMapper mapper = session.getMapper(BrandMapper.class);

        //调用方法
        Brand brand =new Brand();
        brand.setbrandName("菠萝");
        brand.setcompanyName("菠萝科技有限公司");
        brand.setOrdered("100");
        brand.setDescription("美国有苹果,中国有菠萝");
        brand.setStatus(0);
        //添加信息
        mapper.addBrand(brand);
        //释放资源
        session.close();
    }

结果图:
在这里插入图片描述
在这里插入图片描述

二. 添加-主键返回

在数据添加成功后,有时候需要获取插入数据库数据的主键(主键是自增长)。

比如:添加订单和订单项,如下图就是京东上的订单
在这里插入图片描述

明白了什么时候 主键返回 。接下来我们简单模拟一下,在添加完数据后打印id属性值,能打印出来说明已经获取到了。我们将上面添加品牌数据的案例中映射配置文件里 statement 进行修改,如下

  • 添加订单数据

添加订单数据后要时刻注意是否能获取问题
问题:
在没有修改映射文件BrandMapper.xml情况下,正常添加后获取id属性值是没有任何信息的
在这里插入图片描述

解决方法:
我们将上面添加品牌数据的案例中映射配置文件BrandMapper.xml里 statement 进行修改,如下

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

结果图:
在这里插入图片描述

总结:
在 insert 标签上添加如下属性:

  • useGeneratedKeys:是够获取自动增长的主键值。true表示获取
  • keyProperty :指定将获取到的主键值封装到哪儿个属性里(注意说的是类的属性)

三. 修改数据(包含问题所在之处)

在这里插入图片描述

如图所示是修改页面,用户在该页面书写需要修改的数据,点击 提交 按钮,就会将数据库中对应的数据进行修改。注意一点,如果哪儿个输入框没有输入内容,我们是将表中数据对应字段值替换为空白还是保留字段之前的值?答案肯定是保留之前的数据。

  • 接下来我们就具体来实现

注意的问题
注意看没有if标签情况下会出现不理想的状态
在这里插入图片描述

解决方法:

CaseDemo测试类

@Test
    public void updateBrand() throws IOException {
        //构建SqlSessionFactory
        String resource = "mybatis.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //通过Mapper代理获取相对应的接口
        SqlSession session = sqlSessionFactory.openSession(true);
        BrandMapper mapper = session.getMapper(BrandMapper.class);

        //修改信息
        Brand brand = new Brand();
        brand.setbrandName("鸡你太美");
        brand.setId(1);

        int i = mapper.updateBrand(brand);
        System.out.println(i);

        //释放资源
        session.close();
    }

修改BrandMapper.xml映射文件

 <update id="updateBrand">
        update tb_brand
        <set>
            <if test="brandName != null and brandName != ''">
            brand_name = #{brandName},
            </if>
            <if test="companyName != null and companyName != ''">
            company_name = #{companyName},
            </if>
            <if test="ordered != null">
                ordered = #{ordered},
            </if>
            <if test="description != null and description != ''">
                description = #{description},
            </if>
            <if test="status !=null">
                status = #{status}
            </if>
        </set>
            where id = #{id};
    </update>

接口BrandMapper

    //修改数据
    int updateBrand(Brand brand);

结果图:
在这里插入图片描述
从结果中SQL语句可以看出,只修改了 id 和 brand_name 字段值,因为我们给的数据中只给Brand实体对象的 status 属性设置值了。这就是 set 标签的作用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

发热的嘤嘤怪(2003计科胜胜同学)

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

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

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

打赏作者

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

抵扣说明:

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

余额充值