mybatis 按照 map 动态进行 insert 和 update

 

按照 map 动态 进行 insert 和 update

<!--更新用户数据-->
    <update id="update" parameterType="com.Data">
        update loy_data
        <set>
            <if test="deleteFlg != null">
                DELETE_FLG = #{deleteFlg},
            </if>
            UPDATED_AT = #{updatedAt},
            UPDATED_BY = #{updatedBy}
            <foreach collection="fields" index="key" item="value">
                , ${key} = #{value}
            </foreach>
        </set>
        where id = #{id}
    </update>

    <!--新增用户数据-->
    <insert id="insert" parameterType="com.Data" keyProperty="id" useGeneratedKeys="true">
        insert into loy_data(id, DELETE_FLG, CREATED_AT, CREATED_BY, UPDATED_AT, UPDATED_BY
        <foreach collection="fields.keys" index="index" item="key">
            , ${key}
        </foreach>
        )
        VALUES
        (#{id}, #{deleteFlg}, #{createdAt}, #{createdBy}, #{updatedAt}, #{updatedBy}
        <foreach collection="fields.values" index="index" item="value">
            , #{value}
        </foreach>
        )
    </insert>
/**
 * (LoyData)实体类
 *
 * @author wanghang
 * @since 2020-05-14 17:49:31
 */
@SequenceId(property = "id")
public class LoyData implements Serializable {

    private static final long serialVersionUID = 238203000278523870L;

    /**
     * 数据ID
     */
    private Long id;
    /**
     * 删除标志 1:删除 0:不删除
     */
    private Integer deleteFlg;
    /**
     * 创建时间
     */
    private Long createdAt;
    /**
     * 创建人
     */
    private Long createdBy;
    /**
     * 最近更新时间
     */
    private Long updatedAt;
    /**
     * 最近更新人
     */
    private Long updatedBy;
    /**
     * 动态列
     */
    private Map<String, Object> fields;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getDeleteFlg() {
        return deleteFlg;
    }

    public void setDeleteFlg(Integer deleteFlg) {
        this.deleteFlg = deleteFlg;
    }

    public Long getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Long createdAt) {
        this.createdAt = createdAt;
    }

    public Long getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(Long createdBy) {
        this.createdBy = createdBy;
    }

    public Long getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(Long updatedAt) {
        this.updatedAt = updatedAt;
    }

    public Long getUpdatedBy() {
        return updatedBy;
    }

    public void setUpdatedBy(Long updatedBy) {
        this.updatedBy = updatedBy;
    }

    public Map<String, Object> getFields() {
        return fields;
    }

    public void setFields(Map<String, Object> fields) {
        this.fields = fields;
    }

    @Override
    public String toString() {
        return "LoyaltyData{" +
                "id=" + id +
                ", deleteFlg=" + deleteFlg +
                ", createdAt=" + createdAt +
                ", createdBy=" + createdBy +
                ", updatedAt=" + updatedAt +
                ", updatedBy=" + updatedBy +
                ", fields=" + fields +
                '}';
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 在你提供的代码中,使用了MyBatis的foreach标签来遍历map进行更新操作。在XML配置文件中,你可以看到在update语句中使用了foreach标签来遍历ids集合,并将每个id作为参数传递给SQL语句中的占位符。具体来说,foreach标签的collection属性指定了要遍历的集合,item属性指定了每次遍历的元素,open属性指定了遍历开始时的字符串,separator属性指定了每个元素之间的分隔符,close属性指定了遍历结束时的字符串。在service层的代码中,你可以看到将ids和operation封装到了一个map中,并将该map作为参数传递给了allSubmitOrder方法。这样,在执行SQL语句时,MyBatis会根据map中的键值对来替换SQL语句中的占位符,从而实现遍历map进行更新的功能。 #### 引用[.reference_title] - *1* [mybatis遍历Map](https://blog.csdn.net/qq_40852612/article/details/116658500)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [mybatismap遍历多种类型-批量更新](https://blog.csdn.net/cqzdmm/article/details/116943491)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值