查询方法(3.0.3)
1.查询一个员工的数据
@Test
public void selectMethod() throws SQLException { // 根据ID获取一个对象的数据
Employee employee = employeeMapper.selectById(1); if (employee!=null) {
logger.info("++一个员工信息+++++"+gson.toJson(employee));
} }
2.查询的字段设置
@Test
public void selectMethod() throws SQLException { // select 要查询的字段
// SELECT email FROM tbl_employee
List<Employee> employeeList = employeeMapper.selectList(new QueryWrapper<Employee>().select("email")); if (!employeeList.isEmpty()) {
logger.info("++一个员工信息+++++"+gson.toJson(employeeList));
} }
相当于执行SELECT email FROM tbl_employee 这条sql语句
3.拼接where多条件和order by方法时候,不需要的可以直接去掉
@Test
public void selectMethod() throws SQLException { Employee employee = new Employee(); // where 条件查询和and添加和orderby使用
// SELECT age FROM tbl_employee WHERE email = ? AND age = ? ORDER BY id ASC
List<Employee> employeeList = employeeMapper.selectList(new QueryWrapper<Employee>().select("age").eq("email",employee.getEmail()).eq("age",employee.getAge()).orderBy(true,true,"id")); if (!employeeList.isEmpty()) {
logger.info("++一个员工信息+++++"+gson.toJson(employeeList));
} }
相当于SQL语句SELECT age FROM tbl_employee WHERE email = ? AND age = ? ORDER BY id ASC
4.批量查询
@Test
public void selectMethod() throws SQLException { List<Integer> idList = new ArrayList<>();
idList.add(1);
idList.add(2); List<Employee> employeeList =employeeMapper.selectBatchIds(idList); if (!employeeList.isEmpty()) {
logger.info("++一个员工信息+++++"+gson.toJson(employeeList));
} }
相当于SQL语句SELECT id,last_name,email,gender,age FROM tbl_employee WHERE id IN ( ? , ? )
5.Map查询封装
@Test
public void selectMethod() throws SQLException { Employee employee = employeeMapper.selectById(1); Map<String,Object> map = new HashMap<>(16);
map.put("email",employee.getEmail());
map.put("age",employee.getAge()); List<Employee> employeeList =employeeMapper.selectByMap(map); if (!employeeList.isEmpty()) {
logger.info("++一个员工信息+++++"+gson.toJson(employeeList));
} }
相当于SQL语句SELECT id,last_name,email,gender,age FROM tbl_employee WHERE email = ? AND age = ?
结果封装成map
++一个员工信息+++++[{"id":1,"lastName":"Marry","email":"1","gender":0,"age":55},{"id":2,"lastName":"Marry","email":"1","gender":0,"age":55},{"id":3,"lastName":"Marry","email":"1","gender":0,"age":55},{"id":4,"lastName":"Marry","email":"1","gender":0,"age":55},{"id":12,"lastName":"Marry","email":"1","gender":0,"age":55},{"id":13,"lastName":"Marry","email":"1","gender":0,"age":55},{"id":14,"lastName":"Marry","email":"1","gender":0,"age":55},{"id":15,"lastName":"Marry","email":"1","gender":0,"age":55},{"id":16,"lastName":"Marry","email":"1","gender":0,"age":55},{"id":17,"lastName":"Marry","email":"1","gender":0,"age":55},{"id":18,"lastName":"Marry","email":"1","gender":0,"age":55},{"id":19,"lastName":"Marry","email":"1","gender":0,"age":55},{"id":20,"lastName":"Marry","email":"1","gender":0,"age":55},{"id":21,"lastName":"Marry","email":"1","gender":0,"age":55}] (TestCRUD.java:146)