[MyBatis] Learning Summary Six: Dynamic SQL

introduction

Dynamic SQL: Dynamic SQL.

In this section, we will learn through the official documentation of MyBatis.

Description

The official description is as follows:

One of the most powerful features of MyBatis has always been its Dynamic SQL capabilities. If you have any experience with JDBC or any similar framework, you understand how painful it is to conditionally concatenate strings of SQL together, making sure not to forget spaces or to omit a comma at the end of a list of columns. Dynamic SQL can be downright painful to deal with.

While working with Dynamic SQL will never be a party, MyBatis certainly improves the situation with a powerful Dynamic SQL language that can be used within any mapped SQL statement.

The Dynamic SQL elements should be familiar to anyone who has used JSTL or any similar XML based text processors. In previous versions of MyBatis, there were a lot of elements to know and understand. MyBatis 3 greatly improves upon this, and now there are less than half of those elements to work with. MyBatis employs powerful OGNL based expressions to eliminate most of the other elements:

● if ● choose (when, otherwise) ● trim (where, set) ● foreach

My View

The above passage can be summed up in three points:

  • MyBatis is very powerful in dynamic SQL.

  • You need to know OGNL expressions

  • if/choose/trim/foreach

OGNL

See here for usage: OGNL Guide

Also, take a look at escaping:

character decimal escape character
" " "
& & &
< < <
> > >

if/where/trim/choose/foreach

  • if means judgment
  <if test="title != null">
    AND title like #{title}
  </if>
  • where

We always write AND in the if tag, then there is a problem when the first one doesn't exist: sql: ...WHERE AND ...

So whereit solves the problem

  <where>
    <if test="state != null">
         state = #{state}
    </if>
    <if test="title != null">
        AND title like #{title}
    </if>
    <if test="author != null and author.name != null">
        AND author_name like #{author.name}
    </if>
  </where>
  • trim

Some people will say, since there is a problem with writing it in the front, can it be written in the back? of course can.

Let's see some usage of trim:

<trim prefix="WHERE" prefixOverrides="AND |OR ">
  ...
</trim>

-----

<trim prefix="SET" suffixOverrides=",">
  ...
</trim>

To sum up: prefix: what to add before prefixOverrides: what to ignore before suffix: suffix suffixOverrides: what to ignore after

  • choose
  <choose>
    <when test="title != null">
      AND title like #{title}
    </when>
    <when test="author != null and author.name != null">
      AND author_name like #{author.name}
    </when>
    <otherwise>
      AND featured = 1
    </otherwise>
  </choose>
  • foreach

Traverse a collection.

  WHERE ID in
  <foreach item="item" index="index" collection="list"
      open="(" separator="," close=")">
        #{item}
  </foreach>

postscript

I'm here today, and I'll add it later if I have an insight.

Test code: DynamicSQL-Demo

It should be noted that in order to maintain the test style, some return value types are incorrect, resulting in test errors, but this does not affect the learning of the section, because you can see the sql and the results.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324990336&siteId=291194637