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中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码

《Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码》:本文主要介绍Java中日期时间转换的多种方法,包括将Date转换为LocalD... 目录一、Date转LocalDateTime二、Date转LocalDate三、LocalDateTim

如何配置Spring Boot中的Jackson序列化

《如何配置SpringBoot中的Jackson序列化》在开发基于SpringBoot的应用程序时,Jackson是默认的JSON序列化和反序列化工具,本文将详细介绍如何在SpringBoot中配置... 目录配置Spring Boot中的Jackson序列化1. 为什么需要自定义Jackson配置?2.

Java中使用Hutool进行AES加密解密的方法举例

《Java中使用Hutool进行AES加密解密的方法举例》AES是一种对称加密,所谓对称加密就是加密与解密使用的秘钥是一个,下面:本文主要介绍Java中使用Hutool进行AES加密解密的相关资料... 目录前言一、Hutool简介与引入1.1 Hutool简介1.2 引入Hutool二、AES加密解密基础

Spring Boot项目部署命令java -jar的各种参数及作用详解

《SpringBoot项目部署命令java-jar的各种参数及作用详解》:本文主要介绍SpringBoot项目部署命令java-jar的各种参数及作用的相关资料,包括设置内存大小、垃圾回收... 目录前言一、基础命令结构二、常见的 Java 命令参数1. 设置内存大小2. 配置垃圾回收器3. 配置线程栈大小

SpringBoot实现微信小程序支付功能

《SpringBoot实现微信小程序支付功能》小程序支付功能已成为众多应用的核心需求之一,本文主要介绍了SpringBoot实现微信小程序支付功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录一、引言二、准备工作(一)微信支付商户平台配置(二)Spring Boot项目搭建(三)配置文件

解决SpringBoot启动报错:Failed to load property source from location 'classpath:/application.yml'

《解决SpringBoot启动报错:Failedtoloadpropertysourcefromlocationclasspath:/application.yml问题》这篇文章主要介绍... 目录在启动SpringBoot项目时报如下错误原因可能是1.yml中语法错误2.yml文件格式是GBK总结在启动S

Spring中配置ContextLoaderListener方式

《Spring中配置ContextLoaderListener方式》:本文主要介绍Spring中配置ContextLoaderListener方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录Spring中配置ContextLoaderLishttp://www.chinasem.cntene

jupyter代码块没有运行图标的解决方案

《jupyter代码块没有运行图标的解决方案》:本文主要介绍jupyter代码块没有运行图标的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录jupyter代码块没有运行图标的解决1.找到Jupyter notebook的系统配置文件2.这时候一般会搜索到

java实现延迟/超时/定时问题

《java实现延迟/超时/定时问题》:本文主要介绍java实现延迟/超时/定时问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java实现延迟/超时/定时java 每间隔5秒执行一次,一共执行5次然后结束scheduleAtFixedRate 和 schedu

Java Optional避免空指针异常的实现

《JavaOptional避免空指针异常的实现》空指针异常一直是困扰开发者的常见问题之一,本文主要介绍了JavaOptional避免空指针异常的实现,帮助开发者编写更健壮、可读性更高的代码,减少因... 目录一、Optional 概述二、Optional 的创建三、Optional 的常用方法四、Optio