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 use the ibatis resultMap groupBy property

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

Share

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

This article will explain in detail how to use the ibatis resultMap groupBy attribute for you. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Recently began to switch to j2ee for development, using the data persistence layer is iBatis.

Ibatis resultMap must be used to solve the problem of 1groupBy N and MVR N in iBatis, but a problem will be encountered in the process of using it, that is, the problem of groupBy can not be inherited. Now let's give you an example to introduce this problem and its solution.

Ibatis resultMap groupBy problem description:

Consider this situation. If a company decides to set up an amateur sports club for employee welfare, employees are free to sign up. The type of sports is determined by the department, so there are four entities, companies, departments, employees and sports. And their relationship is that the company: the department, the department: the employees all have a 1-V-N relationship, while the department-to-sports relationship is a NRV-M relationship.

If we want to use a sql statement to make a registration page in the following figure, how should we implement it?

First, we must define resultMap, with the following code:

Select

*

FROM

TbDepartment, tbStaff, tbResult, tbDepSport

Where

TbStaff.deptid = tbDepartment.deptID

AND

TbSport.sportid = tbDepSport.sportid

AND

TbDepartment.deptID = tbDepSport.deptID

Problem analysis:

Why there is this problem, the crux of the problem is the attribute groupBy.

First, let's analyze the result set from the execution of the above sql statement:

Development and support Department staff A Football

Development and support Department staff A basketball

Badminton of staff An of the Development support Department

Employee B Football of Development support Department

Employee B Basketball of Development support Department

Badminton employee B of the Development support Department

C Football for staff of Development support Department

C basketball of the staff of the development support department

C badminton of the staff of the development support department

Ding Football, an employee of the Development support Department

Ding Basketball, an employee of the Development support Department

Ding badminton of the staff of the development support department

The staff of the development support department play football.

The staff of the development support department play basketball.

Badminton staff of the development support department

The employees of the infrastructure department have played football.

The staff of the infrastructure department has played basketball.

The staff of the infrastructure department have played badminton.

Infrastructure staff Geng Football

Geng basketball staff of the infrastructure department

Geng badminton, an employee in the infrastructure department.

Employees of Infrastructure Department apply for football

The employees of Infrastructure Department apply for basketball.

Infrastructure staff apply for badminton

The staff of Infrastructure Department is not football.

The staff of the infrastructure department is not basketball.

Non badminton, an employee of infrastructure department

Infrastructure staff Kui Football

Sunflower basketball for the staff of infrastructure department

Infrastructure staff Kui Badminton

This is the result of a Cartesian product, there are a lot of redundant results, how does ibatis resultMap deal with this result set?

First of all, when the program executes the statement whose id is getEverything, find the resultMap:DepartmentResult first.

Because DepartmentResult has the groupBy keyword, DepartmentResult will filter out the duplicate results according to the value of groupBy, map them to the relevant attributes in departVo, and put them in a List object. * successfully filter out the two departments.

When we find resultMap:StaffResult while processing the fields in DepartmentResult, we find that StaffResult has the keyword groupBy, and similarly, we filter out the duplicate fields according to the value of groupBy. Here, because the employees of the two departments are different, we can get the employee list of each department after filtering the duplicate results.

* * when dealing with resultMap:StaffResult, the same reason will filter out the same result. If the movements of the two departments are inconsistent, then we can easily get the result sets of the two movements. But if the movement of the two departments is the same, because of the groupBy, we can only get one motion result set, which is the result of the above picture.

Solution:

In fact, I think this is a bug of ibatis. If the groupBy of resultMap can inherit upward, then the groupBy of StaffResult is actually deptID and sportID, then this will not happen. So to solve this problem, we must introduce deptID into StaffResult and modify the groupBy property.

At the same time, in order for the ibatis to map properly, we need to add the deptID attribute to the SportVo.

This is the end of this article on "how to use ibatis resultMap groupBy attributes". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

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