MyBatis查询嵌套对象
来源:互联网 发布:在校学生网络兼职 编辑:程序博客网 时间:2024/05/16 01:16
前言
MyBatis支持查询时查询查出非基本类型的对象。
The association element deals with a “has-one” type relationship. For example, in our example, a Blog has one Author. An association mapping works mostly like any other result.
三种方式
Nested Select for Association
- 当一个select查询结束之后,再执行一次select,通过第二次的查询给Association赋值。
- 在association节点里使用column和select属性实现。
- 当需要传递多个参数给查询字句时,可以使用如下方式
column="{prop1=col1,prop2=col2}"
代码如下:
<resultMap id="blogResult" type="Blog"> <association property="author" column="author_id" javaType="Author" select="selectAuthor"/></resultMap><select id="selectBlog" resultMap="blogResult"> SELECT * FROM BLOG WHERE ID = #{id}</select><select id="selectAuthor" resultType="Author"> SELECT * FROM AUTHOR WHERE ID = #{id}</select>
Nested Results for Association
查询的时候是联合了多张表一起查询,在association节点里使用resultMap属性配置给如何给对象属性映射。
代码如下:
<select id="selectBlog" resultMap="blogResult"> select B.id as blog_id, B.title as blog_title, B.author_id as blog_author_id, A.id as author_id, A.username as author_username, A.password as author_password, A.email as author_email, A.bio as author_bio from Blog B left outer join Author A on B.author_id = A.id where B.id = #{id}</select><resultMap id="blogResult" type="Blog"> <id property="id" column="blog_id" /> <result property="title" column="blog_title"/> <association property="author" resultMap="authorResult" /></resultMap><resultMap id="authorResult" type="Author"> <id property="id" column="author_id"/> <result property="username" column="author_username"/> <result property="password" column="author_password"/> <result property="email" column="author_email"/> <result property="bio" column="author_bio"/></resultMap>
columnPrefix
association标签里包含columnPrefix这个属性,当使用方式二时,可以配合columnPrefix属性,在映射时,给resultMap里column对应的值添加前缀再去匹配。
<resultMap id="blogResult" type="Blog"> <id property="id" column="blog_id" /> <result property="title" column="blog_title"/> <association property="author" resultMap="authorResult" /> <association property="coAuthor" resultMap="authorResult" columnPrefix="co_" /></resultMap>
Multiple ResultSets for Association(不建议使用)
一次访问数据库,执行两次select、返回两个resultSet,再处理这两个resultSet。
代码如下:
SELECT * FROM BLOG WHERE ID = #{id}SELECT * FROM AUTHOR WHERE ID = #{id}<select id="selectBlog" resultSets="blogs,authors" resultMap="blogResult" statementType="CALLABLE"> {call getBlogsAndAuthors(#{id,jdbcType=INTEGER,mode=IN})}</select><resultMap id="blogResult" type="Blog"> <id property="id" column="id" /> <result property="title" column="title"/> <association property="author" javaType="Author" resultSet="authors" column="author_id" foreignColumn="id"> <id property="id" column="id"/> <result property="username" column="username"/> <result property="password" column="password"/> <result property="email" column="email"/> <result property="bio" column="bio"/> </association></resultMap>
三种实现方式比较
方式一
- 劣势:这种方式会出现N+1次查询的情况,当查询的数据量较大时,效率会非常非常查,但是MyBatis有延迟加载,如果查询后就使用内部的对象,延迟加载也没有作用。
- 优势:使用比较方便。
方式二
- 劣势:使用不太方便。当联合的表很多时,使用起来非常麻烦。
- 优势:不会出现N+1次查询的情况。
方式三
- 劣势:Starting from version 3.2.3。并不是所有数据库都支持。使用不方便。
- 优势:不会出现N+1次查询的情况。
最后
- 查询多个非基本类型对象使用collection,用法和association有很小区别,如果需要,你可以通过下面的网站自行补充。
- 该文章参考自:http://www.mybatis.org/mybatis-3/sqlmap-xml.html
1 0
- MyBatis查询嵌套对象
- MyBatis嵌套查询解析
- MyBatis嵌套查询解析
- 查询依赖对象(嵌套)
- mybatis 嵌套查询,一对多
- Mybatis接口映射,嵌套查询
- mybatis 嵌套查询 学习记录
- mybatis 一对多,嵌套查询
- Mybatis关联查询(嵌套查询)
- Mybatis关联查询(嵌套查询)
- Mybatis查询结果列表嵌套列表
- Mybatis嵌套查询讲解内容的基础
- Mybatis嵌套查询的原理与例子
- Mybatis 嵌套查询(高级结果映射)
- Mybatis各种查询,一对一,一对多,嵌套
- mybatis查询嵌套之参数传递
- Mybatis 批量查询 Map 嵌套 List
- MyBatis 中使用 Association 嵌套查询
- 光伏能源公司(未完,继续整理中~)
- CentOS7 安装cobbler自动部署ubuntu
- Linux下的压缩zip,解压缩unzip命令详解及实例
- android Monkey
- centos+RabbitMQ安装详解
- MyBatis查询嵌套对象
- 虚拟机栈和本地方法栈溢出
- 泊松图像融合(泊松融合)
- 统计学习方法(三) logisitic回归与最大熵模型
- SIFT算法大致原理作用
- 登录被拒绝; 权限不足或用户名/口令无效
- 序
- 通过document.referrer判断 及实现页面跳转
- 关于无线网卡中的一个参数:DB的介绍