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

简介: 版权声明:欢迎转载,请注明沉默王二原创。 https://blog.csdn.net/qing_gee/article/details/79214721 有类似这样的场景,我作为一个写作者来说,我写了很多篇文章,如果把我抽象成一个对象,那么该如何通过Mybatis 获取到我和我写的文章呢?这种情况下,使用Mybatis结果集的集合就可以满足需求。
版权声明:欢迎转载,请注明沉默王二原创。 https://blog.csdn.net/qing_gee/article/details/79214721

有类似这样的场景,我作为一个写作者来说,我写了很多篇文章,如果把我抽象成一个对象,那么该如何通过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中的前六条?谁有好的办法?


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

相关文章
|
22天前
|
存储 安全 Java
【Java集合类面试二十五】、有哪些线程安全的List?
线程安全的List包括Vector、Collections.SynchronizedList和CopyOnWriteArrayList,其中CopyOnWriteArrayList通过复制底层数组实现写操作,提供了最优的线程安全性能。
|
21天前
|
安全
List集合特有功能
List集合特有功能
25 2
|
22天前
|
Java
【Java集合类面试二十三】、List和Set有什么区别?
List和Set的主要区别在于List是一个有序且允许元素重复的集合,而Set是一个无序且元素不重复的集合。
|
28天前
|
存储 Java
Java学习笔记 List集合的定义、集合的遍历、迭代器的使用
Java学习笔记 List集合的定义、集合的遍历、迭代器的使用
|
29天前
|
存储 安全 Java
java集合框架复习----(2)List
这篇文章是关于Java集合框架中List集合的详细复习,包括List的特点、常用方法、迭代器的使用,以及ArrayList、Vector和LinkedList三种实现类的比较和泛型在Java中的使用示例。
java集合框架复习----(2)List
|
29天前
|
存储 安全 Java
java集合框架复习----(4)Map、List、set
这篇文章是Java集合框架的复习总结,重点介绍了Map集合的特点和HashMap的使用,以及Collections工具类的使用示例,同时回顾了List、Set和Map集合的概念和特点,以及Collection工具类的作用。
java集合框架复习----(4)Map、List、set
|
13天前
|
Java
用JAVA架建List集合为树形结构的代码方法
这段代码定义了一个表示树形结构的 `Node` 类和一个用于构建树形结构的 `TreeController`。`Node` 类包含基本属性如 `id`、`pid`、`name` 和 `type`,以及子节点列表 `children`。`TreeController` 包含初始化节点列表并将其转换为树形结构的方法。通过过滤和分组操作实现树形结构的构建。详情可见:[代码示例链接1](http://www.zidongmutanji.com/zsjx/43551.html),[代码效果参考链接2](https://www.257342.com/sitemap/post.html)。
25 5
|
24天前
|
存储 NoSQL 算法
Redis6入门到实战------ 三、常用五大数据类型(列表(List)、集合(Set)、哈希(Hash)、Zset(sorted set))
这是关于Redis 6入门到实战的文章,具体内容涉及Redis的五大数据类型:列表(List)、集合(Set)、哈希(Hash)、有序集合(Zset(sorted set))。文章详细介绍了这些数据类型的特点、常用命令以及它们背后的数据结构。如果您有任何关于Redis的具体问题或需要进一步的帮助,请随时告诉我。
List集合的实现类
List集合的实现类
|
1月前
|
索引