mybatis 多层级collection嵌套

json结构:

第一步查询(第一层查询,将第一层的id传递到第二层当条件查询 column=“id”):

<resultMap id="BaseResultMapClass" type="com.kingyon.common.models.shortcut.ShortcutKeyClassBean">
    <id column="id" jdbcType="INTEGER" property="id"/>
    <result column="title" jdbcType="VARCHAR" property="title"/>
    <result column="img" jdbcType="VARCHAR" property="img"/>
    <result column="bgColor" jdbcType="VARCHAR" property="bgColor"/>
    <result column="describe" jdbcType="VARCHAR" property="describe"/>
    <result column="type" jdbcType="BIGINT" property="type"/>
    <collection property="functionBeans" ofType="com.kingyon.common.models.shortcut.ShortcutKeyFunctionBean"
                select="com.kingyon.core.repository.ShortcutKeyMapper.getShortcutKeyFunction" column="id">

    </collection>
</resultMap>

<select id="findShortcutKeyByClassID" resultMap="BaseResultMapClass">
    select * from t_shortcut_key_class where id=#{classID} ;
</select>

第二步查询(第二层查询,并将第二层的id传递到第三层当条件查询 column=“funID” 是别名):

<resultMap id="baseShortcutKey" type="com.kingyon.common.models.shortcut.ShortcutKeyFunctionBean">
    <id column="id" jdbcType="INTEGER" property="funID"/>
    <result column="classID" jdbcType="VARCHAR" property="classID"/>
    <result column="describe" jdbcType="VARCHAR" property="describe"/>
    <result column="sort" jdbcType="VARCHAR" property="sort"/>
    <result column="className" jdbcType="VARCHAR" property="className"/>
    <collection property="shortcutKeyBean" ofType="com.kingyon.common.models.shortcut.ShortcutKeyBean"
                select="com.kingyon.core.repository.ShortcutKeyMapper.getShortcutKey" column="funID">

    </collection>
</resultMap>

<select id="getShortcutKeyFunction" resultMap="baseShortcutKey">
    select fun.id as funID,fun.classID,fun.describe,fun.sort from t_shortcut_key_function  as fun where fun.classID=#{classID} order by sort DESC ;
</select>

第三步查询(第三层查询):

<resultMap id="shortcutKeyBean" type="com.kingyon.common.models.shortcut.ShortcutKeyBean">
    <result property="id" column="keyID"/>
    <result property="shortcut" column="shortcut"/>
    <result property="details" column="details"/>
</resultMap>


<select id="getShortcutKey" resultMap="shortcutKeyBean">
      select * from t_shortcut_key where funID=#{id};
</select>

其实蛮好理解的,就是查询一个,然后把条件传递下步继续查询。。。 使用时调第一层,也就是从高到底。

最后附实体截图,其实看json就可以了:

第一层实体:

第二层实体:

第三层实体:

方式二(推荐)

<collection property="chapterChildBeans"
            ofType="com.xx.xx.mode.xx.xxBean">

    <id column="cId" property="id"/>
    <result column="cName" property="name"/>
</collection>
SELECT c.id,c.name,cc.id as cId,cc.name as cName from t\_chapter c,t\_chapter\_child cc WHERE c.id=cc.chapterID and c.subjectID=#{subjectID}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值