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.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" />
    
    <collection property="goods" column="id" ofType="Goods" select="selectGoodsForMarket"/>
resultMap>


<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中,定义了,它表明要从另外一个结果集selectGoodsForMarket取出的一个返回Goods集合list到Markets这个Javabean中的goods属性中。

其SQL执行顺序如下图:

Mybaits结果集之集合,Javabean中嵌套List的解决方案_第1张图片

也就是说先执行了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中的前六条?谁有好的办法?


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

你可能感兴趣的:(Mybaits结果集之集合,Javabean中嵌套List的解决方案)