mybatis 连表查询,子查询 collection,返回 vo

该博客详细介绍了如何使用MyBatis进行连表查询和子查询,以获取复杂的数据视图对象AppsVo。AppsService.xml中定义了主查询与子查询的ResultMap,包括Apps实体类与AppsVo视图类的映射。AppsVo包含了分类名称、分类层级名称以及应用分组集合和文章名称列表。同时,展示了AppsGroupMapper的子查询方法,用于获取应用的分组对象列表。主查询实现了分页功能,并支持多种查询条件,如应用名、类型、平台等。

1. 连表查询,子查询 collection,返回 vo

AppsService.xml 主查询

	<resultMap id="appsMap" type="com.boyia.magic.lxt.api.entity.Apps">
        <id property="id" column="id"/>
        <result property="appName" column="app_name"/>
        <result property="appType" column="app_type"/>
        <result property="appIcon" column="app_icon"/>
        <result property="appPlatform" column="app_platform"/>
        <result property="appCategoryId" column="app_category_id"/>
        <result property="appLanguage" column="app_language"/>
        <result property="appBrief" column="app_brief"/>
        <result property="appKey" column="app_key"/>
        <result property="appSecret" column="app_secret"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="status" column="status"/>
        <result property="delFlag" column="del_flag"/>
  </resultMap>
  
	<resultMap id="appVoMap" type="com.boyia.magic.lxt.api.vo.AppsVo">
		<id property="id" column="id"/>
		<result property="appName" column="app_name"/>
		<result property="appType" column="app_type"/>
		<result property="appIcon" column="app_icon"/>
		<result property="appPlatform" column="app_platform"/>
		<result property="appCategoryId" column="app_category_id"/>
		<result property="appLanguage" column="app_language"/>
		<result property="appBrief" column="app_brief"/>
		<result property="appKey" column="app_key"/>
		<result property="appSecret" column="app_secret"/>
		<result property="createBy" column="create_by"/>
		<result property="createTime" column="create_time"/>
		<result property="updateBy" column="update_by"/>
		<result property="updateTime" column="update_time"/>
		<result property="status" column="status"/>
		<result property="delFlag" column="del_flag"/>
		<!--分类名称-->
		<result property="categoryName" column="category_name"/>
		<!--分类层级名称-->
		<result property="categoryNames" column="category_names"/>
		<!--应用分组集合-->
		<collection property="groupList" ofType="com.boyia.magic.lxt.api.dto.AppGroupForListNameDto"
					select="getByAppsId" column="categoryId = id">
					<result property="articleId" column="id"/>
					<result property="articleName" column="name"/>
		</collection>

		<collection property="articleNameList" ofType="javax.long.String"
					select="getByAppsId" column="categoryId = id">
					<result property="articleName" column="name"/>
		</collection>
	</resultMap>

<select id="getByAppsId">
	SELECT
		name,
		sex
	WHERE
		article.category_id = #{categoryId}
</select>

	<!--分页查询-->
	<select id="pageByQuery" resultMap="appVoMap" resultType="com.boyia.magic.lxt.api.vo.AppsVo">
		SELECT
		  t1.id,
			t1.app_name,
			t1.app_type,
			t1.app_icon,
			t1.app_platform,
			t1.app_category_id,
			t1.app_language,
			t1.app_brief,
			t1.app_key,
			t1.app_secret,
			t1.create_by,
			t1.create_time,
			t1.update_by,
			t1.update_time,
			t1.status,
			t2.category_name,
			t2.category_names
		FROM
			lxt_platform_apps AS t1
			LEFT JOIN lxt_platform_apps_category AS t2 ON t1.app_category_id = t2.id
		WHERE
			t1.del_flag = '0'
			AND t2.del_flag = '0'
		<if test="query.appName != null and query.appName != ''">
			AND t1.app_name LIKE CONCAT(CONCAT('%', #{query.appName}), '%')
		</if>
		<if test="query.appType != null and query.appType != ''">
			AND t1.app_type = #{query.appType}
		</if>
		<if test="query.appPlatform != null and query.appPlatform != ''">
			AND t1.app_platform = #{query.appPlatform}
		</if>
		<if test="query.appCategoryId != null">
			AND t1.app_category_id = #{query.appCategoryId}
		  	OR t1.app_category_id IN ( SELECT id FROM lxt_platform_apps_category WHERE parent_ids LIKE CONCAT(CONCAT('%,',#{query.appCategoryId}),',%') )
		</if>
		<if test="query.groupId != null">
			AND t1.id IN ( SELECT apps_id FROM lxt_platform_apps_group_relation WHERE group_id = #{query.groupId} )
		</if>
		ORDER BY
			t1.create_time
		DESC
	</select>

AppsGroupMapper.java 子查询

@Mapper
public interface AppsGroupMapper extends MagicBaseMapper<AppsGroup> {

	/**
	 * 根据应用id查询分组对象列表
	 *
	 * @param id 应用id
	 * @return 应用分组对象列表
	 */
	List<AppGroupForListNameDto> getByAppsId(Long id);
}

AppsGroupMapper.xml 子查询

<mapper namespace="com.boyia.magic.lxt.mapper.AppsGroupMapper">

  <resultMap id="appsGroupMap" type="com.boyia.magic.lxt.api.entity.AppsGroup">
        <id property="id" column="id"/>
        <result property="groupName" column="group_name"/>
        <result property="rankWeight" column="rank_weight"/>
        <result property="remark" column="remark"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="status" column="status"/>
        <result property="delFlag" column="del_flag"/>
  </resultMap>

	<!--根据应用id查询分组对象列表-->
	<select id="getByAppsId" resultType="com.boyia.magic.lxt.api.dto.AppGroupForListNameDto">
		SELECT
			id,
			group_name
		FROM
			lxt_platform_apps_group
		WHERE
			id IN ( SELECT group_id FROM lxt_platform_apps_group_relation WHERE apps_id = #{id})
	</select>
</mapper>
MyBatis 中 resultMap 是一个强大的工具,用于处理多查询时结果集的映射。resultMap 的子元素包括:id 一般对应数据库中该行的主键 id,设置此项可提高 MyBatis 性能;result 映射到 JavaBean 的某个“简单类型”属性;association 映射到 JavaBean 的某个“复杂类型”属性,比如 JavaBean 类;collection 映射到 JavaBean 的某个“复杂类型”属性,比如集合 [^2]。 以下是两个配置示例: ### 一对多查询示例 ```xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.stock1Server.mapper.UserMapper"> <resultMap id="getUserMap" type="com.stock1Server.pojo.vo.UserVO"> <result property="id" column="id"/> <result property="userName" column="user_name"/> <result property="userCode" column="user_code"/> <collection property="workIds" javaType="ArrayList" ofType="com.stock1Server.entity.WorktableEntity"> <result property="i1" column="i1"/> <result property="i2" column="i2"/> <result property="i3" column="i3"/> <result property="i4" column="i4"/> </collection> </resultMap> <select id="getUser" resultMap="getUserMap"> select user.*, worktable.* from user left join worktable on worktable.user_id = user.id </select> </mapper> ``` 在这个示例中,定义了一个 resultMap 名为 `getUserMap`,它将查询结果映射到 `com.stock1Server.pojo.vo.UserVO` 类。其中 `collection` 元素用于处理一对多的关系,将查询到的 `worktable` 的记录映射到 `UserVO` 类的 `workIds` 属性中 [^1]。 ### 关联查询示例 ```xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.zwk.domain.UserMapper"> <resultMap id="UserResult" type="User"> <id property="id" column="id"/> <result property="password" column="password"/> <association property="userInfo" column="info_id" select="getUserInfoById"/> </resultMap> <select id="getUserInfoById" resultType="UserInfo"> select * from test_userinfo where id=#{id} </select> <select id="findUserById" parameterType="String" resultMap="UserResult"> select * from test_userinfo i,test_user u where i.id=u.id and u.id=#{id} </select> </mapper> ``` 此示例中,`resultMap` 名为 `UserResult`,将查询结果映射到 `User` 类。`association` 元素用于处理一对一的关联关系,通过 `select` 属性指定一个子查询来获取关联的 `UserInfo` 对象 [^5]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Whitemeen太白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值