For Example (and, or) applications of Mybatis, mybatisexample

Source: Internet
Author: User

For Example (and, or) applications of Mybatis, mybatisexample

In a recent project, I have encountered the and or application of Mybatis Example. I feel it is necessary to record it. (For personal opinions, please note if you have any questions. Thank you)

 

1. In Example, each Criteria is equivalent to a bracket, and the content in the Criteria is regarded as a whole.

Where (Userid = "11" and pointname = "22 ")

BasePointsExample.Criteria criteria1 = example.createCriteria();criteria1.andUseridEqualTo("11");criteria1.andPointnameLike("22");

2. In Criteria, there is no direct or constructor (at least I don't Know), so it will be a little troublesome to use and or after the where clause.

2.1 where (condition 1 and condition 2) or (Condition 3 and Condition 4)

 BasePointsExample.Criteria criteria1 = example.createCriteria();            criteria1.andUseridEqualTo('11');            criteria1.andPointnameLike(StringUtil.concatlike('22'));  BasePointsExample.Criteria criteria2 = example.createCriteria();            criteria2.andUsernameEqualTo('33');            criteria2.andPointcontentLike(StringUtil.concatlike('44'));  example.or(criteria2);

2.2 where condition 1 and (condition 2 or condition 3) This is a problem I encountered. It feels good to see a splitting method on the Internet.

A and (B or C) ==> (A and B) or (A and C)

 BasePointsExample.Criteria criteria1 = example.createCriteria();            criteria1.andUseridEqualTo('11');            criteria1.andPointnameLike(StringUtil.concatlike('22'));            BasePointsExample.Criteria criteria2 = example.createCriteria();            criteria2.andUseridEqualTo('11');            criteria2.andPointcontentLike(StringUtil.concatlike('33'));            example.or(criteria2);

  

This is what I encountered in the project and can solve my problem. I feel that there are still better ways to find and try. This method is simple and complicated.

 

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.