MyBatis Generator產生的Example類

Example類用於構造複雜的篩選條件。html

基本概念java

  • Criterion

    Criterion是最基本,最底層的Where條件,用於字段級的篩選,feild用於指代字段名字,列舉以下:mybatis

    只有一個條件,不須要其餘參考值
    feild IS NOLL
    feild IS NOT NULLapp

    與一個參考值進行算數運算
    feild > value
    feild >= value
    feild = value
    feild <> value
    feild <= value
    feild < valueui

    與一個參考值進行模糊查詢,參值中的%,?只能在構造查詢條件時手動指定。lua

    feild LIKE value
    feild NOT LIKE valuespa

    介於兩個參考值之間.net

    feild BETWEEN value AND secondValuehtm

    在或不在一個參考值集合中,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。

用法

示例來自官方文檔

 

[java] view plain copy

  1. TestTableExample example = new TestTableExample();  
  2.    
  3.   example.or()  
  4.     .andField1EqualTo(5)  
  5.     .andField2IsNull();  
  6.    
  7.   example.or()  
  8.     .andField3NotEqualTo(9)  
  9.     .andField4IsNotNull();  
  10.    
  11.   List<Integer> field5Values = new ArrayList<Integer>();  
  12.   field5Values.add(8);  
  13.   field5Values.add(11);  
  14.   field5Values.add(14);  
  15.   field5Values.add(22);  
  16.    
  17.   example.or()  
  18.     .andField5In(field5Values);  
  19.    
  20.   example.or()  
  21.     .andField6Between(3, 7);  

 

 

or()方法會產生一個新的Criteria對象,添加到oredCriteria中,並返回這個Criteria對象,從而能夠鏈式表達,爲其添加Criterion。
產生的動態SQL是這樣的:

 

[java] view plain copy

  1. where (field1 = 5 and field2 is null)  
  2.      or (field3 <> 9 and field4 is not null)  
  3.      or (field5 in (8, 11, 14, 22))  
  4.      or (field6 between 3 and 7)  

 

 

其餘

Example類的distinct字段用於指定DISTINCT查詢。

orderByClause字段用於指定ORDER BY條件,這個條件沒有構造方法,直接經過傳遞字符串值指定。

相關文章
相關標籤/搜索