解决思路:把联表查询的多 变为子查询
错误代码
<resultMap id="IndicatorTemplateResult" type="com.crcc.financial.vo.IndicatorTemplateVO">
<id column="id" property="id" />
<result column="template_code" property="templateCode" />
<result column="template_name" property="templateName" />
<result column="calculation_formula_type" property="calculationFormulaType" />
<result column="indicator_content" property="indicatorContent" />
<result column="create_time" property="createTime" />
<result column="create_name" property="createName" />
<result column="actual_value_type" property="actualValueType" />
<result column="planned_value_type" property="plannedValueType" />
<result column="index_calculation_formula" property="indexCalculationFormula" />
<!-- 一对多的关系 -->
<!-- property: 指的是集合属性的值,对应类中的属性名 ofType:指的是集合中元素的类型 -->
<collection property="calculationFormulaList" ofType="com.crcc.financial.vo.CalculationFormulaView">
<id column="cid" property="cid" />
<result column="subject_calculation_type" property="subjectCalculationType" />
<result column="calculation_formula" property="calculationFormula" />
<result column="subject_id" property="subjectId" />
<result column="input_number" property="inputNumber" />
<result column="subject_name" property="subjectName" />
</collection>
</resultMap>
<select id="listIndicatorTemplatePage" resultMap="IndicatorTemplateResult">
SELECT
t.id as id,
t.template_code,
t.template_name,
t.calculation_formula_type,
t.indicator_content,
t.create_name,
t.create_time,
t.actual_value_type,
t.planned_value_type,
t.index_calculation_formula,
c.id as cid,
c.subject_calculation_type,
c.calculation_formula,
c.subject_id,
c.input_number,
c.subject_name
FROM
f_indicator_template t
LEFT JOIN f_calculation_formula_view c ON t.id = c.parent_id
WHERE
t.data_status = 1
and
c.type=#{type}
<if test="templateCode !=null and templateCode != ''">
AND t.template_code LIKE concat('%',#{templateCode} ,'%')
</if>
<if test="templateName !=null and templateName != ''">
AND t.template_name LIKE concat('%',#{templateName} ,'%')
</if>
<if test="indicatorContent !=null and indicatorContent != ''">
AND t.indicator_content LIKE concat('%',#{indicatorContent} ,'%')
</if>
group by t.id
</select>
这个会导致分页条数不对,
正确代码:
<!--type表示dto层的返回类型,property是返回类型里面的参数,column是sql的字段-->
<resultMap type="com.crcc.financial.vo.IndicatorTemplateVO" id="ProjectDataResult">
<result property="id" column="id"/>
<result column="template_code" property="templateCode" />
<!--ofType对应的是下面resultMap的type, column里是要传递的父类值 select是查询的sql-->
<collection property="calculationFormulaList" ofType="com.crcc.financial.vo.CalculationFormulaView" column="{parent_id=parent_id}"
select="selectFiles"/>
</resultMap>
<resultMap type="com.crcc.financial.vo.CalculationFormulaView" id="ProjectFileResult">
<result column="subject_calculation_type" property="subjectCalculationType" />
<result column="calculation_formula" property="calculationFormula" />
<result column="subject_id" property="subjectId" />
<result column="input_number" property="inputNumber" />
<result column="subject_name" property="subjectName" />
</resultMap>
<select id="selectFiles" resultMap="ProjectFileResult">
SELECT
c.id AS cid,
c.subject_calculation_type,
c.calculation_formula,
c.subject_id,
c.input_number,
c.subject_name
FROM
f_calculation_formula_view c
where
c.parent_id=#{parent_id}
and c.type=2
</select>
<select id="listIndicatorTemplatePage" resultMap="ProjectDataResult">
SELECT
t.id as id,
t.id as parent_id,
t.template_code,
t.template_name,
t.calculation_formula_type,
t.indicator_content,
t.create_name,
t.create_time,
t.actual_value_type,
t.planned_value_type,
t.index_calculation_formula
FROM
f_indicator_template t
WHERE
t.data_status = 1
<if test="templateCode !=null and templateCode != ''">
AND t.template_code LIKE concat('%',#{templateCode} ,'%')
</if>
<if test="templateName !=null and templateName != ''">
AND t.template_name LIKE concat('%',#{templateName} ,'%')
</if>
<if test="indicatorContent !=null and indicatorContent != ''">
AND t.indicator_content LIKE concat('%',#{indicatorContent} ,'%')
</if>
</select>
修改为子查询 并且一和多创建独立resultMap 避免指向冲突