在 MyBatis3 中使用 <collection> 为项目列表提供 NULL

Using <collection> in MyBatis3 gives NULL for list of items

我正在尝试在 MyBatis3 中使用 <collection>。但是,构成 List/collection 一部分的所有项目始终为 NULL。

这是我的 SQL -

<select id="fetchPaymentWorkingALL" resultType="paymentWorkingALL" parameterType="java.util.Map">
    select
        (person_id + '-' + convert(char(10), end_date, 126) + '-' + company + '-' + plan) as paymentid
         ,person_id as personid
         ,(person_id + '-' + convert(char(10), end_date, 126) + '-' + company + '-' + plan + '-' + 'non_mid_year') as otherid
         ,'non_mid_year' as typename
         ,'0' as typeid
         ,sum(amount) as amount
         ,sum(return1_amount) + sum(return2_amount) as returnamount
         ,sum(endamount) as endamount
    from #ABCD
    group by person_id, income_type, end_date, company, plan, plan_id
</select>

查询的输出如下所示 -

paymentid                        | personid    | otherid                                       | typename        | typeid  |     amount        | returnamount  | endamount
---------------------------------|-------------|-----------------------------------------------|-----------------|---------|-------------------|---------------|------------------
3520-2017-12-31-ABCD-Mandatory   | 3520        | 3520-2017-12-31-ABCD-Mandatory-non_mid_year   | non_mid_year    |    0    |    10000.0000     | 1200.0000     | 11200.0000
3520-2017-12-31-ABCD-Mandatory   | 3520        | 3520-2017-12-31-ABCD-Mandatory-total          | total           |    2    |    10000.0000     | 1200.0000     | 11200.0000
3520-2018-12-31-ABCD-Mandatory   | 3520        | 3520-2018-12-31-ABCD-Mandatory-mid_year       | mid_year        |    1    |    15000.0000     | 1150.0000     | 16150.0000
3520-2018-12-31-ABCD-Mandatory   | 3520        | 3520-2018-12-31-ABCD-Mandatory-non_mid_year   | non_mid_year    |    0    |    10000.0000     | 1200.0000     | 11200.0000
3520-2018-12-31-ABCD-Mandatory   | 3520        | 3520-2018-12-31-ABCD-Mandatory-total          | total           |    2    |    25000.0000     | 2350.0000     | 27350.0000
3520-2019-12-31-EFGH-Mandatory   | 3520        | 3520-2019-12-31-EFGH-Mandatory-non_mid_year   | non_mid_year    |    0    |    10000.0000     | 1200.0000     | 11200.0000
3520-2019-12-31-EFGH-Mandatory   | 3520        | 3520-2019-12-31-EFGH-Mandatory-total          | total           |    2    |    10000.0000     | 1200.0000     | 11200.0000
3520-2019-12-31-ABCD-Mandatory   | 3520        | 3520-2019-12-31-ABCD-Mandatory-mid_year       | mid_year        |    1    |    15000.0000     | 1150.0000     | 16150.0000
3520-2019-12-31-ABCD-Mandatory   | 3520        | 3520-2019-12-31-ABCD-Mandatory-non_mid_year   | non_mid_year    |    0    |    10000.0000     | 1200.0000     | 11200.0000
3520-2019-12-31-ABCD-Mandatory   | 3520        | 3520-2019-12-31-ABCD-Mandatory-total          | total           |    2    |    25000.0000     | 2350.0000     | 27350.0000
3520-2020-12-31-ABCD-Mandatory   | 3520        | 3520-2020-12-31-ABCD-Mandatory-mid_year       | mid_year        |    1    |    15000.0000     | 1150.0000     | 16150.0000
3520-2020-12-31-ABCD-Mandatory   | 3520        | 3520-2020-12-31-ABCD-Mandatory-total          | total           |    2    |    15000.0000     | 1150.0000     | 16150.0000

resultMaps看起来是这样的-

<resultMap id="paymentWorkingALL" type="PaymentWorkingALL">
    <id property="paymentid" column="paymentid" />
    <result property="personid" column="personid" />
    <collection property="paymentWorkings"
        ofType="PaymentWorking"
        resultMap="paymentWorkingMap" />

</resultMap>

<resultMap id="paymentWorkingMap" type="PaymentWorking">
    <id property="otherid" column="otherid" />
    <result property="typename" column="typename"/>
    <result property="typeid" column="typeid"/>
    <result property="amount" column="amount"/>
    <result property="endamount" column="endamount"/>
    <result property="returnamount" column="returnamount"/>
</resultMap>

我也像这样添加了别名 -

<typeAlias type="com.abcd.PaymentWorkingALL" alias="PaymentWorkingALL"/>
<typeAlias type="com.abcd.PaymentWorking" alias="PaymentWorking"/>

我的类看起来是这样的-

public class PaymentWorkingALL {

    private String paymentid;
    private String personid;
    private List<PaymentWorking> paymentWorkings;

    public String getPaymentid() {
        return paymentid;
    }

    public void setPaymentid(String paymentid) {
        this.paymentid = paymentid;
    }

    public String getPersonid() {
        return personid;
    }

    public void setPersonid(String personid) {
        this.personid = personid;
    }

    public List<PaymentWorking> getPaymentWorkings() {
        return paymentWorkings;
    }

    public void setPaymentWorkings(List<PaymentWorking> paymentWorkings) {
        this.paymentWorkings = paymentWorkings;
    }
}
public class PaymentWorking {

    private String otherid;
    private String typename;
    private String typeid;
    private Double amount;
    private Double returnamount;
    private Double endamount;

    public String getOtherid() {
        return otherid;
    }

    public void setOtherid(String otherid) {
        this.otherid = otherid;
    }

    public String getTypename() {
        return typename;
    }

    public void setTypename(String typename) {
        this.typename = typename;
    }

    public String getTypeid() {
        return typeid;
    }

    public void setTypeid(String typeid) {
        this.typeid = typeid;
    }

    public Double getAmount() {
        return amount;
    }

    public void setAmount(Double amount) {
        this.amount = amount;
    }

    public Double getReturnamount() {
        return returnamount;
    }

    public void setReturnamount(Double returnamount) {
        this.returnamount = returnamount;
    }

    public Double getEndamount() {
        return endamount;
    }

    public void setEndamount(Double endamount) {
        this.endamount = endamount;
    }
}

我的期望是我会得到一个如下所示的列表 -

[
  {
    "paymentid": "3520-2017-12-31-ABCD-Mandatory",
    "personid": "3520",
    "paymentWorkings": [
      {
        "otherid": "3520-2017-12-31-ABCD-Mandatory-non_mid_year",
        "typename": "non_mid_year",
        "typeid": "0",
        "amount": 10000.00,
        "returnamount": 1200.00,
        "endamount": 11200.00      
      },
      {
        "otherid": "3520-2017-12-31-ABCD-Mandatory-total",
        "typename": "total",
        "typeid": "2",
        "amount": 10000.00,
        "returnamount": 1200.00,
        "endamount": 11200.00      
      }
    ]
  },
  {
    "paymentid": "3520-2018-12-31-ABCD-Mandatory",
    "personid": "3520",
    "paymentWorkings": [
      {
        "otherid": "3520-2018-12-31-ABCD-Mandatory-non_mid_year",
        "typename": "non_mid_year",
        "typeid": "0",
        "amount": 10000.00,
        "returnamount": 1200.00,
        "endamount": 11200.00      
      },
      {
        "otherid": "3520-2018-12-31-ABCD-Mandatory-mid_year",
        "typename": "mid_year",
        "typeid": "1",
        "amount": 15000.00,
        "returnamount": 1150.00,
        "endamount": 16150.00      
      },
      {
        "otherid": "3520-2018-12-31-ABCD-Mandatory-total",
        "typename": "total",
        "typeid": "2",
        "amount": 25000.00,
        "returnamount": 2350.00,
        "endamount": 27350.00      
      }
    ]
  }
]

但是,当查询运行时,我得到一个 List<PaymentWorkingALL>,大小为 12,每个 paymentWorkingsNULL

根据我的理解,它应该返回长度为 5List<PaymentWorkingALL> 并且它们的 ID 将是 -

之一
'3520-2017-12-31-ABCD-Mandatory',
'3520-2018-12-31-ABCD-Mandatory',
'3520-2019-12-31-EFGH-Mandatory',
'3520-2019-12-31-ABCD-Mandatory',
'3520-2020-12-31-ABCD-Mandatory'

我正在使用以下版本的mybatis和mybatis-spring -

<mybatis.version>3.2.3</mybatis.version>
<mybatis.spring.version>1.2.0</mybatis.spring.version>

paymentWorkingALL result map没有被使用是因为你没有指示mybatis使用它,所以有两个后果:

  1. mybatis 不知道 PaymentWorkingALLid 字段是什么,并将所有行视为唯一对象(因此结果中有 12 个对象)
  2. paymentWorkings 根本没有映射关联(因此不会在集合中创建对象)

要解决此问题,请将结果映射指定为 select 节点的属性,如下所示:

<select id="fetchPaymentWorkingALL" resultMap="paymentWorkingALL" parameterType="java.util.Map">
 ...
</select>