MyBatis——案例——修改(修改全部字段,修改动态字段)

风陵南·2022-09-27 22:03·299 次阅读

MyBatis——案例——修改(修改全部字段,修改动态字段)

修改-修改全部字段

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

    参数:所有数据

    结果:void(通过异常捕获判断成功修改与否)

       int (表示sql语句影响的行数)

/**
* 修改
*/
int update(Brand brand);

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

<!-- 修改 -->
<update id="update">
update tb_brand
set brand_name = #{brandName},
company_name = #{companyName},
orderd = #{orderd},
description = #{description},
status = #{status}
where
id = #{id}
</update>

  3、执行方法,测试#

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

 

修改-修改动态字段

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

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

    结果:void(通过异常捕获判断成功修改与否)

       int (表示sql语句影响的行数)

/**
* 修改
*/
int update(Brand brand);

  2、编写SQL语句:SQL映射文件 <set> 会自动检测语法错误并纠正</set>  与<where>标签一样 #

<!-- 动态修改(修改参数中不为空的字段) -->
<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="orderd != null">
orderd = #{orderd},
</if>
<if test="description != null and description != ''">
description = #{description},
</if>
<if test="status != null">
status = #{status}
</if>
</set>
where
id = #{id}
</update>

  3、执行方法,测试#

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

 

posted @   风陵南  阅读(299)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
点击右上角即可分享
微信分享提示
目录