MyBatishml2Dynamic SQL
Preface
demo Project address: https://gitee.com/shuashua-world/shuashua-blog/tree/master/demo/mybatis-simple-demo
MyBatisDynamic SQL Element types: two conditional judgments, one controls the prefix and suffix value, and one is used for traversal.
- if
- choose (when, otherwise)
- trim (where, set)
- foreach
if tag
if tag is used for conditional judgment. The most common scenario for using dynamic SQL is to include part of a where clause based on the condition. For example:
only uses the if tag, which is the following situation:
choose (when, otherwise) tag
is similar to if tag, and is used for conditional judgment. Sometimes, we don't want to use all the conditions, but just want to choose one of the conditions to use. For this case, MyBatis provides a choice element, which is a bit like the switch statement in Java.
trim (where, set) Tags
where and set tags can be considered as a subset of trim tags. The functions they can provide, trim can provide.
where tag
where
and where elements:
trim prefix="WHERE" prefixOverrides="AND |OR ".../trim
where tag usage demo:
set tag
set element can be used to dynamically contain columns that need to be updated, and other columns that are not updated are ignored. . The settml2 element dynamically inserts the SET keyword at the beginning of the row and deletes the extra comma (these commas are introduced when assigning values to the column using conditional statements). The custom trim element equivalent to
and set elements are:
trim prefix="SET" suffixOverrides=",".../trim
set tag usage demo:
trim tag
trim tag can be used for pre-suffix processing.
A complete trim tag: trim prefix="" prefixOverrides="" suffix="" suffixOverrides="" suffixOverrides=""/trim
trim tag four properties:
- prefix: Insert the prefix when the child element returns anything.
- suffix: inserts the suffix if the child element returns anything.
- prefixOverrides: removes all content specified in the prefixOverrides attribute. The prefixOverrides attribute ignores the text sequence separated by the pipe character, that is, multiple contents can be separated by the pipe character|.
- suffixOverrides: Similar to prefixOverrides.
foreach
A common usage scenario is to traverse the collection (especially when building IN conditional statements).
foreach element has very powerful functions. It allows you to specify a collection that declares collection items (item) and index variables that can be used in the element body. It also allows you to specify the separator between the string at the beginning and the end, and the iteration of the collection item. This element also does not add extra separators incorrectly.
It can pass any iterable object (such as List, Set, etc.), Map object or array object as collection parameters to foreach. When using an iterable object or array, index is the sequence number of the current iteration, and the value of item is the element obtained in this iteration. When using Map objects (or collections of Map.Entry objects), index is the key and item is the value.