gpt4 book ai didi

java - 如何使用 Mybatis 将 Addresses 类与 EmployeeMap 中的地址列表进行映射?

转载 作者:行者123 更新时间:2023-12-01 17:32:27 25 4
gpt4 key购买 nike

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

下面的查询是获取employeeId 和hos 多个地址位置。 MyBatis 中如何映射这些字段?感谢您帮助绘制字段。仅供引用,我无法发布包含其他字段的完整地址类

<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(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;
}
}

最佳答案

<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 />在父结果图中。

关于java - 如何使用 Mybatis 将 Addresses 类与 EmployeeMap 中的地址列表进行映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61093582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com