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

时间:2019-04-02 12:32:16

标签: java mybatis spring-mybatis

我正在尝试使用 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 ,每个都有paymentWorkings作为NULL

据我了解,它应该返回长度为 5 List<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>

1 个答案:

答案 0 :(得分:0)

paymentWorkingALL未使用结果图,因为您尚未指示mybatis使用它,因此有两个结果:

  1. mybatis不知道id的{​​{1}}字段是什么,并将所有行都视为唯一对象(因此结果中有12个对象)
  2. PaymentWorkingALL关联根本未映射(因此未在集合中创建对象)

要解决此问题,请指定结果图作为paymentWorkings节点的属性,如下所示:

select