jdbc结果集到带有列表成员的javapojo中

tuwxkamq  于 2021-08-25  发布在  Java
关注(0)|答案(2)|浏览(403)

我有两个查询和结果集,在下面的代码中,我想展示一个特定的查询和结果集 userGroupCode 我有确定的理由 userPreferenceemployee 与之相关的。我已经写了下面的代码来显示 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合并表。
我觉得代码的性能没有得到优化,我如何优化代码呢。先谢谢你。

ee7vknir

ee7vknir1#

欧拉,
您可以使用以id为键、值为对象(要聚合到)的Map进行分组。例如:

if(map.containes(key))
{
    get object from map and do Ops.
}
else
{
    1. Create new object
    2. Do set Ops on Object
    3. Add to map.
}
yuvru6vn

yuvru6vn2#

你基本上有两种解决方案,
编写一个查询,返回带有连接的所有结果,并用java进行过滤,使用两个Map(一个用于 UserGroup 另一个是 Employee .
编写查询并使用 list 在查询本身中。

SELECT ug.userGroupCode, ug.userGroupDesc, up.userPreference, emp.employee_id,emp.name,emp.role
FROM dbo.UserGroup_link ug
INNER JOIN dbo.UserPreference up ON  ug.userGroupCode = up.userGroupCode
INNER JOIN dbo.employee emp ON  ug.userGroupCode = emp.userGroupCode

然后使用 RowCallbackHandler 实现你想要的(而不是 ResultSetExtractor .

Map<String, UserGroup> userGroups = new HashMap<>;
Map<Integer, Employee> employees = new HashMap<>;

jdbc.query(query, (rs) -> {

 String userGroupCode = rs.getString("userGroupCode"); 
 String emp_id = rs.getString("employee_id");

 UserGroupMapping ugm userGroups.computeIfAbsent(userGroupCode,  {
    UserGroupMapping ugm1 = new UserGroupMapping();
    ugm1.setUserGroupCode(userGroupCode);
    ugm1.setUserGroupDesc(rs.getString("userGroupDesc");
    ugm1.setUserPreference(new ArrayList<>());
    ugm1.getEmployee(new ArrayList<>());
    return ugm1;
 });
 ugm.getUserPreference().add(rs.getString("userPreference"));

 Employee emp = employees.computeIfAbsent(emp_id, {
   Employee emp1 = new Employee();
   emp1.setName(rs.getString("name"));
   emp1.setRole(new ArrayList<>());
   ugm.getEmployee().add(emp);
   return emp1;
 });

 emp.getRole().add(rs.getString("role"));

});

return userGroups.values();

以上代码将获得所有 UserGroupMapping 来自结果的对象,包括所有 Employee 示例。需要临时Map来确定记录是否已经显示。
另一个解决办法是使用 list 在您的查询和一些 GROUP BY 语句让查询执行聚合的一部分。这样,您就可以更轻松地创建 Employee .

相关问题