example用来构造复杂的条件语句使用的,example里面包含了一个Criteria(他是一个条件集),Criteria中有许多Cretiron,cretiron之间是用and连接,是逻辑与关系
oredCriteria,Example内有一个成员叫oredCriteria,是Criteria的集合,就想其名字所预示的一样,这个集合中的Criteria是由OR连接的,是逻辑或关系
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);
产生的动态SQL是这样的
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)
TestTableExample example=new TestTableExample();
TestTableExample.Criteria criteria1=example.createCriteria();
criteria1.andField1EqualTo(5).andField2IsNull();
TestTableExample.Criteria criteria2=example.createCriteria();
criteria2.andAEqualTo(3).andCEqualTo(4);
example.or(criteria2); SqlSession sqlSession = MyBatisUtil.openSession();TestTableMapper m = sqlSession.getMapper(TestTableMapper.class);m.countByExample(example); //生成的sql语句select count(*) from demo WHERE ( field1 = 5 and field2 is null ) or ( a = 3 and c
= 4 )