注意:本教程使用的数据库脚本、数据模型和环境信息请参考 “MyBatis Plus环境准备” 章节,点击下载示例源码。
本章节将介绍使用 groupBy 实现分组,groupBy 函数定义如下:
groupBy(R... columns) groupBy(boolean condition, R... columns)
参数说明:
columns:要分组的数据表列名称列表
condition:用于指定当前这个条件是否有效;如果为 true,则应用当前条件;如果为 false,则忽略当前条件。
实例:用户信息表根据 sex 和 age 列进行分组,如下:
QueryWrapper<UserBean> wrapper = new QueryWrapper<>(); wrapper.groupBy("sex", "age");
运行上面代码,你实际上将执行如下 SQL 语句:
SELECT user_id,name,sex,age,face,salary,borthday FROM user GROUP BY sex,age
仔细观察上面的 SQL 语句,这并不是我们想要的 SQL 语句,只有后面的“GROUP BY sex,age”是我们想要的。而我们实际上需要的 SQL 可能如下:
-- 统计按 sex 和 age 分组后,每一个 sex 和 age 组合的总薪水 SELECT sex, age, sum(salary) as total_salary FROM user GROUP BY sex,age; -- 统计同一个 sex 和 age 的用户数 SELECT sex, age, count(*) as user_count FROM user GROUP BY sex,age; -- 统计同一个 sex 和 age 的用户平均薪水 SELECT sex, age, avg(salary) as avg_salary FROM user GROUP BY sex,age;
下面通过实例演示根据 sex 和 age 分组,然后统计他们的总薪水、用户数和平均薪水。代码如下:
package com.hxstrive.mybatis_plus.simple_mapper.condition; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.hxstrive.mybatis_plus.mapper.SimpleMapper; import com.hxstrive.mybatis_plus.model.UserBean; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest class Condition22Test { @Autowired private SimpleMapper simpleMapper; private void totalSalary() { QueryWrapper<UserBean> wrapper = new QueryWrapper<>(); wrapper.groupBy("sex", "age"); wrapper.select("sex, age, sum(salary) as total_salary"); List<UserBean> userBeanList = simpleMapper.selectList(wrapper); for(UserBean userBean : userBeanList) { System.out.println("sex=" + userBean.getSex() + ", age=" + userBean.getAge() + ", totalSalary=" + userBean.getTotalSalary()); } } private void totalCount() { QueryWrapper<UserBean> wrapper = new QueryWrapper<>(); wrapper.groupBy("sex", "age"); wrapper.select("sex, age, count(*) as total_count"); List<UserBean> userBeanList = simpleMapper.selectList(wrapper); for(UserBean userBean : userBeanList) { System.out.println("sex=" + userBean.getSex() + ", age=" + userBean.getAge() + ", totalCount=" + userBean.getTotalCount()); } } private void avgSalary() { QueryWrapper<UserBean> wrapper = new QueryWrapper<>(); wrapper.groupBy("sex", "age"); wrapper.select("sex, age, avg(salary) as avg_salary"); List<UserBean> userBeanList = simpleMapper.selectList(wrapper); for(UserBean userBean : userBeanList) { System.out.println("sex=" + userBean.getSex() + ", age=" + userBean.getAge() + ", avgSalary=" + userBean.getAvgSalary()); } } @Test void contextLoads() { totalCount(); totalSalary(); avgSalary(); } }
运行上面代码,输出结果如下:
==> Preparing: SELECT sex, age, count(*) as total_count FROM user GROUP BY sex,age ==> Parameters: sex=女, age=20, totalCount=16 sex=女, age=21, totalCount=11 ... sex=男, age=68, totalCount=7 sex=男, age=69, totalCount=13 ==> Preparing: SELECT sex, age, sum(salary) as total_salary FROM user GROUP BY sex,age ==> Parameters: sex=女, age=20, totalSalary=109333.5 sex=女, age=21, totalSalary=60554.25 ... sex=男, age=68, totalSalary=48114.0 sex=男, age=69, totalSalary=68889.0 ==> Preparing: SELECT sex, age, avg(salary) as avg_salary FROM user GROUP BY sex,age ==> Parameters: sex=女, age=20, avgSalary=6833.3438 sex=女, age=21, avgSalary=5504.9316 ... sex=男, age=68, avgSalary=6873.4287 sex=男, age=69, avgSalary=5299.154
注意:上面示例中的“wrapper.select();”select 方法用来设置 SELECT 语句的列字符串,例如:SELECT wrapper.select() FROM user
UserBean的定义没找到啊,能提供下吗