修改字段

修改全部字段

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

  参数:所有数据

  结果:void

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

3.执行方法,测试

 步骤一:

 

 

 步骤二:

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

步骤三

     //添加数据
     public void testAdd() throws IOException {
         //接受参数
         //现在是固定数据,以后会变成动态数据
         int id = 4;
         String brandName = "yzx";
         String companyName = "喜欢";
         String ordered = "lzj";
         String description = "!!!!!";
         int status = 0;

         //处理参数,定义一个关键字,将查询的关键字封装
         Brand brand = new Brand();
         brand.setId(id);
         brand.setBrandName(brandName);
         brand.setCompanyName(companyName);
         brand.setOrdered(ordered);
         brand.setDescription(description);
         brand.setStatus(status);


         //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.update(brand);

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

 修改动态字段

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

  参数:所有数据

  结果:void

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

3.执行方法,测试

 <update id="update">
        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 and ordered != '' ">
                ordered = #{ordered},
            </if>
            <if test="description != null and description != '' ">
                description = #{description},
            </if>
            <if test="status != null">
                status = #{status}
            </if>
        </set>
        where id = #{id};
    </update>

 

posted @ 2023-03-30 13:00  YE-  阅读(15)  评论(0编辑  收藏  举报