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条件,这个条件没有构造方法,直接经过传递字符串值指定。

相关文章
相关标签/搜索