Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement Mybatis federated query

Shulou Source: shulou.com Published: 2022-06-02 03:56:04 09月30日 Update

This article mainly explains "how to implement Mybatis federated query". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to implement Mybatis federated query"!

Database table structure

Department

Employee

Demand one

Now the requirement is to enter id to query the corresponding employee data of the employee table, and to query the department information of the employee.

Public class Employee {private Integer id; private String lastName; private String email; private String gender; private Department dept; setter and getter.} public class Department {private Integer id; private String departmentName; setter and getter.} 1, cascading attributes encapsulate the result set implementation

This requirement obviously requires the use of two tables, and the resultMap property is needed to encapsulate department information into the dept field of the Employee object.

Method one

Select, d.id did, d.department_name from employee e, department d where e.d_id = d.id and e.id = # {id}

Method two

Select, d.id did, d.department_name from employee e, department d where e.d_id = d.id and e.id = # {id}

test

@ Test public void test1 () {SqlSession sqlSession = MyTest.getSqlSession (); EmployeeMapper mapper = sqlSession.getMapper (EmployeeMapper.class); System.out.println (mapper.getEmployee (1));}

Result

2. Step-by-step query method

DepartmentMapper.xml

Select * from department where id = # {id}

EmployeeMaper.xml

Select * from employee where id = # {id}

test

@ Test public void test1 () {SqlSession sqlSession = MyTest.getSqlSession (); EmployeeMapper mapper = sqlSession.getMapper (EmployeeMapper.class); System.out.println (mapper.getEmployee2 (1));}

Result

Requirement two

Now the requirement is to enter id to query the department information corresponding to the department table, and to query all the employee information under the department.

Public class Employee {private Integer id; private String lastName; private String email; private String gender; setter and getter.} public class Department {private Integer id; private String departmentName; private List employees; setter and getter.} 3, cascading attributes encapsulate the result set

Method

Select d.birthday, e.id eid, e.last_name, e.email, e.gender from department d left join employee e on d.id = e.d_id where d.id = # {id}

test

@ Test public void test2 () {SqlSession sqlSession = MyTest.getSqlSession (); DepartmentMapper mapper = sqlSession.getMapper (DepartmentMapper.class); System.out.println (mapper.getDepartment (1));}

Result

4. Query step by step

EmployeeMaper.xml

Select * from employee where d_id = # {did}

DepartmentMapper.xml

Select * from department where id = # {id}

test

@ Test public void test2 () {SqlSession sqlSession = MyTest.getSqlSession (); DepartmentMapper mapper = sqlSession.getMapper (DepartmentMapper.class); System.out.println (mapper.getDepartment3 (1));}

Result

At this point, I believe you have a deeper understanding of "how to implement Mybatis federated query". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Queries results methods information tests unions employees attributes departments encapsulation content data truth learning input utility obvious deeper two interest Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Technology vpn Shulou Information MariaDB