Mybatis-Plus 实战完整学习笔记(七)------select测试二
1.查询selectOne (3.0.3版)
1 @Test 2 public void selectMethod() throws SQLException { 3 4 // 根据ID获取一个对象的数据 5 6 Employee employee =employeeMapper.selectOne(new QueryWrapper<Employee>().eq("id","1")); 7 8 if (employee!=null) { 9 logger.info("++一个员工信息+++++"+gson.toJson(employee)); 10 } 11 12 13 14 15 16 }
其相当于SQL语句:SELECT id,last_name,email,gender,age FROM tbl_employee WHERE id = ?
2.3版本的写法是直接注入一个对象,对象的set值就是查询的条件,同时,ID要是Long或Integer类型,条件一定要唯一,否则,会报数据重复的异常。
1 Employee employee = new Employee(); 2 employee.setId(1); 3 4 Employee result =employeeMapper.selectOne(employee); 5 6 if (result!=null) { 7 logger.info("++一个员工信息+++++"+gson.toJson(result)); 8 }
2、查询selectMaps(3.0.3新出)
1 @Test 2 public void selectMethod() throws SQLException { 3 4 // 6、多个Map查询 5 Employee employee = employeeMapper.selectById(1); 6 7 QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>(); 8 9 List<Map<String, Object>> employeeList =employeeMapper.selectMaps(employeeQueryWrapper); 10 11 12 if (!employeeList.isEmpty()) { 13 logger.info("++一个员工信息+++++"+gson.toJson(employeeList)); 14 } 15 16 17 18 }
写法跟其他的查询没有区别,只是返回的结果,变成List<Map<String,Object>>,条件查询跟上边的一样。
返回样式
1 ++一个员工信息+++++[{"gender":"0","last_name":"Marry","id":1,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":2,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":3,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":4,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":12,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":13,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":14,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":15,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":16,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":17,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":18,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":19,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":20,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":21,"email":"1","age":55}] (TestCRUD.java:159)
本来以为是要装多个map进去查询就写成下边的样子,结果不是那个意思
1 // 6、多个Map查询 2 Employee employee = employeeMapper.selectById(1); 3 4 Map<String,Object> map1 = new HashMap<>(16); 5 map1.put("email",employee.getEmail()); 6 map1.put("age",employee.getAge()); 7 8 Employee employee2 = employeeMapper.selectById(2); 9 Map<String,Object> map2 = new HashMap<>(16); 10 map2.put("email",employee2.getEmail()); 11 map2.put("age",employee2.getAge()); 12 13 List<Map<String,Object>> mapList = new ArrayList<>(); 14 mapList.add(map1); 15 mapList.add(map2); 16 17 18 QueryWrapper<List<Map<String, Object>>> maps = new QueryWrapper<List<Map<String, Object>>>(); 19 maps.setEntity((mapList)); 20 21 22 List<Map<String, Object>> employeeList =employeeMapper.selectMaps(maps);
3、查询selectObjs
1 @Test 2 public void selectMethod() throws SQLException { 3 4 // 7、selectObjs 5 Employee employee = employeeMapper.selectById(1); 6 7 List<Object> employeeList =employeeMapper.selectObjs(new QueryWrapper<Employee>().eq("email",employee.getEmail())); 8 9 10 if (!employeeList.isEmpty()) { 11 logger.info("++一个员工信息+++++"+gson.toJson(employeeList)); 12 } 13 14 15 }
用法一样,就是返回的结果是个Object类型。
4、统计结果查询selectCount
1 @Test 2 public void selectMethod() throws SQLException { 3 4 // 8、selectCount 5 Employee employee = employeeMapper.selectById(1); 6 7 Integer count = employeeMapper.selectCount(new QueryWrapper<Employee>().eq("email", employee.getEmail())); 8 9 if (count > 0) { 10 logger.info("++统计结果:+++++" + count); 11 } 12 13 }
5、分页查询selectPage
1 @Test 2 public void selectMethod() throws SQLException { 3 4 // 9、分页查询 5 6 Integer count = employeeMapper.selectCount(new QueryWrapper<Employee>().between("age", 0, 100) 7 .eq("gender", 0)); 8 9 IPage<Employee> employeeIPage = new Page<Employee>(1, 3,count); 10 11 IPage<Employee> employeeList = employeeMapper.selectPage(employeeIPage,new QueryWrapper<Employee>() 12 .between("age", 0, 100) 13 .eq("gender", 0)); 14 15 if (!employeeList.getRecords().isEmpty()) { 16 logger.info("++一个员工信息+++++" + gson.toJson(employeeList)); 17 } 18 19 }
6、分页查询返回map类型的selectMapsPage
1 @Test 2 public void selectMethod() throws SQLException { 3 4 // 10、分页查询Map结果返回 5 Integer count = employeeMapper.selectCount(new QueryWrapper<Employee>().between("age", 0, 100) 6 .eq("gender", 0)); 7 8 IPage<Employee> employeeIPage = new Page<Employee>(1, 3, count); 9 10 IPage<Map<String, Object>> employeeList = employeeMapper.selectMapsPage(employeeIPage, new QueryWrapper<Employee>() 11 .between("age", 0, 100) 12 .eq("gender", 0)); 13 14 if (!employeeList.getRecords().isEmpty()) { 15 logger.info("++一个员工信息+++++" + gson.toJson(employeeList)); 16 } 17 18 }
System.out.println("返回数据:"+employeeIPage.getRecords());
System.out.println("总条数:"+page.getTotal());
System.out.println("当前页码:"+page.getCurrent());
System.out.println("总页码:"+page.getPages());
System.out.println("每页显示条数:"+page.getSize());
System.out.println("是否有上一页:"+page.hasPrevious());
System.out.println("是否有下一页:"+page.hasNext());
System.out.println("返回的数据:"+page.getRecords());