Mybatis & lt; choose & gt; & lt; when & gt;, mybatischoose

Source: Internet
Author: User

Mybatis uses <choose> <when>, mybatischoose

I. Requirements

The backend uses the orcale database and mybatis as the persistent layer. The frontend search function searches by type, but the database does not contain type fields,

Therefore, you must make a judgment in the where Condition Statement. When type = x1 and type = x2, the judgment conditions in where are different.

Ii. Solution

<Select id = "" resultMap = "">

Select * from table

<Where>

<If test = 'Type = "x1" '>

And condition 1;

</If>

<If test = 'Type = "x2" '>

And condition 2;

</If>

</Where>

</Select>

Or

<Select id = "" resultMap = "">

Select * from table

<Choose>

<When test = "type = 'x1'">

Where condition 1;

</When>

<When test = "type = 'x2'' ">

Where condition 2;

</When>

<Otherwise>

If none of the conditions in the when are met, use condition 3;

</Otherwise>

 

</Choose>

<If test = "type = 'x2 '"> // if there are other judgment conditions in addition to the preceding conditions, put them in the chose tag, and do not need to write where

And condition 2;

</If>

</Select>

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.