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

简介: 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执行顺序如下图:

image.png



也就是说先执行了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>




相关文章
|
1月前
|
存储 安全 Java
java集合框架及其特点(List、Set、Queue、Map)
java集合框架及其特点(List、Set、Queue、Map)
|
3月前
|
Java 程序员
Java集合框架:List、Set、Map类型及泛型详解
Java集合框架:List、Set、Map类型及泛型详解
|
1月前
|
存储 安全 Java
Python教程第3章 | 集合(List列表、Tuple元组、Dict字典、Set)
Python 列表、无序列表、字典、元组增删改查基本用法和注意事项
50 1
|
1月前
|
存储 安全 Java
【Java】集合(一)单列集合List
【Java】集合(一)单列集合List
20 0
|
2月前
|
Java
Java对list集合元素进行排序的几种方式
Java对list集合元素进行排序的几种方式
22 0
|
2月前
|
存储 Java 索引
java list集合相关介绍和方法使用操作
java list集合相关介绍和方法使用操作
29 1
|
3月前
|
存储 Java 索引
从零开始学习 Java:简单易懂的入门指南之Collection集合及list集合(二十一)
从零开始学习 Java:简单易懂的入门指南之Collection集合及list集合(二十一)
|
3月前
|
Java
java 读取csv到list,再将list集合数据写入新的csv文件中
java 读取csv到list,再将list集合数据写入新的csv文件中
|
3月前
|
存储 安全 算法
【深入探究Java集合框架】从List到Map的完整指南
【深入探究Java集合框架】从List到Map的完整指南
|
4月前
|
存储 SQL 关系型数据库
Mysql鸡础(从数据库中导入学生数据用list集合存储emp成员)
Mysql鸡础(从数据库中导入学生数据用list集合存储emp成员)