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);

                
            
        
浙公网安备 33010602011771号