运用 在MyBatis3中为项目列表提供NULL

问题描述 投票:0回答:1

我想在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      
      }
    ]
  }
]

但是,当查询运行时,我得到一个大小为12的List<PaymentWorkingALL>,每个都有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>
java mybatis spring-mybatis
1个回答
0
投票

没有使用paymentWorkingALL结果图,因为你没有指示mybatis使用它,所以它有两个结果:

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

要修复此指定结果映射作为select节点的属性,如下所示:

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

© www.soinside.com 2019 - 2024. All rights reserved.