In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The editor will share with you what dynamic tags MyBatis has. I hope you will get something after reading this article. Let's discuss it together.
Preface
MyBatis provides 9 kinds of dynamic SQL tags: trim, where, set, foreach, if, choose, when, otherwise, bind
Its execution principle is that it uses OGNL to calculate the value of the expression from the SQL parameter object, and dynamically splices the SQL according to the value of the expression, so as to complete the function of dynamic SQL.
Dynamic label usage 1.if
If: a condition is executed only when the parameter meets the condition
SELECT stu.name FROM tab_stu stu WHERE age = 20 AND name like # {name} 2.choose, when, otherwise
Choose, when, otherwise: the choose tag determines in order whether the test condition in its internal when tag is true, and if one is true, the choose ends; if all the when conditions are not met, the SQL in the otherwise is executed. Similar to the switch statement of java.
SELECT stu.name FROM tab_stu stu WHERE age = # {age} AND name like # {name} AND class like # {class} AND class = 1 3.where SELECT stu.name FROM tab_stu stu WHERE age = # {age} AND name= # {name} AND class = # {class}
When the first if is dissatisfied or the first, second and third if is not satisfied, the following occurs
SELECT stu.name FROM tab_stu stu WHERE AND name = "millet" AND class = "Class 1"; SELECT stu.name FROM tab_stu stu WHERE
This causes the query to fail. Using where tags can solve this problem.
SELECT stu.name FROM tab_stu stu age = # {age} AND name= # {name} AND class = # {class}
The where tag inserts the WHERE keyword only if more than one if condition is met, and if the final content begins with "AND" or "OR", where will definitely retain it according to the syntax.
4.set
Set tags are used to solve the symbol problems in dynamic update statements.
Update tab_stu name=# {name}, age=# {age}, class=# {class}, subject=# {subject}
The set tag dynamically preplaces the SET keyword and eliminates extraneous commas, because conditional statements may leave commas after the generated assignment statements.
5.trim
Trim:trim tag can realize the function of where/set tag.
The Trim tag has four attributes, which are prefix, suffix, prefixOverrides and suffixOverrides
Prefix: indicates that the specified content is added before the SQL of the trim tag package
Suffix: indicates that the specified content is added to the end of the SQL of the trim tag package
PrefixOverrides: removes (overwrites) the specified header content of the trim tag package, and removes multiple content written as and | or (space cannot be omitted) (generally used to remove redundant AND in if judgment | OR)
SuffixOverrides: indicates the removal (overwriting) of the SQL specified tail content of the trim tag package (usually used to remove extra commas when if judgment in update statements)
SELECT stu.name FROM tab_stu stu age = # {age} AND name= # {name} OR class = # {class} Update tab_stu Name=# {name} Age=# {age}, class=# {class}, subject=# {subject} 6.foreach
Foreach: traversing the collection
SELECT stu.name FROM tab_stu stu where id in # {item}
Here are the attributes of the foreach tag:
Collection: the name of the iterative collection, which can be specified using the @ Param annotation. This parameter is required (java input parameter, relative to # {listName})
Item: indicates the element obtained in this iteration. If collection is List, Set or array, it represents the element; if collection is Map, it represents the value of key-value. This parameter is required.
Index: in List, Set, and array, index represents the location of the current iteration, and in Map, index refers to the key of the element, which is optional
Open: indicates what the statement begins with. The most common use is the left parenthesis "(". MyBatis splices this character before the SQL statement wrapped by the foreach tag, and only once. This parameter is optional.
Close: indicates what the statement ends with, the most commonly used is the right bracket ")". MyBatis splices this character to the end of the SQL statement wrapped by the foreach tag. This parameter is optional.
Separator:MyBatis adds the character specified by the separator attribute to the SQL statement after each iteration, and this parameter is optional
7.bind
The bind:bind tag can create a variable from an OGNL (object Graph Navigation language) expression and bind it to the context
The use of bind is also involved in fuzzy query string concatenation (like) using Mysql in Mybatis.
SELECT stu.name FROM tab_stu stu name like # {stuName} finished reading this article I believe you have a certain understanding of "what dynamic tags MyBatis has". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for your reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.