2

<sql> element like this:

<sql id="update_fragment">
    <set>
        <if test="id != null">
            id = #{id},
        </if>
        <if test="name != null">
            id = #{name},
        </if>
        ...
    </set>
</sql>        

And it's used in a <update>:

<update id="update" parameterType="MyTableBean">
    UPDATE MyTable
    <include refid="update_fragment"/>
    WHERE id =# {id}
</update>

In DAO:

Integer update(MyTableBean myTableBean);

Now, I write a batchUpdate method:

Integer batchUpdate(@Param("ids")List<Integer> ids, @Param("bean")MyTableBean myTableBean);

And I want to re-use the 'update_fragment' <sql> element.

<update id="update">
    UPDATE MyTable
    <include refid="update_fragment"/>
    WHERE ids IN
    <foreach>...</foreach>
</update>

But it seems the scope in 'update_fragment' is wrong.
It seems that I have to rewrite a <sql> which changes #{id} to #{bean.id}.

It's there any way to re-use the statement without rewrite a new <sql>.

illuz
  • 23
  • 1
  • 5

1 Answers1

0

You can try to add @Param annotation to your update method:

Integer update(@Param("bean") MyTableBean myTableBean);

And then correct your sql expression:

<sql id="update_fragment">
<set>
    <if test="id != null">
        id = #{bean.id},
    </if>
    <if test="name != null">
        id = #{bean.name},
    </if>
    ...
</set>

So this will add some universatility for you.

KillSwitch
  • 132
  • 2
  • 10