Mybatis14_代码测试——单表的条件查询(Example的使用)
程序员文章站
2022-03-11 18:45:17
...
继上篇文章,根据员工的部门编号和地址(模糊查询)查询员工信息
1. 在service层声明接口方法
-
EmpService.java
添加如下代码List<Employee> getEmpsByAddAndDepid(String address, Integer depid);
2. 实现接口方法
-
EmpServiceImpl.java
添加如下代码@Override public List<Employee> getEmpsByAddAndDepid(String address, Integer depid) { Example example = new Example(Employee.class); //借助一个容器进行查询 Example.Criteria criteria = example.createCriteria(); if (StringUtils.isNotBlank(address)) { criteria.andLike("address", "%" + address.trim() + "%"); } if (depid != null && depid != -1) { criteria.andEqualTo("depid", depid); } List<Employee> employeeList = employeeMapper.selectByExample(example); return employeeList; }
3. 编写测试方法进行测试
-
测试方法
@Test public void getEmpsByAddAndDepid() { String address = "市"; Integer depid = 2; List<Employee> empList = empService.getEmpsByAddAndDepid(address, depid); if (empList != null) { for (Employee employee : empList) { System.out.println(employee.getHiredate()); } } }
-
测试结果