In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail how to implement dynamic SQL in MyBatis! The content of the article is of high quality, so the editor shares it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
I. if tag
If is the most commonly used tag, often used in judgment statements, can achieve some simple conditional selection. Basic examples of usage are as follows:
Select * from user where 1 and name = # {name} and age = # {age}
II. Where tags
In the above example, "1x 1" is used to avoid reporting errors when the subsequent conditions are not met. Is there any way to avoid this kind of writing?
Of course, there is, that is, the where,where tag to be mentioned next will automatically determine that if there is a return value in the tag, insert a where into the sql. If the last content returned by the where tag begins with and or or, it will also be automatically removed. In the above example, it will be written as follows after changing to the where tag:
Select * from user and name = # {name} and age = # {age}
III. Trim tags
The role of trim is to remove special strings, its prefix attribute represents the prefix of the statement, the prefixOverrides attribute represents which special strings need to be removed, and the prefixOverrides attribute ignores characters separated by pipes, and suffixes are treated the same as prefixes.
The main attributes of the trim tag are as follows
Prefix: the prefix overrides and increases its content.
Suffix: the suffix overrides and increases its content.
PrefixOverrides: the condition for prefix judgment.
SuffixOverrides: the condition of suffix judgment.
Give me two examples.
Use prefix attribut
Select * from user and name = # {name} or sex = # {sex} and age = # {age}
Use suffix attribut
UPDATE user, id=# {id}, name=# {name}, age=# {age} WHERE ID=# {id}
IV. Foreach tag
The function of foreach is to traverse collections. It can well support the traversal of arrays and collections of List and Set interfaces, and often has more combinations with in in sql.
The main attributes of the foreach tag are as follows
Item: represents the current element in the loop.
Index: represents the subscript of the current element at the location of the collection.
Collection: configure the property name of list, and so on.
Open and close: what symbols are configured to wrap these collection elements.
Separator: the separator of each element is configured.
For example:
Select * from user where id in # {id} about how to implement dynamic SQL in MyBatis! So much for sharing here. I hope the above content can help you to some extent and learn more knowledge. If you think the article is good, you can share it for more people to see.
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.