亲宝软件园·资讯

展开

mybatis Criteria用法or与isNull

杨杨1234 人气:2

mybatis Example的Criteria用法or与isNull

1.or

BaUserExample baUserExample = new BaUserExample();
Criteria criteria1 = baUserExample.createCriteria();
criteria1.andOrgIdEqualTo(“1”);
criteria1.andDeptIdEqualTo(“1”);
Criteria criteria2 = baUserExample.createCriteria();
criteria2.andUserNameEqualTo(“name”);
criteria2.andEmailLike("%test@%");
baUserExample.or(criteria2);
userMapper.countByExample(baUserExample);

2.isNull

为空要用isNull() 不要用equalTo(null) ,否则会报错。

mybatis Criteria的使用

查询名字中带有字母 r ,性别为男性(1)或者email中带有数字1的人员信息

@Test
 public void test1() throws IOException {
  SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
  SqlSession openSession = sqlSessionFactory.openSession(true);
  try {
   EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
//   List<Employee> selectAll = mapper.selectByExample(null);
   EmployeeExample example = new  EmployeeExample();
   Criteria criteria = example.createCriteria();
   criteria.andNameLike("%r%");
   criteria.andGenderEqualTo("1");
   Criteria criteria2 = example.createCriteria();
   criteria2.andEmailLike("%1%");
   example.or(criteria2);
   List<Employee> select = mapper.selectByExample(example);
   for(Employee employee :select) {
   System.out.println(employee.getName()+employee.getEmail());
   }
  }finally {
   openSession.close();
  }
 }

1.先实例化一个EmployeeExample对象

2.调用createCriteria()方法

3.查询名字中带有字母 r ,性别为男性(1)之间是and关系

criteria.andNameLike("%r%");   
criteria.andGenderEqualTo("1");

4.查询email中带有数字1,并且已第一个criteria为基准与其进行or运算

Criteria criteria2 = example.createCriteria();   
criteria2.andEmailLike("%1%"); 
example.or(criteria2);

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论