Mybatis中查询特定的列

Mybatis中查询特定的列

/**
     * 返回Maps  可以用于,查询特定的列
     */
    @Test
    public void selectByWrapperMaps() {
          
   
      QueryWrapper<User> queryWrapper = new QueryWrapper<>();
      queryWrapper.select("name","age");
        List<Map<String, Object>> maps = userMapper.selectMaps(queryWrapper);
        /* Sql
        Preparing: SELECT name,age FROM user
         */
        maps.forEach(System.out::println);
    }

    /**
     * 11、按照直属上级分组,查询每组的平均年龄、最大年龄、最小年龄
     * 并且只取年龄总和小于500的组
     * select avg(avg) avg_age,min(age) min_age,max(age) max_age
     * from user
     * group by manager_id
     * having sum(age) < 500
     *
     */
    @Test
    public void selectByWrapperMaps2(){
          
   
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("avg(age) avg_age","min(age) min_age","max(age) max_age")
                .groupBy("manager_id").having("sum(age)<{0}",500);
        List<Map<String, Object>> list = userMapper.selectMaps(queryWrapper);
          /*
         Preparing: SELECT avg(age) avg_age,min(age) min_age,max(age) max_age
                    FROM user
                    GROUP BY manager_id
                    HAVING sum(age)<?
         */
        list.forEach(System.out::println);
    }
经验分享 程序员 微信小程序 职场和发展