无名阁,只为技术而生。流水不争先,争的是滔滔不绝。

(updatebyexampleselective) mybatis:updatebyexample与updatebyexampleselective MyBatis持久层框架两种update方法 全网首发(图文详解1)

前沿技术 Micheal 4个月前 (06-02) 40次浏览 已收录 扫描二维码

(updatebyexampleselective) mybatis:updatebyexample与updatebyexampleselective

MyBatis是一个优秀的持久层框架,它支持自定义SQL、存储过程和高级映射。MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集的过程。MyBatis可以使用简单的XML或注解来配置和映射原生类型、接口和Java的POJO为数据库中的记录。

MyBatis Generator (MBG) 中,常常会生成两个更新方法:updateByExample 和 updateByExampleSelective。

  • updateByExample 是将满足Example条件的记录全字段更新
  • updateByExampleSelective 是将满足Example条件的记录非空字段更新

按照以下步骤可以使用这两个方法:

  • 添加MyBatis依赖

在Maven中添加对应的依赖,确保你已经在POM文件中添加了对应的依赖。

  • 编写Mapper接口和xml配置文件

编写相应的Mapper接口以及相对应的XML映射文件。在这个接口中,需要有updateByExample 和 updateByExampleSelective方法。

    int updateByExampleSelective(@Param("record") Record record, @Param("example") Example example);

    int updateByExample(@Param("record") Record record, @Param("example") Example example);

在XML映射文件中,要有对应的sql语句。简单条件下,可如下实现:

<update id="updateByExampleSelective" parameterType="map" >
        update your_table
        <set >
            <if test="record.id != null" >
                id = #{record.id,jdbcType=INTEGER},
            </if>
            ......
        </set>
        <include refid="Update_By_Example_Where_Clause" />
    </update>

    <update id="updateByExample" parameterType="map" >
        update your_table
        set id = #{record.id,jdbcType=INTEGER},
        ......
        <include refid="Update_By_Example_Where_Clause" />
    </update>
  • 调用方法

在对应的service层,调用以上接口方法,如下:

    public int updateByExampleSelective(Record record, Example example){
        return recordMapper.updateByExampleSelective(record,example);
    }
    public int updateByExample(Record record, Example example){
        return recordMapper.updateByExample(record,example);
    }

在这里,“record”代表你要更新的对象,“example”就是你要更新的条件。
(iloc和loc的区别) Pandas DataFrame中loc()和iloc()的区别 Pandas中的loc()和iloc()函数:行标签vs整数位置 全网首发(图文详解1)
(object is not callable) Python报”TypeError: ‘dict’ object is not callable “的原因以及解决办法 错误通常意味着尝试像函数一样调用了一个字典对象:TypeError: ‘dict’ object is not callable 全网首发(图文详解1)

喜欢 (0)
[]
分享 (0)
关于作者:
流水不争先,争的是滔滔不绝