MyBatis嵌套查询column传多个参数描述

本文介绍了一个使用MyBatis实现关联映射的例子,展示了如何通过HashMap参数类型进行条件查询,包括如何使用if标签实现动态SQL。

代码如下,红色部分为关键代码。

 

注意parameterType要为java.util.HashMap

 

<resultMap id="baseResultMap" type="Blog">

  <association property="author" column="{id=author_id,likename=author_name}" javaType="Author" select="selectAuthor"/>

</resultMap>

 

 

<select id="selectAuthor" resultType="Author" parameterType="java.util.HashMap">

  SELECT * FROM AUTHOR WHERE 1=1

  <if test="id != null and id != '' ">

         and ID = #{id} 

  </if>

  <if test="likename != null and likename != '' ">

         and name like CONCAT('%',#{likename},'%')

  </if>

 <if test="author_id!= null and author_id!= '' ">

         and id=#{author_id}

  </if>

</select>

转载于:https://www.cnblogs.com/jimmy-muyuan/p/5459970.html

### MyBatis嵌套查询与嵌套集合的用法及区别 #### 一、嵌套查询 嵌套查询是指在一个 `resultMap` 的定义中,通过 `<association>` 或者 `<collection>` 标签中的 `select` 属性来指定另一个查询语句。这种方式会触发额外的一次数据库查询操作。 - **实现方式** 在 `resultMap` 定义中使用 `<association select="..." />` 或 `<collection select="...">...</collection>` 来执行子查询[^1]。例如: ```xml <resultMap type="com.example.MyBatis.dao.model.Person" id="personResultMap"> <id property="id" column="id"/> <result property="name" column="name"/> <!-- 关联查询 --> <association property="address" column="address_id" select="selectAddressById"/> </resultMap> <select id="selectPersonById" resultMap="personResultMap"> SELECT * FROM person WHERE id = #{id} </select> <select id="selectAddressById" resultType="com.example.MyBatis.dao.model.Address"> SELECT * FROM address WHERE id = #{id} </select> ``` - **特点** - 数据库会被多次访问,因为每次都需要单独发起子查询。 - 更适合用于复杂逻辑处理或者当父表和子表之间存在一对多关系时,且子表的数据量较小时[^4]。 --- #### 二、嵌套集合 嵌套集合则是指在同一个 SQL 查询中完成所有的数据获取工作,并利用 `JOIN` 将多个表的数据组合在一起。之后,在 `resultMap` 中通过 `<collection>` 和 `<association>` 映射这些联合查询的结果。 - **实现方式** 使用单条 SQL 进行联合查询并映射到复杂的对象结构中[^3]。例如: ```xml <resultMap type="com.example.MyBatis.dao.model.Course" id="courseWithSectionsAndLessons"> <id property="uuid" column="course_uuid"/> <result property="title" column="course_title"/> <!-- 节点集合 --> <collection property="sections" ofType="com.example.MyBatis.dao.model.Section"> <id property="uuid" column="section_uuid"/> <result property="name" column="section_name"/> <!-- 子节点集合 --> <collection property="lessons" ofType="com.example.MyBatis.dao.model.Lesson"> <id property="uuid" column="lesson_uuid"/> <result property="title" column="lesson_title"/> </collection> </collection> </resultMap> <select id="getCourseDetail" resultMap="courseWithSectionsAndLessons"> SELECT * FROM course c LEFT JOIN section s ON c.uuid = s.course_uuid LEFT JOIN lesson l ON s.uuid = l.section_uuid WHERE c.uuid = #{courseId} </select> ``` - **特点** - 只需一次数据库查询即可完成所有数据加载。 - 对于大数据量的情况可能更高效,但也可能导致 SQL 复杂度增加以及性能瓶颈(如笛卡尔积等问题)[^3]。 --- #### 三、两者的对比分析 | 特性 | 嵌套查询 | 嵌套集合 | |-------------------|----------------------------------|----------------------------------| | **数据库交互次数** | 多次 | 单次 | | **适用场景** | 父子表间一对一或多对一时,子表数据较少 | 当需要一次性获取大量父子关联数据时 | | **SQL 编写难度** | 较低 | 较高(涉及多表连接) | | **性能影响因素** | 如果子查询过多可能会降低效率 | 若返回记录数过大,则内存占用较高 | --- #### 四、总结 如果希望减少 SQL 的复杂性和提高可读性,可以选择嵌套查询;而如果追求更高的查询效率并且能够接受一定的开发成本提升,则可以考虑采用嵌套集合的方式[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值