Mybatis Dynamic Prepend

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Mybatis Dynamic Prepend

Nikita Sahu
        Hi,
        
        I have recently upgraded from mybatis version 2 to 3.3.
        Since mybatis 3.3.0 doesn't support dynamic prepend anymore, we tried to replace as shown below:
        
        version 2 query
        delete FROM
        Employee
        where
        ID_UNIQUE_KEY = #{uniqueKey}
        <dynamic prepend=" and ID_Employee_CODE not in">
        <if test="Employees != null">
        <isNotEmpty property="Employees">
        <iterate open=" (" close=") " conjunction="," property="Employees">
        <if test="Employees[].idEmployeeCode != null">
        #{Employees[].idEmployeeCode}
        </if>
        </iterate>
        </isNotEmpty>
        </if>
        </dynamic>
        
        replaced it with :
        
        delete FROM
        Employee
        where
        ID_UNIQUE_KEY = #{uniqueKey}
        <if test="Employees != null and Employees.size() &gt; 0">
        and ID_Employees_CODE not in
        <foreach close=")" collection="Employees" index="index" item="item" open="(" separator=",">
        <if test="item != null and item.idEmployeeCode != null">
        #{item.idEmployeeCode}
        </if>
        </foreach>
        </if>
        
        
        
        The problem with this is 
        If i have a list of not null DTOs(Employees) but the attribute of dto, which is being used in the query(idEmployeeCode), is null, then in that case AND clause(  and ID_Employees_CODE not in) gets appended to the query with empty (), hence failing it.
    
    We can handle it in java as well..before calling these queries.
    Though is there any way to handle it here in queries, considering the amount of queries affected by this is huge.
        
        
        This was working fine with dynamic prepend.
        
        Any idea on this?
        
        
        Thanks & Regards,
        Nikita

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Mybatis Dynamic Prepend

Erwan Letessier
I guess you just need to move prepend text into the open attribute of the foreach.


On Feb 20, 2018 12:39, "Nikita Sahu" <[hidden email]> wrote:
        Hi,
        
        I have recently upgraded from mybatis version 2 to 3.3.
        Since mybatis 3.3.0 doesn't support dynamic prepend anymore, we tried to replace as shown below:
        
        version 2 query
        delete FROM
        Employee
        where
        ID_UNIQUE_KEY = #{uniqueKey}
        <dynamic prepend=" and ID_Employee_CODE not in">
        <if test="Employees != null">
        <isNotEmpty property="Employees">
        <iterate open=" (" close=") " conjunction="," property="Employees">
        <if test="Employees[].idEmployeeCode != null">
        #{Employees[].idEmployeeCode}
        </if>
        </iterate>
        </isNotEmpty>
        </if>
        </dynamic>
        
        replaced it with :
        
        delete FROM
        Employee
        where
        ID_UNIQUE_KEY = #{uniqueKey}
        <if test="Employees != null and Employees.size() &gt; 0">
        and ID_Employees_CODE not in
        <foreach close=")" collection="Employees" index="index" item="item" open="(" separator=",">
        <if test="item != null and item.idEmployeeCode != null">
        #{item.idEmployeeCode}
        </if>
        </foreach>
        </if>
        
        
        
        The problem with this is 
        If i have a list of not null DTOs(Employees) but the attribute of dto, which is being used in the query(idEmployeeCode), is null, then in that case AND clause(  and ID_Employees_CODE not in) gets appended to the query with empty (), hence failing it.
    
    We can handle it in java as well..before calling these queries.
    Though is there any way to handle it here in queries, considering the amount of queries affected by this is huge.
        
        
        This was working fine with dynamic prepend.
        
        Any idea on this?
        
        
        Thanks & Regards,
        Nikita

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Mybatis Dynamic Prepend

Jeff Butler
That fails if all the items in the forEach loop fail the nested if test (you still get the empty in statement).  I put an answer on the stack overflow question that works for all cases (it uses the trim tag).

On Tue, Feb 20, 2018 at 1:04 PM Erwan Letessier <[hidden email]> wrote:
I guess you just need to move prepend text into the open attribute of the foreach.


On Feb 20, 2018 12:39, "Nikita Sahu" <[hidden email]> wrote:
        Hi,
        
        I have recently upgraded from mybatis version 2 to 3.3.
        Since mybatis 3.3.0 doesn't support dynamic prepend anymore, we tried to replace as shown below:
        
        version 2 query
        delete FROM
        Employee
        where
        ID_UNIQUE_KEY = #{uniqueKey}
        <dynamic prepend=" and ID_Employee_CODE not in">
        <if test="Employees != null">
        <isNotEmpty property="Employees">
        <iterate open=" (" close=") " conjunction="," property="Employees">
        <if test="Employees[].idEmployeeCode != null">
        #{Employees[].idEmployeeCode}
        </if>
        </iterate>
        </isNotEmpty>
        </if>
        </dynamic>
        
        replaced it with :
        
        delete FROM
        Employee
        where
        ID_UNIQUE_KEY = #{uniqueKey}
        <if test="Employees != null and Employees.size() &gt; 0">
        and ID_Employees_CODE not in
        <foreach close=")" collection="Employees" index="index" item="item" open="(" separator=",">
        <if test="item != null and item.idEmployeeCode != null">
        #{item.idEmployeeCode}
        </if>
        </foreach>
        </if>
        
        
        
        The problem with this is 
        If i have a list of not null DTOs(Employees) but the attribute of dto, which is being used in the query(idEmployeeCode), is null, then in that case AND clause(  and ID_Employees_CODE not in) gets appended to the query with empty (), hence failing it.
    
    We can handle it in java as well..before calling these queries.
    Though is there any way to handle it here in queries, considering the amount of queries affected by this is huge.
        
        
        This was working fine with dynamic prepend.
        
        Any idea on this?
        
        
        Thanks & Regards,
        Nikita

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.