MyBatis分组统计查询、多条件查询

声明一下,本文章基于之前的博客https://blog.csdn.net/Xeon_CC/article/details/107219206 的环境下进行编码,但是不影响童鞋们浏览此文章,毕竟说的是一些查询方法

 

在MyBatisController类,用上@Controller,因为我们要用到Thymeleaf 和 SpringMVC,这样才能返回html页面

以下代码在联合查询的情况下,当然你也可以不联合查询,方法是相通的

包含了以下查询功能

①使用EmpExample类进行全查

②简单条件查询,查询年龄在20到40岁的员工

③或运算查询,查询年龄20到40或者姓张的员工

④与运算,查询年龄0到40而且是姓张的员工

⑤包含了分组查询各个部门有多少人,并且加上条件筛选20到40岁的员工

⑥统计各个姓分别有多少个人

 

import com.example.mapper.DeptMapper;
import com.example.mapper.EmpMapper;
import com.example.model.Dept;
import com.example.model.DeptExample;
import com.example.model.Emp;
import com.example.model.EmpExample;
import com.example.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.*;


@Controller
public class MyBatisController {
    @Autowired
    EmpService empService;

    @Autowired
    EmpMapper empMapper;

    @Autowired
    DeptMapper deptMapper;

    @GetMapping("/findAllEmp")
    public String findAllEmp(Model model){
        EmpExample empExample = new EmpExample();
        empExample.setOrderByClause("emp_id ASC");
        model.addAttribute("tip_allEmp","查询所有员工");
        model.addAttribute("allEmp",empMapper.selectByExampleWithDept(empExample));
        return "index";
    }

    //查询年龄在20到 40的员工
    @GetMapping("/findAgeGt20Lt40")
    public String findAgeGt20Lt40(Model model){
        EmpExample empExample = new EmpExample();
        EmpExample.Criteria empCriteria = empExample.createCriteria();
        empCriteria.andEmpAgeBetween(20,40);
        model.addAttribute("tip_selectEmpFromeAge","查询年龄在20到 40的员工");
        model.addAttribute("selectEmpFromeAge",empMapper.selectByExampleWithDept(empExample));
        return "index";
    }

    //查询年龄20到40或者姓名是姓张的
    @GetMapping("/findAgeGt20Lt40AndNameLike")
    public String findAgeGt20Lt40AndNameLike(Model model){
        EmpExample empExample = new EmpExample();
        //执行或运算查询
        EmpExample.Criteria empCriteria1 = empExample.createCriteria();
        EmpExample.Criteria empCriteria2 = empExample.createCriteria();
        empCriteria1.andEmpAgeBetween(20,40);
        empCriteria2.andEmpNameLike("张%");
        model.addAttribute("tip_selectEmpFromAgeAndName","查询年龄20到40或者姓名是姓张的");
        model.addAttribute("selectEmpFromAgeAndName",empMapper.selectByExampleWithDept(empExample));
        return "index";
    }

    //查询年龄0到40而且姓名是姓张的
    @GetMapping("/findAgeGt0Lt40AndNameLike")
    public String findAgeGt0Lt40AndNameLike(Model model){
        EmpExample empExample = new EmpExample();
        //执行与运算
        EmpExample.Criteria empCriteria = empExample.createCriteria();
        empCriteria.andEmpAgeBetween(0,40);
        empCriteria.andEmpNameLike("张%");
        model.addAttribute("tip_selectEmpFromAgeAndName","查询年龄0到40而且姓名是姓张的");
        model.addAttribute("selectEmpFromAgeAndName",empMapper.selectByExampleWithDept(empExample));
        return "index";
    }

    //分组查询各个部门有多少人,并且加上条件筛选20到40岁的员工
    @GetMapping("/findEmpGroupByDept")
    public String findEmpGroupByDept(Model model){
        DeptExample deptExample = new DeptExample();
        List<Dept> deptList = deptMapper.selectByExample(deptExample);
        Map<String, Long> deptCountMap = new HashMap<>();
        for(int i=0;i<deptList.size();i++){
            EmpExample empExample = new EmpExample();
            EmpExample.Criteria empCriteria =  empExample.createCriteria();
            empCriteria.andEmpAgeBetween(20,40);
            empCriteria.andDeptIdEqualTo(deptList.get(i).getDeptId());
            Long dept_emp_count = empMapper.countByExample(empExample);
            deptCountMap.put(deptList.get(i).getDeptName(),dept_emp_count);
        }

        model.addAttribute("tip_deptCountMap","分组查询各个部门有多少人,并且加上条件筛选20到30岁的员工");
        model.addAttribute("deptCountMap",deptCountMap);

        return "index";
    }

    //统计各个姓有多少个人
    @GetMapping("/findFirstNameCount")
    public String findFirstNameCount(Model model){
        EmpExample empExample = new EmpExample();
        Map<String,Long> firstNameMap = new HashMap<>();

        List<Emp> empList = empMapper.selectByExampleWithDept(empExample);
        //不重复集合
        Set<String> set = new HashSet();
        for(int i=0;i<empList.size();i++){
            set.add(empList.get(i).getEmpName().substring(0,1));
        }

        model.addAttribute("set",set);

        for(String firstName : set){
            EmpExample empExample2 = new EmpExample();
            EmpExample.Criteria empCriteria = empExample2.createCriteria();
            empCriteria.andEmpNameLike(firstName+"%");
            Long firstNameCount = empMapper.countByExample(empExample2);
            firstNameMap.put(firstName,firstNameCount);
        }

        model.addAttribute("firstNameMap",firstNameMap);

        return "index";
    }


}

 

index.html页面,使用springboot工程内置的的thymeleaf会更加灵活好用

编辑index.html文件

 

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!-- ${xxxkey} 意思是在Controller类中取出model的属性值,取值以后可以直接使用它 -->
<span th:text="${tip_allEmp}"></span>
<p th:each="Emp : ${allEmp}">

    <span th:text="${Emp.getEmpId()}"></span>
    <span th:text="${Emp.getEmpName()}"></span>
    <span th:text="${Emp.getEmpAge()}"></span>
    <span th:text="${Emp.getEmpGender() eq 'm'?'男':'女'}"></span>
    <span th:text="${Emp.getDeptId()}"></span>
    <span th:text="${Emp.getDept().getDeptName()}"></span>
</p>

<span th:text="${tip_selectEmpFromeAge}"></span>
<p th:each="Emp : ${selectEmpFromeAge}">
    <span th:text="${Emp.getEmpId()}"></span>
    <span th:text="${Emp.getEmpName()}"></span>
    <span th:text="${Emp.getEmpAge()}"></span>
    <span th:text="${Emp.getEmpGender() eq 'm'?'男':'女'}"></span>
    <span th:text="${Emp.getDeptId()}"></span>
    <span th:text="${Emp.getDept().getDeptName()}"></span>
</p>

<span th:text="${tip_selectEmpFromAgeAndName}"></span>
<p th:each="Emp : ${selectEmpFromAgeAndName}">
    <span th:text="${Emp.getEmpId()}"></span>
    <span th:text="${Emp.getEmpName()}"></span>
    <span th:text="${Emp.getEmpAge()}"></span>
    <span th:text="${Emp.getEmpGender() eq 'm'?'男':'女'}"></span>
    <span th:text="${Emp.getDeptId()}"></span>
    <span th:text="${Emp.getDept().getDeptName()}"></span>
</p>

<span th:text="${tip_deptCountMap}"></span>
<p th:each="dcMapVal : ${deptCountMap}">
    <span th:text="${dcMapVal.key }"></span>
    <span th:text="${dcMapVal.value +'人'}"></span>
</p>



<p th:each="fnMap : ${firstNameMap}">
    <span th:text="${'姓'+ fnMap.key }"></span>
    <span th:text="${fnMap.value +'人'}"></span>
</p>

</body>
</html>

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值