0

我在下面给出了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);

}
4

2 回答 2

0

您可以使用它Criteria来创建动态更新查询。在你的情况下,你可以像下面那样做,

public class GuestPaymentDAO {

    @PersistenceContext
    private EntityManager em;

   public void updateGustPayment(String sourcesysupdated, String sourcesysupdatedcomments, int PrimaryId) {
       CriteriaBuilder cb = this.em.getCriteriaBuilder();

       // create update
       CriteriaUpdate<Guestpayment> update = cb.createCriteriaUpdate(Guestpayment.class);

       // set the root class
       Root e = update.from(Guestpayment.class);

       // set update clause
       if (sourcesysupdated != null) {
           update.set("SourceSystemUpdated", sourcesysupdated);
       }
       if (sourcesysupdatedcomments != null) {
           update.set("SourceSystemUpdateComment", sourcesysupdatedcomments);
       }
       // set where clause
       update.where(cb.equal(e.get("PrimaryId"), PrimaryId));

       // perform update
       this.em.createQuery(update).executeUpdate();
   }
}
于 2021-02-07T08:48:13.753 回答
0

为什么不使用mybatis xml和entity相互配合呢?
例如:
DAO

@Mapper
@Repository
public interface GuestpaymentDAO {

    void updateguestpayment(@Param("updateguestpayment") Guestpayment updateguestpayment);

}

mybatis mapper xml文件:GuestpaymentDAO.xml

<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.dao.GuestpaymentDAO">
<resultMap id="Guestpayment" type="com.example.demo.entity.Guestpayment" >
    <id property="PrimaryId" column="PrimaryId" javaType="string" jdbcType="VARCHAR" />
    <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>

文档:https
://mybatis.org/mybatis-3/sqlmap-xml.html src:src 图像

于 2021-02-07T12:08:03.020 回答