MyBatis use of Trim tags in dynamic sql

Source: Internet
Author: User


The My Batis official documentation shows less of the scenarios and effects of using trim tags in dynamic SQL.

In fact the trim tag is somewhat similar to the replace effect.

Trim Property

Prefix: Prefixes overwrite and increase their contents

Suffix: suffixes overwrite and increase their contents

Prefixoverrides: Criteria for judging prefixes

Suffixoverrides: The condition of the suffix judgment

Like what:

Java code
  1. Select b.* from Sys_menu b where 1 = 1
  2. <trim suffix="WHERE" suffixoverrides="and | OR ">
  3. <if test="id! = NULL and ID! =" ">
  4. and b.ID =#{id}
  5. </if>
  6. <if test="Name! = NULL" >
  7. and b.menu_name like #{name}
  8. </if>
  9. </trim>

The final SQL print is:

Select b.* from Sys_menu b where 1 = 1 and b.menu_name "where"

From the results can be found:

Java code
    1. <trim suffix="WHERE" suffixoverrides="and | OR ">

Suffix is the append suffix suffix value for suffixoverrides compliant SQL statements.

Word:

Java code
    1. <trim suffix="WHERE" suffixoverrides="and | OR ">

and sqlxxx

The end result is:

and Sqlxxx WHERE

I'm not quite sure of the specifics of this usage, but many of the tags are sufficient for the current MyBatis dynamic SQL statement.

MyBatis use of Trim tags in dynamic sql

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.