连接表并将结果保存在 ArrayList-Attribute 中

Join tables and save the result in an ArrayList-Attribute

每个客户都可以拥有多个驾驶执照 classes。例如:客户 John 可以拥有 classes A、B 和 C。有一个 table 客户、DrivingLicence 和 DrivingLicenceCustomer。

select 语句如下所示(在我的映射器中):

<select id="selectCustomer" resultType="CustomerDTO">
    SELECT *
    FROM Customer c
    INNER JOIN DrivingLicenseCustomer d
    ON c.CustomerID = d.Customer
</select>

结果可能是:

+------------+-------+---------------------+
| CustomerId | Name  | DrivingLicenceClass |
+------------+-------+---------------------+
|          1 | John  | A                   |
|          1 | John  | B                   |
|          1 | John  | C                   |
|          2 | Clara | A                   |
|          3 | Sarah | C                   |
+------------+-------+---------------------+

客户的 class 如下所示:

public class CustomerDTO {
    private int customerId;
    private String name;
    private ArrayList<String> drivingLicences;
    //....
}

正如您在上面的结果中看到的:同一客户有多个行(每行代表一个对象)。目前我的实现无法正确映射 DrivingLicenceClass。结果应如下所示:

+------------+-------+---------------------+
| CustomerId | Name  | DrivingLicenceClass |
+------------+-------+---------------------+
|          1 | John  | {A, B, C}           |
|          2 | Clara | {A}                 |
|          3 | Sarah | {C}                 |
+------------+-------+---------------------+

我的MyBatis-Mapper可以自动填充ArrayList吗? 非常感谢!!!

我用下面的代码解决了:

for (CustomerDTO customerDTO : customerList) {
        List<DrivingLicence> licenceList = s.selectList("selectDrivingLicencesByCustomer", customerDTO.getCustomerId());
}

(Select 每个客户的驾驶执照)

但这并不是我真正想要的方式。如果有更好的方法,请告诉我。