需求:年度报告中加入月度报告,根据年度报告的yearReportId查询月度报告List
自定义返回值类型yearReportDTO.class
@Data
public class yearReportDto {
/**
* 年度报告id
*/
private Long yearReportId;
/**
* 团队code
*/
private String rootEntpCode;
/**
* 年份
*/
private Integer year;
/**
* 结果来源:1.填写,2.自主核算
*/
private Integer resultSource;
/**
* 修改日期
*/
private Date gmtModified;
/**
* 核算方法状态(1:未过期;2:过期)
*/
private Integer accountingMethodStatus;
/**
* 数据唯一标示
*/
private String uniqueCode;
/**
* 报告存储路径
*/
private String filePath;
/**
* 产值
*/
private BigDecimal outputValue;
/**
* 排放强度
*/
private BigDecimal emissionIntensity;
/**
* 碳排放总量
*/
private BigDecimal carbonEmissionsTotal;
/**
* 行业名称
*/
private String industryName;
/**
* 行业填报
*/
private String industryAndFill;
/**
* 月度报告
*/
private List<CmcMonthReport> monthReportList;
}
xml文件
查询出年报告id
<select id="carbonEmission01" resultMap="yearReportDto"
parameterType="com.sinocarbon.carbondata.dto.CmcYearReportDTO">
SELECT
yr.id AS yearReportId,
yr.root_entp_code AS rootEntpCode,
yr.`year`,
yr.result_source AS resultSource,
yr.gmt_modified AS gmtModified,
yr.accounting_method_status AS accountingMethodStatus,
yr.unique_code AS uniqueCode,
ywps.file_path AS filePath,
ywps.output_value AS outputValue,
ywps.emission_intensity AS emissionIntensity,
ywps.carbon_emissions_total AS carbonEmissionsTotal,
GROUP_CONCAT(ci.industry_name ORDER BY ci.id) AS industryName,
GROUP_CONCAT(CONCAT_WS(':',ci.id,rir.filling_mode)) AS industryAndFill
FROM
cmc_year_report AS yr
LEFT JOIN cmc_year_whole_plant_statistics AS ywps ON yr.id = ywps.year_report_id
LEFT JOIN cmc_year_report_industry_rela AS rir ON rir.report_id = yr.id
LEFT JOIN cmc_industry AS ci ON ci.id = rir.industry_id
WHERE yr.root_entp_code = #{rootEntpCode} AND yr.`status` = 1
<if test="resultSource != null and resultSource != ''">
and yr.result_source = #{resultSource}
</if>
GROUP BY yr.id ORDER BY yr.`year` desc
</select>
返回自定义类型的映射
<resultMap id="yearReportDto" type="com.sinocarbon.carbondata.dto.yearReportDTO">
<result column="yearReportId" property="yearReportId"/>
<result column="rootEntpCode" property="rootEntpCode"/>
<result column="year" property="year"/>
<result column="resultSource" property="resultSource"/>
<result column="gmtModified" property="gmtModified"/>
<result column="accountingMethodStatus" property="accountingMethodStatus"/>
<result column="uniqueCode" property="uniqueCode"/>
<result column="filePath" property="filePath"/>
<result column="outputValue" property="outputValue"/>
<result column="emissionIntensity" property="emissionIntensity"/>
<result column="carbonEmissionsTotal" property="carbonEmissionsTotal"/>
<result column="industryName" property="industryName"/>
<result column="industryAndFill" property="industryAndFill"/>
<collection property="monthReportList" select="listMonthReport" column="yearReportId" />
</resultMap>
collection 中property的“monthReportList”和自定义类中List的命名对应
select的“listMonthReport”不需要用foreach包裹
<select id="listMonthReport" resultMap="MonthReport">
select
cmr.id, cmr.root_entp_code as rootEntpCode, cmr.year_report_id as yearReportId, cmr.month as month,
cmr.result_source as resultSource, cmr.unique_code as uniqueCode, cmr.accounting_method_status as
accountingMethodStatus,
cmr.file_path as filePath
FROM cmc_month_report as cmr
where
year_report_id = #{yearReportId }
and status = 1
</select>
原先是这样写的
<select id="listMonthlyReports" resultType="java.util.Map" parameterType="java.util.List">
select
cmr.id, cmr.root_entp_code as rootEntpCode, cmr.year_report_id as yearReportId, cmr.month as month,
cmr.result_source as resultSource, cmr.unique_code as uniqueCode, cmr.accounting_method_status as accountingMethodStatus,
cmr.file_path as filePath
FROM cmc_month_report as cmr
where
year_report_id in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item.yearReportId }
</foreach>
and status = 1
</select>
然后返回类型映射,这里的column和property是自定义类命名
<resultMap id="MonthReport" type="com.sinocarbon.carbondata.pojo.CmcMonthReport">
<id column="id" property="id" />
<result column="rootEntpCode" property="rootEntpCode" />
<result column="yearReportId" property="yearReportId" />
<result column="month" property="month" />
<result column="resultSource" property="resultSource" />
<result column="uniqueCode" property="uniqueCode" />
<result column="accountingMethodStatus" property="accountingMethodStatus" />
<result column="filePath" property="filePath" />
<result column="status" property="status" />
<result column="gmtCreate" property="gmtCreate" />
<result column="gmtModified" property="gmtModified" />
</resultMap>
总结经验:resultMap和resultType的区别 认识不足
Mybatis的学习不扎实