如何使用Mybatis将Addresses类与EmployeeMap中的Address列表进行映射?

huangapple go评论77阅读模式
英文:

How to map Addresses class with list of Address in EmployeeMap using Mybatis?

问题

如何使用Mybatis将Addresses类与EmployeeMap中的Address列表进行映射?我的代码是否正确?

以下是用于获取employeeId和多个地址位置的查询。如何在MyBatis中映射这些字段?感谢您帮助映射这些字段。请注意,我无法发布包含其他字段的完整Address类。

<resultMap id="EmployeeMap" type="Employee">
    <result column="emplId" property="emplId"/>
    <collection property="addressList">
        <result column="addressLineOne" property="addressLineOne"/>
        <result column="addressLineTwo" property="addressLineTwo"/>
        <result column="city" property="city"/>
        <result column="country" property="country"/>
        <result column="zipCode" property="zipCode"/>
    </collection>
</resultMap>
<select id="employeeData" resultMap="EmployeeMap">Select * from employee</select>

这是我的Java代码:

public class Employee {
    String emplId;
    Addresses addressList;

    public String getEmplId() {
        return emplId;
    }

    public void setEmplId(String value) {
        this.emplId = value;
    }

    public Addresses getAddressList() {
        return addressList;
    }

    public void setAddressList(Addresses value) {
        this.addressList = value;
    }
}

public class Addresses {
    List<Address> addresses;

    public List<Address> getAddresses() {
        if (addresses == null) {
            addresses = new ArrayList<Address>();
        }
        return this.addresses;
    }
}

public class Address {
    String city;

    public String getCity() {
        return city;
    }

    public void setCity(String value) {
        this.city = value;
    }
}
英文:

**How to map Addresses class with list of Address in EmployeeMap using Mybatis? is my code below correct? **

Below is query is to get employeeId and hos multiple address locations. How to map those fields in MyBatis? Appreciate your help to map the fields. FYI, I could not post complete Address class with other fields

&lt;resultMap id=&quot;EmployeeMap&quot; type=&quot;Employee&quot;&gt;
	&lt;result column=&quot;emplId&quot; property=&quot;emplId&quot;/&gt;
	&lt;collection property=&quot;addressList&quot; &gt;
		&lt;result column=&quot;addressLineOne&quot; property=&quot;addressLineOne&quot;/&gt;
		&lt;result column=&quot;addressLineTwo&quot; property=&quot;addressLineTwo&quot;/&gt;
		&lt;result column=&quot;city&quot; property=&quot;city&quot;/&gt;
		&lt;result column=&quot;country&quot; property=&quot;country&quot;/&gt;
		&lt;result column=&quot;zipCode&quot; property=&quot;zipCode&quot;/&gt;
	&lt;/collection&gt;
 &lt;/resultMap&gt;
 &lt;select id=&quot;employeeData&quot; resultMap=&quot;EmployeeMap&quot;&gt;Select * from employee&lt;/select&gt;

Here is My Java code

public class Employee{

    String emplId;
    Addresses addressList;

    public String getEmplId() {
        return emplId;
    }

   public void setEmplId(Addresses value) {
        this.emplId = value;
   }


   public Addresses getAddressList() {
       return addressList;
   }

   public void setAddressList(Addresses value) {
       this.addressList = value;
   }
}
public class Addresses{

   List&lt;Address&gt; addresses;

   public List&lt;Address&gt; getAddresses() {
     if (addresses == null) {
        addresses = new ArrayList&lt;Address&gt;();
      }
     return this.addresses;
   }
 }
 public class Address{
   String city;
     
  public String getCity(){
       return city;
   }

   public void setCity(String value) {
       this.city = value;
   }
}

答案1

得分: 1

&lt;collection /&gt; 的目标属性是 addressList.addresses,因此结果映射应如下所示:

&lt;resultMap id=&quot;EmployeeMap&quot; type=&quot;Employee&quot;&gt;
  &lt;id column=&quot;emplId&quot; property=&quot;emplId&quot;/&gt;
  &lt;collection property=&quot;addressList.addresses&quot;
      javaType=&quot;list&quot; ofType=&quot;Address&quot;&gt;
    &lt;result column=&quot;addressLineOne&quot; property=&quot;addressLineOne&quot;/&gt;
    &lt;result column=&quot;addressLineTwo&quot; property=&quot;addressLineTwo&quot;/&gt;
    &lt;result column=&quot;city&quot; property=&quot;city&quot;/&gt;
    &lt;result column=&quot;country&quot; property=&quot;country&quot;/&gt;
    &lt;result column=&quot;zipCode&quot; property=&quot;zipCode&quot;/&gt;
  &lt;/collection&gt;
&lt;/resultMap&gt;

注意:您应该在父结果映射中指定 &lt;id /&gt;

英文:

The target property of &lt;collection /&gt; is addressList.addresses, so the result map should look as follows:

&lt;resultMap id=&quot;EmployeeMap&quot; type=&quot;Employee&quot;&gt;
  &lt;id column=&quot;emplId&quot; property=&quot;emplId&quot;/&gt;
  &lt;collection property=&quot;addressList.addresses&quot;
      javaType=&quot;list&quot; ofType=&quot;Address&quot;&gt;
    &lt;result column=&quot;addressLineOne&quot; property=&quot;addressLineOne&quot;/&gt;
    &lt;result column=&quot;addressLineTwo&quot; property=&quot;addressLineTwo&quot;/&gt;
    &lt;result column=&quot;city&quot; property=&quot;city&quot;/&gt;
    &lt;result column=&quot;country&quot; property=&quot;country&quot;/&gt;
    &lt;result column=&quot;zipCode&quot; property=&quot;zipCode&quot;/&gt;
  &lt;/collection&gt;
&lt;/resultMap&gt;

Note: You should specify &lt;id /&gt; in the parent result map.

huangapple
  • 本文由 发表于 2020年4月8日 12:55:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/61093582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定