MyBatis Generator產生的Example類
http://openwares.net/database/mybatis_generator_example.html
Example類用於構造複雜的篩選條件。
基本概念
- Criterion
Criterion是最基本,最底層的Where條件,用於欄位級的篩選,feild用於指代欄位名字,列舉如下:
只有一個條件,不需要其他參考值
feild IS NOLL
feild IS NOT NULL與一個參考值進行算數運算
feild > value
feild >= value
feild = value
feild <> value
feild <= value feild < value 與一個參考值進行模糊查詢,參值中的%,?只能在構造查詢條件時手動指定。 feild LIKE value feild NOT LIKE value 介於兩個參考值之間 feild BETWEEN value AND secondValue 在或不在一個參考值集合中,item來自於value集合 feild IN (item,item,item,...) feild NOT IN (item,item,item,...) MyBatis Generator會為每個欄位產生如上的Criterion,如果表的欄位比較多,產生的Example類會十分龐大。理論上通過Example類可以構造你想到的任何篩選條件。 - Criteria
Criteria包含一個Cretiron的集合,每一個Criteria物件內包含的Cretiron之間是由AND連線的,是邏輯與的關係。
- oredCriteria
Example內有一個成員叫oredCriteria,是Criteria的集合,就想其名字所預示的一樣,這個集合中的Criteria是由OR連線的,是邏輯或關係。oredCriteria就是ORed Criteria。
用法
示例來自官方文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
TestTableExample
example = new
TestTableExample();
example.or()
.andField1EqualTo( 5 )
.andField2IsNull();
example.or()
.andField3NotEqualTo( 9 )
.andField4IsNotNull();
List<Integer>
field5Values = new
ArrayList<Integer>();
field5Values.add( 8 );
field5Values.add( 11 );
field5Values.add( 14 );
field5Values.add( 22 );
example.or()
.andField5In(field5Values);
example.or()
.andField6Between( 3 ,
7 );
|
or()方法會產生一個新的Criteria物件,新增到oredCriteria中,並返回這個Criteria物件,從而可以鏈式表達,為其新增Criterion。
產生的動態SQL是這樣的:
1 2 3 4 |
where
(field1 = 5 and
field2 is
null )
or
(field3 <> 9 and
field4 is
not
null )
or
(field5 in
(8, 11, 14, 22))
or
(field6 between
3 and
7)
|
其他
Example類的distinct欄位用於指定DISTINCT查詢。
orderByClause欄位用於指定ORDER BY條件,這個條件沒有構造方法,直接通過傳遞字串值指定。