mybatis之collection的用法

中文官方文档

权威的文档当然要看官方的文档说明

上面有一些面试题还有mybatis的各种问题
mybatis源码系列

 

场景:行业查询,一级行业,二级行业

1、POJO

@Getter
@Setter
public class XXXEntity implements Serializable {
    private Integer id;
    private String name;
    private Integer parentId;
    private Integer level;
    // 节点是否展开,true展开,false折叠
    private boolean open;
    private List<XXXEntity> second;
}

2、mapper

一对多的ResultMap

<resultMap id="SecondResultMap" type="com.xxx.xxx.entity.XXXEntit ">
    <id column="pkid" jdbcType="INTEGER" property="id"/>
    <result column="trade_tag" jdbcType="VARCHAR" property="name"/>
    <result column="parent_id" jdbcType="INTEGER" property="parentId"/>
    <result column="level" jdbcType="INTEGER" property="level"/>
    <collection property="seconds" ofType="com.xxx.xxx.entity.XXXEntity"
        column="pkid" select="getSecondList">
    </collection>
</resultMap>
<select id="getSecondList" resultMap="BaseResultMap" parameterType="java.lang.Integer">
    select
       <include refid="Base_Column_List"></include>
    from trade_info
    where level = 2 AND parent_id = #{pkid}
</select>

还有另一种写法,是将id,result在collection里面再重复一遍,个人不喜欢这种方式,觉得上面这种方式清晰明了

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值