• 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>
  • 相关阅读:
    listview删除item
    标准listview加图标布局
    android事件消费机制,从外传到里面,里面具有优先选择权,如果里面的不需要,则传递给外面一层消费
    listview 按最新数据展示
    给listview添加数据,添加数据之后即刻显示出来,并把数据放在listview列表的最前面
    EditText限制输入长度和限定输入数字
    josn解析常见的几种方法
    bnu Robots on a grid
    hdu 1348 Wall
    hdu poj Oulipo
  • 原文地址:https://www.cnblogs.com/xushy/p/9004282.html
Copyright © 2020-2023  润新知