按照 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 +
'}';
}
}