[MyBatis series ③] Dynamic SQL

Table of contents

1 Introduction

2. if tag

3. foreach tag

4. SQL extraction


⭐MyBatis series ①: add, delete, modify and check

⭐MyBatis Series②: Two Dao development methods

1. Introduction

During development, SQL statements are configured in the MyBatis mapping file, but the previous configurations are relatively simple and do not involve slightly complicated business scenarios. To deal with complex business scenarios, dynamic SQL needs to be introduced.

MyBatis's dynamic SQL is a technology that generates different SQL fragments according to different conditions in SQL statements. It enables you to dynamically add, modify or exclude some SQL codes in SQL statements according to different situations, so as to flexibly build database queries or operation statements. Dynamic SQL is very useful in handling different query conditions, sorting, paging, etc., and can effectively reduce repeated SQL codes.

The screenshot of the official website is as follows:

MyBatis 3 has greatly improved on this and now has less than half the elements available. MyBatis uses powerful OGNL-based expressions to eliminate most other elements

MyBatis provides four tags:

Label

introduce

<if>

Conditional judgment, dynamically add or exclude part of the code in the SQL statement.

<choose>

Similar to the switch statement, different code blocks are selected to be executed according to the condition.

<trim>

Trim redundant connectives in SQL for dynamically generating WHERE or SET clauses.

<foreach>

Loop through collections, often used to generate repetitive structures such as IN clauses.

The focus here is if and foreach

2. if tag

We use different SQL statements to query according to the different values ​​of the entity class.

For example, if the id is not empty, it can be queried according to the id, and if the username is not empty, the user name must be added as a condition.

This situation is often encountered in our multi-condition combination query.

Use as follows:

The <where> tag here will automatically identify whether where is needed. For example, when all the conditions are not satisfied, it means that there is no need to splice the sql statement, and the execution is select * from user . If you remove the <where>, you need to use the following writing method To achieve the effect of conditional query:

It is not as beautiful to read as using the where tag.

3. foreach tag

The attributes of the foreach tag have the following meanings:

The <foreach> tag is used to traverse the collection, its attributes:

  1. collection: Represents the collection elements to be traversed, be careful not to write #{} when writing
  2. open: represents the beginning of the statement
  3. close: represents the end part
  4. item: represents the variable name generated by traversing each element of the collection
  5. sperator: represents the separator

Loop the splicing operation of SQL, for example: SELECT * FROM USER WHERE id IN (1,2,5) .

Test Results:

4. SQL extraction

Duplicated sql can be extracted from Sql, and can be referenced by include when using it, so as to achieve the purpose of sql reuse.

用法:<sql id="selectUser" select * from User</sql>

Guess you like

Origin blog.csdn.net/qq_60735796/article/details/132481914