级联(数据字典)

二级级联:

一:新建两个Bean

父级:

/**
 * @Description 数据字典
 * @Author WangKun
 * @Date 2023/7/25 10:15
 * @Version
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("HW_DICT_KEY")
public class DictKey implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private String id;

    private Timestamp created;

    private Timestamp modified;

    private String dictKey;

    private String dictName;

    private List<DictItemValue> dictItemValues;
}

子级:

/**
 * @Description 数据字段
 * @Author WangKun
 * @Date 2023/7/25 10:15
 * @Version
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("HW_DICT_ITEM_VALUE")
public class DictItemValue implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private String id;

    private Timestamp created;

    private Timestamp modified;

    private String dictKey;

    private String itemCode;

    private String itemName;

}

mybatis 或者 mybatisplus

<mapper namespace="com.module.dict.mapper.DictMapper">

    <resultMap id="BaseResultMap" type="com.module.dict.bean.DictKey">
            <result column="dictKey" property="dictKey" jdbcType="VARCHAR"/>
            <result column="dictName" property="dictName" jdbcType="VARCHAR"/>
        <collection property="dictItemValues" ofType="com.module.dict.bean.DictItemValue">
            <result column="dictKey" property="dictKey" jdbcType="VARCHAR"/>
            <result column="itemName" property="itemName" jdbcType="VARCHAR"/>
            <result column="itemCode" property="itemCode" jdbcType="VARCHAR"/>
        </collection>
    </resultMap>

    <select id="queryAllDict" resultMap="BaseResultMap">
        SELECT
            t1.id,
            t1.dict_name AS 'dictName',
            t1.dict_key AS 'dictKey',
            t2.item_name AS 'itemName',
            t2.item_code AS 'itemCode'
        FROM
            hw_dict_key t1
                JOIN hw_dict_item_value t2 ON t2.dict_key = t1.dict_key
    </select>

</mapper>

三级级联:

新建三个Bean

父级:

/**
 * @Description 数据字典
 * @Author WangKun
 * @Date 2023/7/25 10:15
 * @Version
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("HW_DICT")
public class Dict implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private String id;

    private String key;

    private String name;

    private String parentId;

    private List<DictValue> children;
}

子级:

/**
 * @Description 数据字典
 * @Author WangKun
 * @Date 2023/7/25 10:15
 * @Version
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("HW_DICT_VALUE")
public class DictValue implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private String id;

    private String key;

    private String name;

    private String parentId;

    private List<DictValueItem> children;
}

孙级:

/**
 * @Description 数据字典
 * @Author WangKun
 * @Date 2023/7/25 10:15
 * @Version
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("HW_DICT_VALUE_ITEM")
public class DictValueItem implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private String id;

    private String key;

    private String name;

    private String parentId;
}

mybatis 或者 mybatisplus

<mapper namespace="com.module.dict.mapper.DictMapper">

    <resultMap id="BaseResultMap" type="com.module.dict.bean.Dict">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <result column="key" property="key"></result>
        <collection property="children" ofType="com.module.dict.bean.DictValue">
            <id column="value_id" property="id"></id>
            <result column="value_name" property="name"></result>
            <result column="value_key" property="key"></result>
            <collection property="children" ofType="com.module.dict.bean.DictValueItem">
                <id column="item_id" property="id"></id>
                <result column="item_name" property="name"></result>
                <result column="item_key" property="key"></result>
            </collection>
        </collection>

    </resultMap>
    <select id="queryAllDict" resultMap="BaseResultMap">
        select
            t1.id,
            t1.name,
            t1.key,
            t2.id value_id,
            t2.name value_name,
            t2.key value_key,
            t3.id item_id,
            t3.name item_name,
            t3.key item_key
        from hw_dict t1
                 join hw_dict_value t2 on t2.parent_id = t1.id
                 join hw_dict_value_item t3 on t3.parent_id = t2.id
--         where p.parent_id = '0'
    </select>


</mapper>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值