mybatis Example条件查询

目录:
  or条件查询时,结合0、1两个例子得出差异
  Criteria添加条件时如果遇上or条件时,结合2、3两个例子得出差异

0.
  DepartmentExample departmentExample = new DepartmentExample();
  departmentExample.createCriteria().andTenantIdEqualTo(tenantId).andBranchIdIsNull().andStatusIdIn(departmentState);
  departmentExample.or().andBranchIdEqualTo(0L);      
  departmentExample.setOrderByClause(" id ");
  departmentList = departmentMapper.selectByExample(departmentExample);
 

  sql->SELECT * FROM ba_department

WHERE ( tenant_id = ? AND branch_id IS NULL AND status_id IN ( ? , ? ) ) OR( branch_id = ? ) ORDER BY id




1.
  DepartmentExample departmentExample = new DepartmentExample();
  departmentExample.createCriteria().andTenantIdEqualTo(tenantId).andBranchIdIsNull().andStatusIdIn(departmentState);
  departmentExample.or().andTenantIdEqualTo(tenantId).andBranchIdEqualTo(0L).andStatusIdIn(departmentState);      
  departmentExample.setOrderByClause(" id ");
  departmentList = departmentMapper.selectByExample(departmentExample);
 
 sql ->SELECT * FROM ba_department
WHERE ( tenant_id = ? AND branch_id IS NULL AND status_id IN ( ? , ? ) ) OR( tenant_id = ? AND branch_id = ? AND status_id IN ( ? ,? ) ) ORDER BY id



2.
  DepartmentExample departmentExample = new DepartmentExample();
  DepartmentExample.Criteria criteria = departmentExample.createCriteria().andTenantIdEqualTo(tenantId).andBranchIdIsNull().andStatusIdIn(departmentState);
  criteria.andParentIdEqualTo(0L);
  departmentExample.or().andTenantIdEqualTo(tenantId).andBranchIdEqualTo(0L).andStatusIdIn(departmentState);      
  departmentExample.setOrderByClause(" id ");
  departmentList = departmentMapper.selectByExample(departmentExample);

 sql ->SELECT * FROM ba_department
WHERE ( tenant_id = ? AND branch_id IS NULL AND status_id IN ( ? , ? ) AND parent_id = ? ) OR( tenant_id = ? AND branch_id = ? AND status_id IN ( ? , ? ) ) ORDER BY id



3.
  DepartmentExample departmentExample = new DepartmentExample();
  DepartmentExample.Criteria criteria = departmentExample.createCriteria().andTenantIdEqualTo(tenantId).andBranchIdIsNull().andStatusIdIn(departmentState);
  criteria.andParentIdEqualTo(0L);
  DepartmentExample.Criteria criteria1 = departmentExample.or().andTenantIdEqualTo(tenantId).andBranchIdEqualTo(0L).andStatusIdIn(departmentState);      
  criteria1.andParentIdEqualTo(0L);
  departmentExample.setOrderByClause(" id ");
  departmentList = departmentMapper.selectByExample(departmentExample);

  sql->SELECT * FROM ba_department
WHERE ( tenant_id = ? AND branch_id IS NULL AND status_id IN ( ? , ? ) AND parent_id = ? ) OR( tenant_id = ? AND branch_id = ? AND status_id IN ( ? , ? ) AND parent_id = ?) ORDER BY id

你可能感兴趣的:(java,mysql,mybatis,数据库,mybatis,mysql)