我在下面给出了mybatis update qry,其中更新查询以静态方式指定。在我的情况下,我必须根据传入的字段使其动态化。
@Update("UPDATE guestpayment SET " +
"SourceSystemUpdated=#{sourcesysupdated}," +
"SourceSystemUpdateComment=#{sourcesysupdatedcomments}" +
"WHERE PrimaryId=#{PrimaryId}")
void updateguestpayment(Guestpayment updateguestpayment);
如何根据传入字段作为jsonobject使更新查询动态化?
映射器 xml 对象
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.mybatis.dao.GuestpaymentDAO">
<resultMap id="Guestpayment" type="com.mybatis.mybatis.models.Guestpayment" >
<id property="PrimaryId" column="PrimaryId" javaType="int" jdbcType="integer" />
<result property="SourceSystemUpdated" column="SourceSystemUpdated" javaType="string" jdbcType="VARCHAR" />
<result property="SourceSystemUpdateComment" column="SourceSystemUpdateComment" javaType="string" jdbcType="VARCHAR" />
</resultMap>
<update id="updateguestpayment">
update guestpayment as g
<set >
<if test="updateguestpayment.SourceSystemUpdated != null and updateguestpayment.SourceSystemUpdated != ''" >
g.SourceSystemUpdated = #{updateguestpayment.SourceSystemUpdated} ,
</if>
<if test="updateguestpayment.SourceSystemUpdateComment != null and updateguestpayment.SourceSystemUpdateComment != ''">
<!-- No need to deal with commas, <set> will auto delete extra commas -->
g.SourceSystemUpdateComment = #{updateguestpayment.SourceSystemUpdateComment},
</if>
</set>
where g.PrimaryId = #{updateguestpayment.PrimaryId}
</update>
</mapper>
Guestpayment.java
package com.mybatis.mybatis.models;
import java.util.Date;
public class Guestpayment {
private int PrimaryId;
private String sourcesysupdated;
private String sourcesysupdatedcomments;
public int getPrimaryId() {
return PrimaryId;
}
public void setPrimaryId(int primaryId) {
PrimaryId = primaryId;
}
public String getSourcesysupdated() {
return sourcesysupdated;
}
public void setSourcesysupdated(String sourcesysupdated) {
this.sourcesysupdated = sourcesysupdated;
}
public String getSourcesysupdatedcomments() {
return sourcesysupdatedcomments;
}
public void setSourcesysupdatedcomments(String sourcesysupdatedcomments) {
this.sourcesysupdatedcomments = sourcesysupdatedcomments;
}
}
GuestpaymentDAO(接口)
@Mapper
@Repository
public interface GuestpaymentDAO {
void updateguestpayment(@Param("updateguestpayment") Guestpayment updateguestpayment);
}