SQL Bulk update with dynamic where clause

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

SQL Bulk update with dynamic where clause

Kapil Khanna
Hi MyBatis experts,

My requirement is to do this:

UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];

- WHERE condition has multiple conditions ANDed together and is dynamic (1 to N conditions)
- values should be parameterized (in both SET and WHERE)

Is this feasible with MyBatis?
Please advise.

Kapil

--
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: SQL Bulk update with dynamic where clause

Jeff Butler
Yes it is feasible.

If it is dynamic at runtime, then I think there are basically two options:

Option 1... you can compose the entire statement yourself in code outside of MyBatis and then pass the parameterized string to MyBatis to execute.  This is the approach I am taking with the utility I am writing here: https://github.com/jeffgbutler/mybatis-dynamic-sql.  I've been working on this utility for quite some time now and have worked out a lot of the difficulties in doing this.  I hope to get an initial release published soon.  This approach has virtually limitless flexibility.  You might also look at the built in SqlBuilder class in MyBatis which can compose a dynamic SQL string - although the parametrization must be done manually.

Option 2... you can use the dynamic SQL tags in XML, or use one of the templating languages like Velocity to generate the dynamic statement.  This is the approach I take with the current MyBatis generator.  Trying to make something completely flexible with this approach is very difficult (which is why the usage of AND and OR in the generated code is pretty limited).

If all you have is some dynamic conditions that need to be ANDed together, the code coming out of the generator might work for you.

Jeff Butler


On Tue, Oct 31, 2017 at 7:59 AM Kapil Khanna <[hidden email]> wrote:
Hi MyBatis experts,

My requirement is to do this:

UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];

- WHERE condition has multiple conditions ANDed together and is dynamic (1 to N conditions)
- values should be parameterized (in both SET and WHERE)

Is this feasible with MyBatis?
Please advise.

Kapil

--
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.