详解mybatis collection标签一对多的使用

所属分类: 软件编程 / java 阅读数: 52
收藏 0 赞 0 分享

查询, 结果集为AssociatedInfo:

<select id="queryReportAssociatedAcp" resultMap="AssociatedInfo">
 
    SELECT
    r.requisition_number AS business_code,
    r.id AS header_id,
    r.document_type_id AS reportTypeId,
    r.applicant_id as employeeId
    FROM
    fec_expense.exp_report_payment_schedule b,fec_expense.exp_report_header r
    WHERE
    b.exp_report_header_id=r.id and r.`status`=1004
    and
    b.frozen_flag ="Y"
    and r.applicant_id=#{applicationId}
    <if test="reportNumber!=null and reportNumber != ''">
      AND r.requisition_number LIKE concat(
      '%',
      concat(#{reportNumber,jdbcType=VARCHAR}, '%'))
    </if>
    <if test="documentTypeId !=null and reportNumber != ''">
      AND r.document_type_id = #{documentTypeId}
    </if>
    <if test="formTypes != null and formTypes.size > 0">
      AND r.document_type_id IN
      <foreach collection="formTypes" item="formId" open="(" separator="," close=")">
        #{formId}
      </foreach>
    </if>
    AND (
    b.amount - ( SELECT
    COALESCE(sum( c.write_off_amount ), 0) AS write_off_amount
    FROM
    csh_write_off c
    WHERE
    c.document_header_id = b.exp_report_header_id
    AND c.document_line_id = b.id
    AND ( c.STATUS = 'Y' OR ( c.STATUS = 'P' AND c.operation_type = 'WRITE_OFF' ) )
    ) - (
    SELECT
    COALESCE(sum( a.amount ), 0) AS commit_amount
    FROM
    csh_data_relation_acp a
    WHERE
    a.report_head_id = b.exp_report_header_id
    AND a.report_line_id = b.id
    AND a.document_type = 'ACP_REQUISITION'
    ) > 0
    )
    GROUP BY
    r.requisition_number,
    b.exp_report_header_id,
    r.document_type_id,
    b.applicant_id
    ORDER BY
    r.requisition_number
 
  </select>

结果集 AssociatedInfo: 使用collection 实现1对多的场景, CashDataPublicReportHeaderDTO实体里包含一个行的集合List<CashDataPublicReportLineDTO> lines: 

<resultMap id="AssociatedInfo" type="com.hand.hcf.app.payment.web.dto.CashDataPublicReportHeaderDTO">
    <result column="header_id" property="reportHeadId"/>
    <result column="business_code" property="reportNumber"/>
    <result column="form_name" property="reportTypeName"/>
    <collection property="lineList" column="{headerId=header_id}"
          ofType="ArrayList" select="getPaymentInfo"/>
  </resultMap>
  <select id="getPaymentInfo" resultType="com.hand.hcf.app.payment.web.dto.CashDataPublicReportLineDTO">
    SELECT
	temp.id scheduleLineId,
	"" AS cshTransactionId,
	temp.amount,
	temp.associated_amount associatedAmount,
	(
	temp.amount - temp.associated_amount - (
SELECT COALESCE
	( sum( c.write_off_amount ), 0 ) AS write_off_amount
FROM
	csh_write_off c
WHERE
	c.document_header_id = temp.exp_report_header_id
	AND c.document_line_id = temp.id
	AND c.document_type = "PUBLIC_REPORT"
	AND ( c.STATUS = 'Y' OR ( c.STATUS = 'P' AND c.operation_type = 'WRITE_OFF' ) )
	)
	) AS availableAmount,
	temp.exp_report_header_id expReportHeaderId,
	0 AS scheduleLineNumber,
	temp.company_id companyId,
	temp.currency_code currency,
	temp.description description,
	temp.exchange_rate exchangeRate,
	temp.payment_schedule_date schedulePaymentDate,
	temp.payment_method paymentMethod,
	temp.payment_type,
	temp.prop_flag prop_flag,
	temp.csh_transaction_class_id cshTransactionClassId,
	( SELECT ctc.description FROM csh_transaction_class ctc WHERE ctc.id = temp.csh_transaction_class_id ) AS cshTransactionClassName,
	temp.cash_flow_item_id cashFlowItemId,
	temp.payee_category payeeCategory,
	temp.payee_id payeeId,
	temp.account_number accountNumber,
	temp.account_name accountName,
	temp.bank_code bankCode,
	temp.bank_name bankName,
	temp.bank_code bankLocationCode,
	temp.bank_name bankLocationName,
	"" provinceCode,
	"" provinceName,
	""cityCode,
	"" cityName,
	 (select c.type_code from csh_transaction_class c where c.id=temp.csh_transaction_class_id ) cshTransactionTypeCode,
	""contractHeaderId
FROM
	(
SELECT
  b.*,
	(
SELECT COALESCE
	( sum( a.amount ), 0 ) AS associated_amount
FROM
	csh_data_relation_acp a
WHERE
	a.report_head_id = b.exp_report_header_id
	AND a.report_line_id = b.id
	AND a.document_type = 'ACP_REQUISITION'
	) AS associated_amount
FROM
	fec_expense.exp_report_payment_schedule b ,fec_expense.exp_report_header r
WHERE
	b.frozen_flag = "Y"
	and b.exp_report_header_id=r.id
	AND b.exp_report_header_id = #{headerId}
	) temp
 
  </select>

包含列表的实体类 CashDataPublicReportHeaderDTO :

package com.hand.hcf.app.payment.web.dto;
 
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Data;
 
import java.time.ZonedDateTime;
import java.util.List;
 
@Data
public class CashDataPublicReportHeaderDTO {
 
  @JsonSerialize(using = ToStringSerializer.class)
  private Long reportHeadId;//报账单头ID
 
  private String reportNumber;//报账单编号
 
  private String reportTypeName;//报账单类型
  @JsonSerialize(using = ToStringSerializer.class)
  private Long reportTypeId;// 报账单类型ID
 
  private List<CashDataPublicReportLineDTO> lineList;//报账单计划付款行
 
  @JsonSerialize(using = ToStringSerializer.class)
  private Long employeeId;//员工ID
  private String employeeName;//员工名称
 
  private ZonedDateTime requisitionDate; // 申请日期
}
更多精彩内容其他人还在看

SpringBoot中使用Ehcache的详细教程

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

在idea 中添加和删除模块Module操作

这篇文章主要介绍了在idea 中添加和删除模块Module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java spring整合junit操作(有详细的分析过程)

这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解JAVA 弱引用

这篇文章主要介绍了 JAVA 弱引用的相关资料,帮助大家更好的理解和学习java引用对象,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

深入了解JAVA 虚引用

这篇文章主要介绍了JAVA 虚引用的相关资料,帮助大家更好的理解和学习JAVA,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解JAVA 强引用

这篇文章主要介绍了JAVA 强引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

java中的按位与(&)用法说明

这篇文章主要介绍了java中的按位与(&)用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

深入了解JAVA 软引用

这篇文章主要介绍了JAVA 软引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

利用MyBatis实现条件查询的方法汇总

这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound

这篇文章主要介绍了Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.Pa
收藏 0 赞 0 分享
查看更多