MyBatis 八——修改全部字段/修改动态字段

配置文件完成修改全部字段

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

        观察参数

        返回结果

 

 

 

    2、编写SQL语句:SQL映射文件;

 

 

    3:执行方法,测试

复制代码
  //添加
    @Test
    public  void testUpdate() throws IOException {
        int id = 10;
        int status =1;
        String companyName = "波导手机";
        String brandName = "波导";
        String description = "asdawasdw手机中的战斗机";
        String ordered = "21000";

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

        //1获取sqlSessionFactory
        String resource = "mybatis-config.xml";                                 //配置文件
        InputStream inputStream = Resources.getResourceAsStream(resource);      //传入流
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);    //返回对象

        //2 获取sqlSession 对象
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //3 获取Mapper接口的代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //4 执行方法

        int count = brandMapper.update(brand);
        System.out.println(count);

        //5 释放资源
        sqlSession.close();
    }
复制代码

配置文件完成修改动态字段

只需要修改SQL语句即可

复制代码
<!--    修改-->
    <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 @   信2005-2赵磊  阅读(518)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示