欢迎访问宙启技术站
智能推送

利用mybatis plus怎么实现一个一对多分页查询功能

发布时间:2023-05-14 16:30:22

MyBatis Plus是一款优秀的Java持久层框架,它在MyBatis基础上进行了功能补充和扩展,为开发者提供了更加易于使用的API,从而让开发者可以更加轻松地进行数据持久化操作。在实际开发中,我们经常需要使用到一对多的关系,例如查询某个部门下面所有的员工信息等等。本篇文章主要介绍如何使用MyBatis Plus实现一个一对多分页查询功能。

一、创建数据库及数据表

这里我们以部门-员工的关系为例,假设一个部门可以有多个员工,一个员工只能属于一个部门。那么我们需要在数据库中创建两张表,一张是部门表,一张是员工表,两张表之间通过部门ID进行关联。

部门表:

CREATE TABLE department (

  id bigint(20) NOT NULL COMMENT '主键ID',

  name varchar(30) NOT NULL COMMENT '部门名称',

  PRIMARY KEY (id)

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='部门表';

员工表:

CREATE TABLE employee (

  id bigint(20) NOT NULL COMMENT '主键ID',

  name varchar(30) NOT NULL COMMENT '员工姓名',

  gender tinyint(1) NOT NULL COMMENT '员工性别:0女,1男',

  age tinyint(3) NOT NULL COMMENT '员工年龄',

  department_id bigint(20) NOT NULL COMMENT '部门ID',

  PRIMARY KEY (id)

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='员工表';

二、创建实体类及Mapper

我们需要创建两个实体类,一个是Department,一个是Employee。同时我们还需要创建两个Mapper接口,一个是DepartmentMapper,一个是EmployeeMapper。

Department实体类:

public class Department {

    private Long id;

    private String name;

    private List<Employee> employees;

    // getter和setter方法省略

}

Employee实体类:

public class Employee {

    private Long id;

    private String name;

    private Integer gender;

    private Integer age;

    private Long departmentId;

    private Department department;

    // getter和setter方法省略

}

DepartmentMapper接口:

public interface DepartmentMapper extends BaseMapper<Department> {}

EmployeeMapper接口:

public interface EmployeeMapper extends BaseMapper<Employee> {}

三、创建XML文件

我们需要创建两个XML文件,一个是DepartmentMapper.xml,一个是EmployeeMapper.xml。在DepartmentMapper.xml中,我们需要定义一个select方法,用于查询所有的部门信息及其下属的员工信息。在EmployeeMapper.xml中,我们需要定义一个select方法,用于查询某个部门下的所有员工信息。

DepartmentMapper.xml:

<?xml version="1.0" encoding="UTF-8" ?>

<mapper namespace="com.example.mybatisplus.mapper.DepartmentMapper">

    <select id="selectAllWithEmployees" resultMap="BaseResultMap">

        SELECT

            d.id,

            d.name,

            e.id as employees_id,

            e.name as employees_name,

            e.gender as employees_gender,

            e.age as employees_age,

            e.department_id as employees_department_id

        FROM

            department d

            LEFT JOIN employee e ON d.id = e.department_id

    </select>

</mapper>

EmployeeMapper.xml:

<?xml version="1.0" encoding="UTF-8" ?>

<mapper namespace="com.example.mybatisplus.mapper.EmployeeMapper">

    <select id="selectByDepartmentId" resultMap="BaseResultMap">

        SELECT

            id,

            name,

            gender,

            age,

            department_id

        FROM

            employee

        WHERE

            department_id = #{departmentId}

    </select>

</mapper>

四、查询方法实现

我们将查询方法放在一个Service中,通过调用Mapper中的方法实现数据查询。在查询方法中,我们需要使用MyBatis Plus提供的Page对象进行分页查询。

DepartmentService:

@Service

public class DepartmentService {

    @Autowired

    private DepartmentMapper departmentMapper;

    @Autowired

    private EmployeeMapper employeeMapper;

    public Page<Department> selectAllWithEmployees(Page<Department> page) {

        // 分页查询部门信息及其下属的员工信息

        List<Department> departmentList = departmentMapper.selectAllWithEmployees();

        // 遍历departmentList,查询每个部门的员工信息

        for(Department department : departmentList) {

            List<Employee> employeeList = employeeMapper.selectByDepartmentId(department.getId());

            department.setEmployees(employeeList);

        }

        // 设置查询结果到Page对象中

        page.setRecords(departmentList);

        page.setTotal(departmentList.size());

        return page;

    }

}

以上就是使用MyBatis Plus实现一对多分页查询功能的方法。通过使用BaseMapper提供的CRUD方法以及自定义的SQL语句,我们可以轻松地完成一对多关系的数据查询操作。