mybatis一对多映射

场景:

  • A:SecControlRulePojo.java
  • B:SecControlSubRulePojo
  • C:SecControlSubRuleManyPojo

实体A中包含List<B>字段,实体B中包含List<C>字段

使用mybatis完成查询映射。

首先定义ResultMap

<resultMap
        type="com.wisdombud.dth.boss.control.pojo.SecControlRulePojo"
        id="FULL_RESULT_MAP">
        <id column="ID" jdbcType="DECIMAL" property="id" />
        <result column="NAME" jdbcType="VARCHAR" property="name" />
        <result column="ACTION_CODE" jdbcType="DECIMAL"
            property="actionCode" />
        <result column="TIP_INFO" jdbcType="VARCHAR" property="tipInfo" />
        <result column="IS_VALID" jdbcType="DECIMAL" property="isValid" />
        <result column="IS_WHITE" jdbcType="DECIMAL" property="isWhite" />
        <result column="CREATE_LOGIN_NO" jdbcType="VARCHAR"
            property="createLoginNo" />
        <result column="CREATE_TIME" jdbcType="TIMESTAMP"
            property="createTime" />
        <result column="CREATE_SERIAL_NUM" jdbcType="DECIMAL"
            property="createSerialNum" />
        <result column="LAST_UPDATE_TIME" jdbcType="TIMESTAMP"
            property="lastUpdateTime" />
        <result column="LAST_LOGIN_NO" jdbcType="VARCHAR"
            property="lastLoginNo" />
        <result column="LAST_SERIAL_NUM" jdbcType="DECIMAL"
            property="lastSerialNum" />
        <result column="REMARK" jdbcType="VARCHAR" property="remark" />
        <result column="RE1" jdbcType="VARCHAR" property="re1" />
        <result column="RE2" jdbcType="VARCHAR" property="re2" />
        <result column="RE3" jdbcType="VARCHAR" property="re3" />
        <result column="RE4" jdbcType="VARCHAR" property="re4" />
        <collection property="subRuleList"
            ofType="com.wisdombud.dth.boss.control.pojo.SecControlSubRulePojo">
            <result column="ID" property="id" />
            <result column="RULE_ID" jdbcType="DECIMAL" property="ruleId" />
            <result column="SUB_ACTION_CODE" jdbcType="DECIMAL"
                property="actionCode" />
            <result column="SUB_IS_VALID" jdbcType="DECIMAL"
                property="isValid" />
            <result column="SUB_IS_WHITE" jdbcType="DECIMAL"
                property="isWhite" />
            <result column="FACTOR_CODE" jdbcType="DECIMAL"
                property="factorCode" />
            <result column="OPERATOR_CODE" jdbcType="DECIMAL"
                property="operatorCode" />
            <result column="QUANTITY_VALUE" jdbcType="DECIMAL"
                property="quantityValue" />
            <result column="QUANTITY_TIME_START" jdbcType="TIMESTAMP"
                property="quantityTimeStart" />
            <result column="QUANTITY_TIME_END" jdbcType="TIMESTAMP"
                property="quantityTimeEnd" />
            <collection property="manyList"
                ofType="com.wisdombud.dth.boss.control.pojo.SecControlSubRuleManyPojo">
                <result column="SUB_MANY_ID" property="id" />
                <result column="SUB_RULE_ID" property="subRuleId" />
                <result column="VALUE_ID" property="valueId" />
                <result column="VALUE_STRING" property="valueString" />
                <result column="EXPRESSION" property="expression" />
            </collection>
        </collection>
    </resultMap>

如果实体中包含相同的列名,需要给列名取别名并设置

<result column="别名" property="实体字段名" />

SQL:

<select id="selectAll" resultMap="FULL_RESULT_MAP">
        SELECT
        controlRule.ID,controlRule.NAME,controlRule.ACTION_CODE,controlRule.CREATE_LOGIN_NO,
        controlRule.CREATE_SERIAL_NUM,controlRule.CREATE_TIME,controlRule.IS_VALID,controlRule.IS_WHITE,
        controlRule.LAST_LOGIN_NO,controlRule.LAST_SERIAL_NUM,controlRule.LAST_UPDATE_TIME,

        subRule.ID subId,subRule.RULE_ID,subRule.ACTION_CODE
        SUB_ACTION_CODE,subRule.IS_VALID SUB_IS_VALID,subRule.IS_WHITE
        SUB_IS_WHITE,
        subRule.FACTOR_CODE,subRule.OPERATOR_CODE,subRule.QUANTITY_VALUE,subRule.QUANTITY_TIME_START,subRule.QUANTITY_TIME_END,

        subRuleMany."ID"
        SUB_MANY_ID,subRuleMany.SUB_RULE_ID,subRuleMany.VALUE_ID,subRuleMany.VALUE_STRING,subRuleMany.EXPRESSION
        FROM
        SEC_CONTROL_RULE controlRule,
        SEC_CONTROL_SUB_RULE subRule,
        SEC_CONTROL_SUB_RULE_MANY subRuleMany
        WHERE
        controlRule."ID"=subRule.RULE_ID AND subRule.RULE_ID =
        subRuleMany.SUB_RULE_ID
    </select>

 

posted on 2018-05-07 18:32  xushy  阅读(243)  评论(0编辑  收藏  举报

导航