1

Mybatis can operate input parameters data with "if" and "foreach", but how about the data just queried within same Mybatis sql?

case:

table field 1

select filed from table; will get 1

Can Mybatis manage to update field to 2 with this kind of statement? :

<update id="updateField"> 
    select field from table;
    <if test="field != 0 ">
        update table 
        set field = 2
        where field = 1;
    </if>
<update>
wo4wangle
  • 99
  • 6
  • No, that cannot be done. The parameters are evaluated when rendering the SQL statement before the block is sent to the database engine, not during it's execution. For a functionality like this one to work a stored procedure would be my solution of choice. – The Impaler Mar 01 '22 at 00:33
  • Although I would agree with the stored procedure, if this isn't possible (for whatever reason) you can try to use the `selectKey` property of the query to populate `field` of your model and then modify the query based on the key field you just got back. – kendavidson Mar 04 '22 at 18:51

0 Answers0