我有两个查询和结果集,在下面的代码中,我想展示一个特定的查询和结果集 userGroupCode
我有确定的理由 userPreference
及 employee
与之相关的。我已经写了下面的代码来显示 userGroupCode
对象:
String query1= "SELECT ug.userGroupCode, ug.userGroupDesc, up.userPreference"
+ "FROM dbo.UserGroup_link ug INNER JOIN dbo.UserPreference up ON ug.userGroupCode = up.userGroupCode";
usergroupcodeusergroupdescuserpreferencea100financemumbaia100financebangalorea200supply Chain Chennai 201MarketingDelhia2201MarketingKolkataa300HealthIndore
String query2= "SELECT ug.userGroupCode, ug.userGroupDesc, emp.employee_id,emp.name,emp.role"
+ "FROM dbo.UserGroup ug INNER JOIN dbo.employee emp ON ug.userGroupCode = emp.userGroupCode";
用户组代码用户组描述员工\u ID姓名角色EA100Finance101Foo1开发人员100Finance101Foo1团队领导200Supply Chain 091Test1经理201Marketing 591Ser1分析201Marketing 1001Book1Crum Mastera300Health1001Book1开发人员
我有课 UserGroupMapping
比如:
public class UserGroupMapping {
private String userGroupCode;
private String userGroupCode;
private List<String> userPreference;
private List<Employee> emp;
//getter and setter
}
另一节课 Employee
是:
public class Employee {
private String employee_id;
private String name;
private List<String> role;
//getter and setter
}
在我的存储过程类中,我在 jdbcTemplate.query()
;
String userCode = null;
List<String> userPreferenceList = new ArrayList<>();
List<UserGroupMapping> userGroupMappingList = new ArrayList<>();
List<UserGroupMapping> userGroupMappingList1 = new ArrayList<>();
UserGroupMapping userGroupMapping = new UserGroupMapping();
List<Employee> employeeList = new ArrayList<>();
Employee emp = new Employee();
UserGroupMapping userGroupMapping1 = new UserGroupMapping();
jdbcTemplate.query(query1, (rs)->{
String user_group_code = rs.getString("userGroupCode");
String user_group_desc = rs.getString("userGroupDesc");
String user_preference = rs.getString("userPreference");
if(userCode == null){
userGroupMapping.setUserGroupCode(user_group_code);
userGroupMapping.setUserGroupDesc(user_group_desc);
userPreferenceList.add(userPreference);
userCode = user_group_code;
} else if (userCode.equals(user_group_code)) {
userPreferenceList.add(userPreference);
} else {
userGroupMapping.setUserPreference(userPreferenceList);
userGroupMappingList.add(userGroupMapping);
userPreferenceList = new ArrayList<>();
userGroupMapping = new userGroupMapping();
userGroupMapping.setUserGroupCode(user_group_code);
userGroupMapping.setUserGroupDesc(user_group_desc);
userPreferenceList.add(userPreference);
userCode = user_group_code;
}});
userCode = null;
userGroupMapping.setUserPreference(userPreferenceList);
userGroupMappingList.add(userGroupMapping);
jdbcTemplate.query(query2, (rs)->{
String user_group_code = rs.getString("userGroupCode");
String user_group_desc = rs.getString("userGroupDesc");
String emp_id = rs.getString("employee_id");
String name = rs.getString("name");
if(userCode == null){
userGroupMapping1.setUserGroupCode(user_group_code);
userGroupMapping1.setUserGroupDesc(user_group_desc);
emp.setId(employeeId);
emp.setName(name);
employeeList.add(emp);
userCode = user_group_code;
} else if (userCode.equals(user_group_code)) {
Employee emp = new Employee();
emp.setId(employeeId);
emp.setName(name);
employeeList.add(emp);
} else {
userGroupMapping1.setEmployee(employeeList);
userGroupMappingList1.add(userGroupMapping1);
employeeList = new ArrayList<>();
userGroupMapping1 = new userGroupMapping();
Employee emp = new Employee();
userGroupMapping1.setUserGroupCode(user_group_code);
userGroupMapping1.setUserGroupDesc(user_group_desc);
emp.setId(employeeId);
emp.setName(name);
employeeList.add(emp);
userCode = user_group_code;
}});
userGroupMapping1.setEmployee(employeeList);
userGroupMappingList1.add(userGroupMapping1);
List<UserGroupMapping> ugList = Stream.concat(userGroupMappingList.stream, userGroupMappingList1.stream).distinct().collect(Collectors.toList())
return ugList;
问题是我希望我的输出如下:
[
{
"userGroupCode" : "A100",
"userGroupDesc" : "Finance",
"userPreference": ["Mumbai","Bangalore"],
"Employee" : [
"employee_id" : "101",
"name" : "Foo1",
"role" : ["Developer","Team Lead"]
]
}
]
合并两个列表后,我得到以下输出:
[
{
"userGroupCode" : "A100",
"userGroupDesc" : "Finance",
"userPreference": ["Mumbai","Bangalore"],
"Employee" : []
},
{
"userGroupCode" : "A100",
"userGroupDesc" : "Finance",
"userPreference": [],
"Employee" : [
"employee_id" : "101",
"name" : "Foo1",
"role" : []
]
}
]
有人能帮我做几件事吗
如何将角色嵌入到employee对象中。
如何基于usergroupcode和usergroupdesc合并表。
我觉得代码的性能没有得到优化,我如何优化代码呢。先谢谢你。
2条答案
按热度按时间ee7vknir1#
欧拉,
您可以使用以id为键、值为对象(要聚合到)的Map进行分组。例如:
yuvru6vn2#
你基本上有两种解决方案,
编写一个查询,返回带有连接的所有结果,并用java进行过滤,使用两个Map(一个用于
UserGroup
另一个是Employee
.编写查询并使用
list
在查询本身中。然后使用
RowCallbackHandler
实现你想要的(而不是ResultSetExtractor
.以上代码将获得所有
UserGroupMapping
来自结果的对象,包括所有Employee
示例。需要临时Map来确定记录是否已经显示。另一个解决办法是使用
list
在您的查询和一些GROUP BY
语句让查询执行聚合的一部分。这样,您就可以更轻松地创建Employee
.