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);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示