Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Mybatis federated query

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report