JavaWeb:MyBatis(8)添加&修改

添加

1.编写接口方法: Mapper接口

参数:除了id之外的所有数据

结果: void

   void add(Brand brand);

2.编写SQL语句: SQL映射文件

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

3.执行方法,测试

MyBatis事务:

openSession():默认开启事务,进行增删改操作后需要使用sqlSession.commit();手动提交事务

openSession(true):可以设置为自动提交事务(关闭事务)

@Test
    public void testAdd()throws Exception{

        //接收id
        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.加载mybatis的核心配置文件,获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);


        //2.获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //3.获取Mapper接口的代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        //4.执行方法
        brandMapper.add(brand);

        //提交事务
        sqlSession.commit();


        //5.释放资源
        sqlSession.close();
    }

添加–主键返回

在数据添加成功后,需要获取插入数据库数据的主键的值

比如:添加订单和订单项

  1. 添加订单

  1. 添加订单项,订单项中需要设置所属订单的id

SQL映射文件中添加属性

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

执行方法,测试代码中添加获取主键的方法

@Test
    public void testaddOrder()throws Exception{

        //接收id
        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.加载mybatis的核心配置文件,获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);


        //2.获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //3.获取Mapper接口的代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        //4.执行方法
        brandMapper.addOrder(brand);
        Integer id = brand.getId();
        System.out.println(id);


        //提交事务
        sqlSession.commit();


        //5.释放资源
        sqlSession.close();
    }

小结:

返回添加数据的主键

<insert useGeneratedKeys="true" keyProperty="id">

修改

  1. 修改全部字段

1.编写接口方法: Mapper接口

参数:所有数据

结果: void

   int update(Brand brand);

2.编写SQL语句: SQL映射文件

    <update id="update">
        update tb_brand
        set brand_name = #{brandName},
        company_name = #{companyName},
        ordered = #{ordered},
        description = #{description},
        status = #{status}
        where id = #{id};
    </update>

3.执行方法,测试

@Test
    public void testUpdate()throws Exception{

        //接收id
        int status = 1;
        String companyName = "波导手机";
        String brandName = "波导";
        String description = "波导手机,手机中的战斗机";
        int ordered = 200;
        int id = 5;



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


        //1.加载mybatis的核心配置文件,获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);


        //2.获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //3.获取Mapper接口的代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        //4.执行方法
        int conut = brandMapper.update(brand);
        System.out.println(conut);


        //提交事务
        sqlSession.commit();


        //5.释放资源
        sqlSession.close();
    }
  1. 修改动态字段

1.编写接口方法: Mapper接口

参数:部分数据,封装到对象中

结果: void

   int updatechanged(Brand brand);

2.编写SQL语句: SQL映射文件

    <update id="updatechanged">
        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>

3.执行方法,测试

    @Test
    public void testUpdateChanged()throws Exception{

        //接收id
        int status = 0;
        String companyName = "波导手机";
        String brandName = "波导";
        String description = "波导手机,手机中的战斗机";
        int ordered = 200;
        int id = 6;



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


        //1.加载mybatis的核心配置文件,获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);


        //2.获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //3.获取Mapper接口的代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        //4.执行方法
        int conut2 = brandMapper.updatechanged(brand);
        System.out.println(conut2);


        //提交事务
        sqlSession.commit();


        //5.释放资源
        sqlSession.close();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值