使用mybatis中collection出现的列名无效

在项目中需要根据指标ID查询出指标的信息,在查询出指标的指标ID和版本号在指标维度映射表查询出维度ID。

在指标表中多增加了一个字段version(版本号),指标维度映射表也增加了version字段

当查询指标信息时sql为:

<select id="selectIndexById" parameterType="hygd.index.model.Index" resultMap="Index">
    SELECT  INDEX_ID,
            A.DATA_SOURCE_SYSTEM,
            INDEX_VALUE_DATA_ATTRIBUTE,
            INDEX_CN_NAME,
            INDEX_EN_NAME,
            INDEX_ALIAS_ABBREVIATION,
            INDEX_QUERY_FAST_CODE,
            INDEX_DEFINE_EXPLAIN,
            INDEX_METRIC,
            MEASUREMENT,
            STATISTIC_FREQUENCY,
            INDEX_TYPE,
            INDEX_VALUE_PRODUCT_MODE,
            SERVICE_TYPE,
            CUSTOM_TYPE1,
            CUSTOM_TYPE2,
            CUSTOM_TYPE3,
            SERVICE_LOGIC_RULE,
            OWNER,
            A.SECURITY_LEVEL,
            EFFECT_DATE,
            NO_EFFECT_DATE,
            RELEASE_STATUS,
            A.OPERATOR,
            MODIFY_TIME,
            A.START_TIME,
            A.END_TIME,
            A.MODEL_CODE,
            A.CREATE_TIME,
            INDEX_CODE,
            A.GROUP_ID,
            b.FORM_CN_NAME,
            a.derive_type
    FROM IDX_INDEX a LEFT JOIN idx_model b ON a.MODEL_CODE = b.MODEL_CODE
    <where>
        INDEX_ID = #{indexId,jdbcType=VARCHAR}
        <if test="version != null and version != ''">
            and VERSION = #{version,jdbcType=VARCHAR}
        </if>
        <if test="version == null or version == ''">
            and RELEASE_STATUS = '1'
        </if>
    </where>
</select>

Index定义为

<resultMap id="Index" type="hygd.index.model.Index">
    <result column="INDEX_ID" jdbcType="VARCHAR" property="indexId"/>
    <result column="DATA_SOURCE_SYSTEM" jdbcType="VARCHAR" property="dataSourceSystem"/>
    <result column="INDEX_VALUE_DATA_ATTRIBUTE" jdbcType="VARCHAR" property="indexValueDataAttribute"/>
    <result column="INDEX_CN_NAME" jdbcType="VARCHAR" property="indexCnName"/>
    <result column="INDEX_EN_NAME" jdbcType="VARCHAR" property="indexEnName"/>
    <result column="INDEX_ALIAS_ABBREVIATION" jdbcType="VARCHAR" property="indexAliasAbbreviation"/>
    <result column="INDEX_QUERY_FAST_CODE" jdbcType="VARCHAR" property="indexQueryFastCode"/>
    <result column="INDEX_DEFINE_EXPLAIN" jdbcType="VARCHAR" property="indexDefineExplain"/>
    <result column="INDEX_METRIC" jdbcType="VARCHAR" property="indexMetric"/>
    <result column="MEASUREMENT" jdbcType="VARCHAR" property="measurement"/>
    <result column="STATISTIC_FREQUENCY" jdbcType="VARCHAR" property="statisticFrequency"/>
    <result column="INDEX_TYPE" jdbcType="VARCHAR" property="indexType"/>
    <result column="INDEX_VALUE_PRODUCT_MODE" jdbcType="VARCHAR" property="indexValueProductMode"/>
    <result column="SERVICE_TYPE" jdbcType="VARCHAR" property="serviceType"/>
    <result column="CUSTOM_TYPE1" jdbcType="VARCHAR" property="customType1"/>
    <result column="CUSTOM_TYPE2" jdbcType="VARCHAR" property="customType2"/>
    <result column="CUSTOM_TYPE3" jdbcType="VARCHAR" property="customType3"/>
    <result column="SERVICE_LOGIC_RULE" jdbcType="VARCHAR" property="serviceLogicRule"/>
    <result column="OWNER" jdbcType="VARCHAR" property="owner"/>
    <result column="SECURITY_LEVEL" jdbcType="VARCHAR" property="securityLevel"/>
    <result column="EFFECT_DATE" jdbcType="VARCHAR" property="effectDate"/>
    <result column="NO_EFFECT_DATE" jdbcType="VARCHAR" property="noEffectDate"/>
    <result column="RELEASE_STATUS" jdbcType="VARCHAR" property="releaseStatus"/>
    <result column="OPERATOR" jdbcType="VARCHAR" property="operator"/>
    <result column="MODIFY_TIME" jdbcType="VARCHAR" property="modifyTime"/>
    <result column="START_TIME" jdbcType="VARCHAR" property="startTime"/>
    <result column="END_TIME" jdbcType="VARCHAR" property="endTime"/>
    <result column="MODEL_CODE" jdbcType="VARCHAR" property="modelCode"/>
    <result column="CREATE_TIME" jdbcType="VARCHAR" property="createTime"/>
    <result column="INDEX_CODE" jdbcType="VARCHAR" property="indexCode"/>
    <result column="GROUP_ID" jdbcType="VARCHAR" property="groupId"/>
    <result column="FORM_CN_NAME" jdbcType="VARCHAR" property="modelName"/>
    <result column="DERIVE_TYPE" jdbcType="VARCHAR" property="deriveType"/>
    <result column="DERIVE_TYPE" jdbcType="VARCHAR" property="deriveType"/>
    <result column="INDEX_DIMENSION" jdbcType="VARCHAR" property="indexDimension"/>
    <result column="VERSION" jdbcType="VARCHAR" property="version"/>
    <collection property="indexDimensionRelations" column="{indexId=INDEX_ID,version=VERSION}"
                ofType="hygd.index.model.IndexDimensionRelation" select="selectRelations"/>
</resultMap>
执行的时候报错:列名无效,但是将日志中的sql复制到plsql执行可以成功,所以一直不知道问题出在哪儿

后来看到resultMap中Index定义最后一个collection中 select方法中有两个参数,indexId和version

<select id="selectRelations" resultMap="RelationMap">
    select INDEX_ID,DIMENSION_ID
      from IDX_INDEX_DIMENSION_RELATION t1
    WHERE t1.INDEX_ID = #{indexId,jdbcType=VARCHAR} and T1.VERSION = #{version,jdbcType=VARCHAR}
    ORDER BY t1.DIMENSION_ID
</select>
而第一个查询字段中没有查询version字段,所以version获取不到

修改:将selectIndexById方法中增加version字段即可。






猜你喜欢

转载自blog.csdn.net/qq_38130280/article/details/79016610