简体   繁体   English

使用mybatis的resultmap将对象收集到一个linkhashmap中

[英]Using resultmap of mybatis to collect objects into a linkedhashmap

I have two tables: employee_details(department_id, employee_id) & skill_details(employee_id, skill_id).我有两个表:employee_details(department_id,employee_id) & Skill_details(employee_id,skill_id)。 I want to construct LinkedHashMap of Employee_Details objects for each department_id, such that我想为每个部门 ID 构造 Employee_Details 对象的 LinkedHashMap,这样

class Employee_Details {
    long employee_id;
    ArrayList<long> skill_ids;
    // other data & code may be
    ----;
    ----; 
}

How this can be achieved using resultmaps in mybatis如何在 mybatis 中使用 resultmaps 来实现这一点

You can use <collection> and <resultMap id="" type="java.util.LinkedHashMap"> like this :您可以像这样使用<collection><resultMap id="" type="java.util.LinkedHashMap">

EmployeeDetails.java

public class EmployeeDetails {
    Long employeeId;
    List<SkillDetails> skillDetails;  // mapping snippet is <collection property="skillDetails" ofType="package.SkillDetails" select="selectSkillDetails" column="employee_id">
    // get set
}

FooMapper.xml

  <resultMap id="EmployeeDetailsResultMap" type="java.util.LinkedHashMap" >
    <result column="employee_id" property="employeeId"/>
    <!-- employee_id as foreign key -->
    <collection property="skillDetails" ofType="package.SkillDetails" select="selectSkillDetails" column="employee_id">
        <result property="employeeId" column="employee_id"/>
        <result property="skillId" column="skill_id"/>
    </collection>
  </resultMap>

  <resultMap id="SkillDetailsResultMap" type="java.util.LinkedHashMap" >
    <result column="col1" property="p1" />
    <result column="etc." property="etc." />
  </resultMap>

  <select id="selectSkillDetails" resultMap="SkillDetailsResultMap" >
    SELECT *
      FROM skill_details t1
      where t1.employee_id=#{employee_id}
  </select>

  <select id="selectEmployeeDetails" resultMap="EmployeeDetailsResultMap">
    SELECT *
      FROM employee_details t1
      where t1.department_id=#{department_id}
  </select>

***In the same way, add: ** ***同理,添加:**

public class Department {
    Long departmentId;
    List<EmployeeDetails> employeeDetails;
    // get set
}

  <resultMap id="DepartmentsResultMap" type="java.util.LinkedHashMap" >
    <result column="department_id" property="departmentId" />
    <collection property="..." ofType="..." select="selectEmployeeDetails" column="employee_id">
      <result property="employeeId" column="employee_id"/>
      <result property="..." column="..."/>
    </collection>
  </resultMap>

  <select id="selectDepartments" resultMap="..." >
    SELECT *
      FROM ...
  </select>

They are all LinkedHashMap, -_-!它们都是LinkedHashMap,-_-!
Hope these help...希望这些帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM