How to put if condition in myBatis datamapper.xml

16,564

use set like this:

<update id='stoIncrement' parameterType='java.util.Map'>
    update DB
    <set>
        <if test="is_increase !=null and  is_increase == 1">
            count=(select max(count) from DB) +1 ,
        </if>
        <if test="is_increase !=null and  is_increase == 0">
            count=(select max(count) from DB) -1 ,
        </if>
    </set>
    where store=#{store}
</update>
Share:
16,564
SiddP
Author by

SiddP

Software Developer

Updated on June 04, 2022

Comments

  • SiddP
    SiddP almost 2 years

    Here is what I want

    <update id='stoIncrement' parameterType='java.util.Map'>
    update DB set 
    <if test="#{is_increase} == 1">
    count=(select max(count) from DB) +1 
     </if>
    <if test="#{is_increase} == 0">
    count=(select max(count) from DB) -1 
     </if>
    where store=#{store}
    </update>
    

    Here is_increase is a key of the parameter map and is not an entity of the table.

    But the above do not seem to work. Can someone help in this regards.

    Thanks

  • SiddP
    SiddP over 8 years
    The above approach gives the following exception gives the following exception : org.apache.ibatis.exceptions.IbatisException: ### Error updating database. Cause: java.lang.NumberFormatException: For input string: "{0=null}" Is there any mistake in this <if test="#{is_increase} == 1">
  • Persia
    Persia over 8 years
    I update the test condition and check the null of is_increase