Mybaits结果集之集合,Javabean中嵌套List的解决方案

有类似这样的场景,我作为一个写作者来说,我写了很多篇文章,如果把我抽象成一个对象,那么该如何通过Mybatis 获取到我和我写的文章呢?这种情况下,使用Mybatis结果集的集合就可以满足需求。

在我的实际项目中,需要是要通过市场market获取对应的商品。

商品和市场的Javabean大致如下:

Markets.java

public class Markets extends DataEntity<Markets> {
    private Integer id;
    private String marketname;
    private String marketcode;
    private Integer market_weight;
    private List<Goods> goods;
    ......

Goods.java

public class Goods  extends DataEntity<Goods> {
    private Long id;
    private Integer market_id;
    private String serial_num;
    private String name;

在mybatis-config.xml中定义其别名如下:

<typeAlias alias='Markets' type='com.cmower.database.entity.Markets' />
<typeAlias alias='Goods' type='com.cmower.database.entity.Goods' />

在MarketMapper.xml中定义一个resultMap,其内容大致如下:

<resultMap type="Markets" id="BaseGoodMarketResultMap">
    <id column="id" property="id" />
    <result column="marketcode" property="marketcode" />
    <result column="marketname" property="marketname" />
    <result column="market_weight" property="market_weight" />
    <result column="market_type" property="market_type" />
    <!-- 这句特别重要,它的作用就是将selectGoodsForMarket取出的结果集映射到Markets这个Javabean中的goods属性 -->
    <collection property="goods" column="id" ofType="Goods" select="selectGoodsForMarket"/>
</resultMap>

<!-- 其中market_id=#{id}中的id为<collection>传递的column属性值 -->
<select id="selectGoodsForMarket" resultType="Goods">
  SELECT 
    g.id,
    g.name,
    g.price_str,
    g.goods_thumb 
FROM 
    ym_goods g 
where g.del_flag = 0 and g.market_id=#{id}
order by g.id DESC
limit 6
</select>

<select id="selectGoodMakets" resultMap="BaseGoodMarketResultMap">
    select 
        m.*
    from 
        market m 
    where 
        m.del_flag = 0 and m.market_type = 0
</select>

以上内容就是Mybaits结果集之集合的一种写法,在调用selectGoodMakets进行查询时,返回的结果集为BaseGoodMarketResultMap,在BaseGoodMarketResultMap中,定义了<collection property="goods" column="id" ofType="Goods" select="selectGoodsForMarket"/>,它表明要从另外一个结果集selectGoodsForMarket取出的一个返回Goods集合list到Markets这个Javabean中的goods属性中。

其SQL执行顺序如下图:

这里写图片描述

也就是说先执行了selectGoodMakets,然后再依次执行了三次selectGoodsForMarket。

这达到了我们期望的结果,但如果selectGoodMakets结果集有很多的话,selectGoodsForMarket就会执行很多次,有没有更好的方法,只执行一条SQL就获取到期望的结果集呢?

有到是有,但结果子结果无法进行limit,这并不是我想要的结果(Stack Overflow上也没有找到想要的答案),不过,就当是学习吧。

<resultMap type="Markets" id="BaseGoodMarketResultMap1">
    <id column="id" property="id" />
    <result column="marketcode" property="marketcode" />
    <result column="marketname" property="marketname" />
    <result column="market_weight" property="market_weight" />
    <result column="market_type" property="market_type" />

    <collection property="goods" ofType="Goods">
        <id column="good_id" property="id" />
        <result column="name" property="name" />
        <result column="price_str" property="price_str" />
        <result column="is_promote" property="is_promote" />
        <result column="issue_price" property="issue_price" />
        <result column="max_point" property="max_point" />
        <result column="back_point" property="back_point" />
        <result column="can_use_point" property="can_use_point" />
        <result column="goods_thumb" property="goods_thumb" />
    </collection>
</resultMap>

<select id="selectGoodMaketsOneSql" resultMap="BaseGoodMarketResultMap1">
    select
        g.id as good_id,
        g.name,
        g.price_str,
        g.is_promote,
        g.issue_price,
        g.max_point,
        g.back_point,
        g.can_use_point,
        g.goods_thumb,
        m.*
    from 
        market m 
        left join ym_goods g on m.id=g.market_id and g.del_flag = 0 and g.status =3 and g.is_onsale=1
    where 
        m.del_flag = 0 and m.market_type = 0
</select>

执行结果如下图:

这里写图片描述

是一条SQL了。但good表中没有limit,不知道这种情况,怎么取good中的前六条?谁有好的办法?


每天早起就是对人生的最大负责!

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沉默王二

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值