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.