Frames No Frames

Example Class Usage Notes

Using the example class is different depending on the value of the generatorSet attribute of the <abatorContext> configuration element.

If you are using the "Legacy" generator set, then see the usage notes here.

If you are using the "Java2" or "Java5" generator set, then see the usage notes here.

The example class specifies how to build a dynamic where clause. Each non-BLOB column in the can optionally be included in the where clause. Examples are the best way to demonstrate the usage of this class.

Using the example Class with the Legacy Generator Set

With this generator set the example class can be used to generate simple where clauses. This generator set is the default generator set, and classes generated work with all versions of iBATIS. However, the function of the example class if somewhat limited. If at all possible, we recommend using one of the other generator sets - but those generator sets are dependant on iBATIS 2.2.0 or higher.

This example shows how to generate a simple where clause using the generated example class:

  TestTableExample example = new TestTableExample();
  example.setField1("Fred");
  example.setField1_Indicator(TestTableExample.EXAMPLE_EQUALS);
  example.setField2(22);
  example.setField2_Indicator(TestTableExample.EXAMPLE_GREATER_THAN);
  example.setField3_Indicator(TestTableExample.EXAMPLE_NULL);

  List results = testTableDAO.selectByExample(example);

In the above example, the dynamically generated where clause will effectively be:

  where field1 = 'Fred' and field2 > 22 and field3 is null

Returned records will meet these criteria.

Note that the default combination is AND. If you want to use OR instead, the you can call the method setCombineTypeOr(true) on the example class.

The available indicators are:

Using the example Class with the Java2 or Java5 Generator Set

With these generator sets the example class can be used to generate a virtually unlimited where clauses. These generator sets require that you are using iBATIS version 2.2.0 or higher.

The example classes created with these generator sets contain an inner static class called Criteria that holds a list of conditions that will be anded together in the where clause. The outer class holds a list of Criteria objects and all the clauses from the inner classes will be ored together. Using different sets of Criteria classes allows you to generate virtually unlimited types of where clauses.

Criteria objects must be created with the createCriteria method in the example class. When the first Criteria object is created it is automatically added to the list of Criteria objects - this makes it easy to write a simple where clause if you don't need to or several other clauses together.

This example shows how to generate a simple WHERE clause using the generated example class:

  TestTableExample example = new TestTableExample();

  example.createCriteria().andField1EqualTo(5);

In the above example, the dynamically generated where clause will effectively be:

  where field1 = 5

The next example shows how to generate a complex WHERE clause using the generated example class (using JSE 5.0 parameterized types):

  TestTableExample example = new TestTableExample();

  example.createCriteria()
    .andField1EqualTo(5)
    .andField2IsNull();

  example.or(example.createCriteria()
    .andField3NotEqualTo(9)
    .andField4IsNotNull());

  List<Integer> field5Values = new ArrayList<Integer>();
  field5Values.add(8);
  field5Values.add(11);
  field5Values.add(14);
  field5Values.add(22);

  example.or(example.createCriteria()
    .andField5In(field5Values));

  example.or(example.createCriteria()
    .andField6Between(3, 7));

In the above example, the dynamically generated where clause will effectively be:

  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)

Returned records will meet these criteria.

The Criteria inner class includes andXXX methods for each field, and each possible SQL predicate including: