Mybatis-Plus 实战完整学习笔记(六)------select测试一

查询方法(3.0.3)

1.查询一个员工的数据

 1 @Test
 2     public void selectMethod() throws SQLException {
 3 
 4         // 根据ID获取一个对象的数据
 5         Employee employee = employeeMapper.selectById(1);
 6 
 7         if (employee!=null) {
 8             logger.info("++一个员工信息+++++"+gson.toJson(employee));
 9         }
10 
11     }
View Code

2.查询的字段设置

 1  @Test
 2     public void selectMethod() throws SQLException {
 3 
 4         // select 要查询的字段
 5         // SELECT email FROM tbl_employee
 6         List<Employee> employeeList = employeeMapper.selectList(new QueryWrapper<Employee>().select("email"));
 7 
 8          
 9 
10         if (!employeeList.isEmpty()) {
11             logger.info("++一个员工信息+++++"+gson.toJson(employeeList));
12         }
13 
14 
15     }
View Code

相当于执行SELECT email FROM tbl_employee  这条sql语句

3.拼接where多条件和order by方法时候,不需要的可以直接去掉

 1 @Test
 2     public void selectMethod() throws SQLException {
 3         
 4         Employee employee = new Employee();
 5 
 6                 // where 条件查询和and添加和orderby使用
 7         // SELECT age FROM tbl_employee WHERE email = ? AND age = ? ORDER BY id ASC
 8         List<Employee> employeeList = employeeMapper.selectList(new QueryWrapper<Employee>().select("age").eq("email",employee.getEmail()).eq("age",employee.getAge()).orderBy(true,true,"id"));
 9 
10 
11         if (!employeeList.isEmpty()) {
12             logger.info("++一个员工信息+++++"+gson.toJson(employeeList));
13         }
14 
15 
16     }
View Code

相当于SQL语句SELECT age FROM tbl_employee WHERE email = ? AND age = ? ORDER BY id ASC

4.批量查询

 1     @Test
 2     public void selectMethod() throws SQLException {
 3  
 4         List<Integer> idList = new  ArrayList<>();
 5         idList.add(1);
 6         idList.add(2);
 7 
 8         List<Employee> employeeList =employeeMapper.selectBatchIds(idList);
 9 
10 
11         if (!employeeList.isEmpty()) {
12             logger.info("++一个员工信息+++++"+gson.toJson(employeeList));
13         }
14 
15 
16     }
View Code

相当于SQL语句SELECT id,last_name,email,gender,age FROM tbl_employee WHERE id IN ( ? , ? )  

5.Map查询封装

 1 @Test
 2     public void selectMethod() throws SQLException {
 3 
 4         Employee employee = employeeMapper.selectById(1);
 5 
 6         Map<String,Object> map = new HashMap<>(16);
 7         map.put("email",employee.getEmail());
 8         map.put("age",employee.getAge());
 9 
10 
11         List<Employee> employeeList =employeeMapper.selectByMap(map);
12 
13         if (!employeeList.isEmpty()) {
14             logger.info("++一个员工信息+++++"+gson.toJson(employeeList));
15         }
16 
17 
18     }
View Code

相当于SQL语句SELECT id,last_name,email,gender,age FROM tbl_employee WHERE email = ? AND age = ? 

结果封装成map

1 ++一个员工信息+++++[{"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) 
View Code

 

posted @ 2018-09-30 22:27  北极的大企鹅  阅读(1520)  评论(0编辑  收藏  举报
阅读 - 79万