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

2024-03-05 06:10

本文主要是介绍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">selectg.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=1where m.del_flag = 0 and m.market_type = 0
</select>

执行结果如下图:

这里写图片描述

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


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

这篇关于Mybaits结果集之集合,Javabean中嵌套List的解决方案的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/775541

相关文章

Java中的.close()举例详解

《Java中的.close()举例详解》.close()方法只适用于通过window.open()打开的弹出窗口,对于浏览器的主窗口,如果没有得到用户允许是不能关闭的,:本文主要介绍Java中的.... 目录当你遇到以下三种情况时,一定要记得使用 .close():用法作用举例如何判断代码中的 input

Redis中的有序集合zset从使用到原理分析

《Redis中的有序集合zset从使用到原理分析》Redis有序集合(zset)是字符串与分值的有序映射,通过跳跃表和哈希表结合实现高效有序性管理,适用于排行榜、延迟队列等场景,其时间复杂度低,内存占... 目录开篇:排行榜背后的秘密一、zset的基本使用1.1 常用命令1.2 Java客户端示例二、zse

Spring Gateway动态路由实现方案

《SpringGateway动态路由实现方案》本文主要介绍了SpringGateway动态路由实现方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录前沿何为路由RouteDefinitionRouteLocator工作流程动态路由实现尾巴前沿S

JavaScript对象转数组的三种方法实现

《JavaScript对象转数组的三种方法实现》本文介绍了在JavaScript中将对象转换为数组的三种实用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友... 目录方法1:使用Object.keys()和Array.map()方法2:使用Object.entr

idea+spring boot创建项目的搭建全过程

《idea+springboot创建项目的搭建全过程》SpringBoot是Spring社区发布的一个开源项目,旨在帮助开发者快速并且更简单的构建项目,:本文主要介绍idea+springb... 目录一.idea四种搭建方式1.Javaidea命名规范2JavaWebTomcat的安装一.明确tomcat

Java高效实现PowerPoint转PDF的示例详解

《Java高效实现PowerPoint转PDF的示例详解》在日常开发或办公场景中,经常需要将PowerPoint演示文稿(PPT/PPTX)转换为PDF,本文将介绍从基础转换到高级设置的多种用法,大家... 目录为什么要将 PowerPoint 转换为 PDF安装 Spire.Presentation fo

Java集合之Iterator迭代器实现代码解析

《Java集合之Iterator迭代器实现代码解析》迭代器Iterator是Java集合框架中的一个核心接口,位于java.util包下,它定义了一种标准的元素访问机制,为各种集合类型提供了一种统一的... 目录一、什么是Iterator二、Iterator的核心方法三、基本使用示例四、Iterator的工

SpringBoot中ResponseEntity的使用方法举例详解

《SpringBoot中ResponseEntity的使用方法举例详解》ResponseEntity是Spring的一个用于表示HTTP响应的全功能对象,它可以包含响应的状态码、头信息及响应体内容,下... 目录一、ResponseEntity概述基本特点:二、ResponseEntity的基本用法1. 创

springboot依靠security实现digest认证的实践

《springboot依靠security实现digest认证的实践》HTTP摘要认证通过加密参数(如nonce、response)验证身份,避免明文传输,但存在密码存储风险,相比基本认证更安全,却因... 目录概述参数Demopom.XML依赖Digest1Application.JavaMyPasswo

java中判断json key是否存在的几种方法

《java中判断jsonkey是否存在的几种方法》在使用Java处理JSON数据时,如何判断某一个key是否存在?本文就来介绍三种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目http://www.chinasem.cn录第一种方法是使用 jsONObject 的 has 方法