最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
mybatis父查询参数传递到子查询的使用过程
时间:2026-06-11 08:33:01 编辑:袖梨 来源:一聚教程网
需求
数据库表是按照日期进行分表,查找数据时候,不同日期的数据到不同的表中查找,此时日期作为参数传递。

在有父、子查询的SQL语句时,希望子查询能够使用到父查询中的参数(如日期)。
实现
将参数作为数据库表返回值的某一列,进行传递。此时,可以在resultMap中获取到。
示例如下:
- Dao层
List <PicMoreDetailVo> selectPicList(@Param("tableName") String tableName, @Param("picVo") PicManagerVo picManagerVo);
- 父查询
<select id="selectPicList" resultMap="bagIdFromMonthTable">
SELECT id,create_time,'${tableName}' as tableName FROM baginfo_${tableName} WHERE stationId = #{picVo.stationId} AND create_time BETWEEN #{picVo.startTime} AND #{picVo.endTime}
ORDER BY create_time ${picVo.sort} LIMIT #{picVo.currentPageNo},#{picVo.pageSize}
</select>
- 父查询返回map
<resultMap type="com.qxmz.vo.pic.PicMoreDetailVo" id="bagIdFromMonthTable">
<id property="bagId" column="id"/>
<id property="createTime" column="create_time"/>
<collection property="picList" ofType="com.qxmz.vo.pic.PicMoreReqVo" select="selectPicListByBagIdFromMonthTable"
column="{id=id,tableName=tableName}">
</collection>
</resultMap>
- 子查询
<select id="selectPicListByBagIdFromMonthTable" resultMap="picId">
SELECT id,pic_url,isMainPic FROM baginfo_pic_${tableName} where bagInfo_id=#{id}
</select>
- 子查询返回map
<resultMap type="com.qxmz.vo.pic.PicMoreReqVo" id="picId">
<id column="id" property="picId"/>
<result column="isMainPic" property="isMainPic"/>
<result column="pic_url" property="picUrl"/>
</resultMap>
另类场景
在父子查询参数传递过程中,除上面String类型外,如果有list这种复杂类型参数,可以将list转为String类型进行传递,类似"a,b,c",然后再在子查询中转为list使用。
由于本人从没遇到过这种场景,下文仅仅作为演示。另外,对于这种场景,也可以考虑从业务上解决。
- Controller层
@GetMapping("/select/test")
@ApiOperation(value = "select test", notes = "select test", httpMethod = "GET")
public Object judgeStationList() {
try {
List<String> timeList = new ArrayList<>();
timeList.add("2021-06-09 10:46:16");
//timeList.add("2021-06-09 10:46:17");
String timeListStr = StringUtils.strip(timeList.toString(), "[]");
return bagInfoDao.parentSelect(timeListStr);
} catch (Exception e) {
logger.error("异常:{}", e);
return "error";
}
}
- Dao层
List<BagInfo> parentSelect(String timeListStr);
- 父查询
<select id="parentSelect" resultMap="testListMap">
select id,create_time,'${timeListStr}' as childTimeListStr from baginfo_2021_06
<where>
<if test="timeListStr!=null and timeListStr!='' ">
and create_time in
<foreach item="time" collection="timeListStr.split(',')" open="(" separator="," close=")">
#{time}
</foreach>
</if>
</where>
</select>
- 父查询返回Map
<resultMap type="com.qxmz.vo.pic.PicMoreDetailVo" id="testListMap">
<id property="bagId" column="id"/>
<id property="createTime" column="create_time"/>
<collection property="picList" ofType="com.qxmz.vo.pic.PicMoreReqVo" select="childSelect"
column="{id=id,childTimeListStr=childTimeListStr}">
</collection>
</resultMap>
- 子查询
<select id="childSelect" resultType="com.qxmz.vo.pic.PicMoreReqVo">
select id picId,isMainPic,pic_url picUrl from baginfo_pic_2021_06
<where>
bagInfo_id=#{id}
<if test="childTimeListStr!=null and childTimeListStr!='' ">
and create_time in
<foreach item="time" collection="childTimeListStr.split(',')" open="(" separator="," close=")">
#{time}
</foreach>
</if>
</where>
</select>
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持本站。
您可能感兴趣的文章:- 使用MyBatis XML和QueryWrapper实现动态查询
- 解决Mybatis一对多/多对多查询时只查出了一条数据问题
- mybatis-plus判断参数是否为空并作为查询条件方式
- MyBatis-Flex多表关联查询指南分享
- MyBatis-plus聚合查询、表关联、条件更新实践
- MyBatisPlus之高级查询用法及说明
- Mybatis实现批量删除和多条件查询方式
相关文章
- eui插件团队框架调整教程 06-11
- 蚂蚁庄园今日答案:2026年6月11日最新 06-11
- 2025今日小鸡最新答案6月11日 支付宝小鸡最新答案2026年6月11日 06-11
- 蚂蚁庄园相似相溶最新答案 6月11日蚂蚁庄园答案 06-11
- Claude Code最佳实践2026版:如何避开5个常见错误? 06-11
- 蚂蚁庄园今日答案 橡皮容易黏塑料尺子 主要与以下哪种原理相关 06-11