英文:
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
<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>
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<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;
}
}
答案1
得分: 1
<collection />
的目标属性是 addressList.addresses
,因此结果映射应如下所示:
<resultMap id="EmployeeMap" type="Employee">
<id column="emplId" property="emplId"/>
<collection property="addressList.addresses"
javaType="list" ofType="Address">
<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>
注意:您应该在父结果映射中指定 <id />
。
英文:
The target property of <collection />
is addressList.addresses
, so the result map should look as follows:
<resultMap id="EmployeeMap" type="Employee">
<id column="emplId" property="emplId"/>
<collection property="addressList.addresses"
javaType="list" ofType="Address">
<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>
Note: You should specify <id />
in the parent result map.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论