Mybatis common tags and dynamic queries

This is the back-end small class of the monastery. Each article is shared from

[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]

Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:

[mybatis common tags and dynamic query]

[Java class] topic name of Xiuzhen Academy

Opening remarks:

Hello, I'm Cui Zhen, the 13th student of Zhengzhou branch of it Academy. I'm an honest, pure and kind java programmer. Today, I'd like to share with you the back-end (Professional) task 1 of the Academy's official website, the knowledge points in deep thinking - mybatis common tags and dynamic queries

(1) Background:

Mybatis framework is a persistence layer framework, which is a top-level project under Apache. Mybatis allows developers to focus on SQL and generate SQL statements freely and flexibly through the mapping method provided by mybatis. After using mybati, you only need to provide SQL statements, and other work is left to mybatis, such as creating connections, statements, and JDBC related exception handling. These repetitive tasks are entrusted to mybatis. We only need to focus on the addition, deletion, query and modification operations, while other repetitive tasks before us are encapsulated by mybatis. This makes the code more concise, easier to interpret and easier to maintain

(2) Knowledge analysis:

label

The if element is the most commonly used judgment statement, which is equivalent to the if statement in Java. It is often used in combination with the test attribute. If test is equivalent to judgment. If the conditions in the label are met, add the internal conditions on the SQL statement. We will talk about the code practice in detail below

label

The where tag is often used with the if tag. The where tag will judge whether the conditions in the if tag are true. If the conditions are true, the SQL statements under the conditions will be spliced and executed. The where tag avoids the error situation of "select * from user where" when all conditions are empty. Of course, we can also add "1 = 1" after the where to solve it simply.

label

The set tag is generally used in the update statement. The set tag is similar to the where tag. When we encounter problems related to multiple fields in the update statement, we will use the set tag. It also judges whether the conditions in the if tag are true, and then splices and executes the SQL statement.

label

Compared with the if tag, the choose tag has a third choice, or more choices. Use choose.. in the dynamic statement of the mapper when.. otherwise.. These three elements can meet different business requirements. Mybatis will judge according to the parameter settings to assemble SQL statements. When no conditions are met under the when tag, the statements in the otherwise tag will be executed.

label

Foreach element is a circular statement. Its function is to traverse the collection. It can well support the collection of arrays and lists. It also provides traversal. Foreach tags are often used in syntax such as in for batch processing

(3) Frequently asked questions:

The difference between if tag and choose tag

(4) Solution:

If means that the next if will be executed regardless of whether the condition is true or not; In choose, if the execute first judgment in when is true, it will not be executed later.

(5) Coding practice:

(6) Expand thinking:

Is there any other way for mybatis besides using XML?

You can also change XML to mapper interface

(7) References:

http://how2j.cn/k/mybatis/mybatis-tutorial/1087.html

http://www.mybatis.org/mybatis-3/

Q1: questioner: why use dynamic query A1: respondent: because you are not sure what users will enter and what fields they want to search, we use dynamic query to judge the content entered by users and query accordingly. Q2: questioner: why should where1 = 1 be added after the SQL statement A2: respondent: because if the next condition judgment is not true, the SQL statement will be select * from student where when querying, and an error will occur when passing in such SQL statement, because there is no judgment condition after where, So we add a judgment condition that 1 = 1 is always true after where, and the SQL statement will not make mistakes. Q3: questioner: what is the difference between if tag and choose tag

A3 respondent: If yes, the next if will be executed regardless of whether the condition is true or not; In choose, if the execute first judgment in when is true, it will not be executed later.

(9) Thanks:

Thanks to elder martial brother Zhang Yaqiang. This tutorial is based on their previous technology sharing.

(10) Conclusion:

That's all for today's sharing. You are welcome to like, forward, leave messages and make bricks~

Ppt link video link

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>