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

相关文章

聊聊springboot中如何自定义消息转换器

《聊聊springboot中如何自定义消息转换器》SpringBoot通过HttpMessageConverter处理HTTP数据转换,支持多种媒体类型,接下来通过本文给大家介绍springboot中... 目录核心接口springboot默认提供的转换器如何自定义消息转换器Spring Boot 中的消息

Springboot项目构建时各种依赖详细介绍与依赖关系说明详解

《Springboot项目构建时各种依赖详细介绍与依赖关系说明详解》SpringBoot通过spring-boot-dependencies统一依赖版本管理,spring-boot-starter-w... 目录一、spring-boot-dependencies1.简介2. 内容概览3.核心内容结构4.

Spring Boot 整合 SSE(Server-Sent Events)实战案例(全网最全)

《SpringBoot整合SSE(Server-SentEvents)实战案例(全网最全)》本文通过实战案例讲解SpringBoot整合SSE技术,涵盖实现原理、代码配置、异常处理及前端交互,... 目录Spring Boot 整合 SSE(Server-Sent Events)1、简述SSE与其他技术的对

Spring Security 前后端分离场景下的会话并发管理

《SpringSecurity前后端分离场景下的会话并发管理》本文介绍了在前后端分离架构下实现SpringSecurity会话并发管理的问题,传统Web开发中只需简单配置sessionManage... 目录背景分析传统 web 开发中的 sessionManagement 入口ConcurrentSess

Java整合Protocol Buffers实现高效数据序列化实践

《Java整合ProtocolBuffers实现高效数据序列化实践》ProtocolBuffers是Google开发的一种语言中立、平台中立、可扩展的结构化数据序列化机制,类似于XML但更小、更快... 目录一、Protocol Buffers简介1.1 什么是Protocol Buffers1.2 Pro

C++ vector越界问题的完整解决方案

《C++vector越界问题的完整解决方案》在C++开发中,std::vector作为最常用的动态数组容器,其便捷性与性能优势使其成为处理可变长度数据的首选,然而,数组越界访问始终是威胁程序稳定性的... 目录引言一、vector越界的底层原理与危害1.1 越界访问的本质原因1.2 越界访问的实际危害二、基

Java实现本地缓存的四种方法实现与对比

《Java实现本地缓存的四种方法实现与对比》本地缓存的优点就是速度非常快,没有网络消耗,本地缓存比如caffine,guavacache这些都是比较常用的,下面我们来看看这四种缓存的具体实现吧... 目录1、HashMap2、Guava Cache3、Caffeine4、Encache本地缓存比如 caff

MyBatis-Plus 与 Spring Boot 集成原理实战示例

《MyBatis-Plus与SpringBoot集成原理实战示例》MyBatis-Plus通过自动配置与核心组件集成SpringBoot实现零配置,提供分页、逻辑删除等插件化功能,增强MyBa... 目录 一、MyBATis-Plus 简介 二、集成方式(Spring Boot)1. 引入依赖 三、核心机制

Java高效实现Word转PDF的完整指南

《Java高效实现Word转PDF的完整指南》这篇文章主要为大家详细介绍了如何用Spire.DocforJava库实现Word到PDF文档的快速转换,并解析其转换选项的灵活配置技巧,希望对大家有所帮助... 目录方法一:三步实现核心功能方法二:高级选项配置性能优化建议方法补充ASPose 实现方案Libre

springboot整合mqtt的步骤示例详解

《springboot整合mqtt的步骤示例详解》MQTT(MessageQueuingTelemetryTransport)是一种轻量级的消息传输协议,适用于物联网设备之间的通信,本文介绍Sprin... 目录1、引入依赖包2、yml配置3、创建配置4、自定义注解6、使用示例使用场景:mqtt可用于消息发