`
yaozuodaoforfly
  • 浏览: 119390 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

mybatis学习笔记-高级嵌套

 
阅读更多
Attribute Description
column The column name from the database, or the aliased column label that holds the value that will be passed to the nested statement as an input parameter. This is the same string that would normally be passed to resultSet.getString(columnName). Note: To deal with composite keys, you can specify multiple column names to pass to the nested select statement by using the syntaxcolumn="{prop1=col1,prop2=col2}". This will cause prop1 and prop2 to be set against the parameter object for the target nested select statement.
select The ID of another mapped statement that will load the complex type required by this property mapping. The values retrieved from columns specified in the column attribute will be passed to the target select statement as parameters. A detailed example follows this table. Note: To deal with composite keys, you can specify multiple column names to pass to the nested select statement by using the syntax column="{prop1=col1,prop2=col2}". This will cause prop1 and prop2 to be set against the parameter object for the target nested select statement.
fetchType Optional. Valid values are lazy and eager. If present, it supersedes the global configuration parameter lazyLoadingEnabled for this mapping.

Attribute Description
column The column name from the database, or the aliased column label that holds the value that will be passed to the nested statement as an input parameter. This is the same string that would normally be passed to resultSet.getString(columnName). Note: To deal with composite keys, you can specify multiple column names to pass to the nested select statement by using the syntax column="{prop1=col1,prop2=col2}". This will cause prop1 and prop2 to be set against the parameter object for the target nested select statement.
select The ID of another mapped statement that will load the complex type required by this property mapping. The values retrieved from columns specified in the column attribute will be passed to the target select statement as parameters. A detailed example follows this table. Note: To deal with composite keys, you can specify multiple column names to pass to the nested select statement by using the syntax column="{prop1=col1,prop2=col2}". This will cause prop1 and prop2 to be set against the parameter object for the target nested select statement.
fetchType Optional. Valid values are lazy and eager. If present, it supersedes the global configuration parameter lazyLoadingEnabled for this mapping.

 

 

上面的嵌套不适合大数据量的查询,它会导致性能下降(因为N+1问题)

 

那么对于大数据量的查询,用下面的嵌套

Nested Results for Association

Attribute Description
resultMap This is the ID of a ResultMap that can map the nested results of this association into an appropriate object graph. This is an alternative to using a call to another select statement. It allows you to join multiple tables together into a single ResultSet. Such a ResultSet will contain duplicated, repeating groups of data that needs to be decomposed and mapped properly to a nested object graph. To facilitate this, MyBatis lets you "chain" result maps together, to deal with the nested results. An example will be far easier to follow, and one follows this table.
columnPrefix When joining multiple tables, you would have to use column alias to avoid duplicated column names in the ResultSet. Specifying columnPrefix allows you to map such columns to an external resultMap. Please see the example explained later in this section.
notNullColumn By default a child object is created only if at least one of the columns mapped to the child's properties is non null. With this attribute you can change this behaviour by specifiying which columns must have a value so MyBatis will create a child object only if any of those columns is not null. Multiple column names can be specified using a comma as a separator. Default value: unset.
autoMapping If present, MyBatis will enable or disable automapping when mapping the result to this property. This attribute overrides the global autoMappingBehavior. Note that it has no effect on an external resultMap, so it is pointless to use it with select or resultMap attribute. Default value: unset.

You've already seen a very complicated example of nested associations above. The following is a far simpler example to demonstrate how this works. Instead of executing a separate statement, we'll join the Blog and Author tables together, like so:

 

<resultMap id="blogResult" type="Blog">
  <collection property="posts" javaType="ArrayList" column="id" ofType="Post" select="selectPostsForBlog"/>
</resultMap>

这里的ofType代表的是集合里存入的类型
  • 大小: 4.4 KB
分享到:
评论

相关推荐

    MyBatis学习笔记.zip

    在"MyBatis学习笔记.zip"这个压缩包中,包含了四天的学习内容,分别以"MyBatis_day1"、"MyBatis_day2"、"MyBatis_day3"和"MyBatis_day4"命名,这些笔记可能详细阐述了MyBatis的核心概念和使用方法。让我们逐一解析...

    mybatis-study-master.zip

    本压缩包"mybatis-study-master.zip"包含了狂神老师关于MyBatis的全套学习资料,包括代码示例和笔记,非常适合希望提升MyBatis技术的开发者进行深入学习。 MyBatis 的核心概念主要有以下几个方面: 1. **XML或注解...

    MyBatis 官方笔记 + MyBatis 3.5.10 官方英文文档

    **MyBatis 知识点概述** MyBatis 是一个优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数...结合官方文档和笔记学习,将有助于深入理解和运用这一工具。

    mybatis学习笔记

    ### MyBatis学习笔记知识点概览 #### 1. MyBatis入门 ##### 1.1 单独使用JDBC编程问题总结 - **JDBC程序示例**:通过一个具体的示例展示了如何使用JDBC连接数据库并执行SQL查询。 - 加载数据库驱动 - 获取数据库...

    【MyBatis学习笔记二】——MyBatis配置说明.zip

    本学习笔记主要围绕MyBatis的核心配置进行深入讲解。 首先,MyBatis的配置文件是整个框架的入口,通常命名为`mybatis-config.xml`。这个文件包含了MyBatis的基本设置,如数据源、事务管理器、环境配置等。在配置...

    mybatis笔记

    MyBatis是一个优秀的Java持久层框架,它支持定制化SQL、存储过程以及高级映射。MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。MyBatis可以使用简单的XML或注解进行配置和原始映射,将接口和Java的...

    mybatis-day01

    通过这个“mybatis-day01”的学习,你将掌握MyBatis的基础使用,为后续深入学习和实际项目开发打下坚实基础。继续探索MyBatis的高级特性和实战应用,你将能更好地驾驭数据库操作,提升开发效率。

    mybatis-learn:mybatis框架源码学习

    在"mybatis-learn-master"这个压缩包中,可能包含了MyBatis的学习示例、源码分析笔记、甚至是框架的源代码。通过这些资料,你可以了解到如何创建Mapper接口和XML文件,如何配置MyBatis,以及如何进行SQL查询。同时,...

    Spring+MVC+MYBatis企业应用实战

    MyBatis则是一个优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射,避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。 在Spring框架中,核心概念包括IoC(Inversion of Control,控制反转)和AOP。...

    基于Java、MySQL和MyBatis实现学生管理、课程管理和成绩管理系统源码(数据库实验)+项目说明.zip

    更多mybatis学习请看:[mybatis学习笔记](mybatis.pdf) 应用预览 其实就是一个while循环里嵌套一个switch,实现一个命令行菜单. 实现功能: **学生类** 1.新生入学信息增加 2.学生信息删除 3.查询所有学生信息 4....

    mybatis资源,用于自学

    8. **课件&代码**:这部分资源可能是教学视频、PPT、笔记或示例代码,它们可以帮助你深入理解 MyBatis 的工作原理,通过实际操作加深对框架的理解。 自学 MyBatis 需要理解这些基本概念,并通过实践项目来巩固。在...

    达内云笔记代码

    【达内云笔记代码】是针对Java开发框架SpringMVC和MyBatis的学习资源集合,这些笔记涵盖了多个学习阶段,包括了从基础到进阶的知识点。以下将详细阐述SpringMVC和MyBatis这两个核心框架的主要内容以及它们在实际开发...

    自己整理的mp笔记自己整理的mp笔记

    在给定的信息中,我们可以看到这是一份关于MyBatis框架的个人学习笔记,主要集中在查询操作上,即“mp查询”。MyBatis是一个优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射,避免了几乎所有的JDBC代码和...

    java,html,jquery,js各种报错解决方案

    8. **Spring+MyBatis+SpringMVC**:这是常见的Java后端开发框架组合,报错可能涉及配置、注入、数据库连接等。通过阅读官方文档,了解这些框架的工作原理和最佳实践,可以更有效地排查问题。 9. **MySQL**:MySQL是...

Global site tag (gtag.js) - Google Analytics