【MyBatis】ResultMap

转载:https://www.cnblogs.com/yangchongxing/p/10486854.html

支持的 JDBC 类型
为了未来的参考,MyBatis 通过包含的 jdbcType 枚举型,支持下面的 JDBC 类型。

注意:在指定jdbcType时类型是列出的这些,并且必须是大写

BIT FLOAT CHAR TIMESTAMP OTHER UNDEFINED
TINYINT REAL VARCHAR BINARY BLOB NVARCHAR
SMALLINT DOUBLE LONGVARCHAR VARBINARY CLOB NCHAR
INTEGER NUMERIC DATE LONGVARBINARY BOOLEAN NCLOB
BIGINT DECIMAL TIME NULL CURSOR ARRAY

 

目录

====================================================================

1、基本内容 id 和 result

2、构造方法 constructor

3、关联 association

4、集合 collection

5、鉴别器 discriminator

====================================================================

constructor - 用于在实例化类时,注入结果到构造方法中
  idArg - ID 参数;标记出作为 ID 的结果可以帮助提高整体性能
  arg - 将被注入到构造方法的一个普通结果
id – 一个 ID 结果;标记出作为 ID 的结果可以帮助提高整体性能
result – 注入到字段或 JavaBean 属性的普通结果
association – 一个复杂类型的关联;许多结果将包装成这种类型
  嵌套结果映射 – 关联可以指定为一个 resultMap 元素,或者引用一个
collection – 一个复杂类型的集合
  嵌套结果映射 – 集合可以指定为一个 resultMap 元素,或者引用一个
discriminator – 使用结果值来决定使用哪个 resultMap
  case – 基于某些值的结果映射
    嵌套结果映射 – 一个 case 也是一个映射它本身的结果,因此可以包含很多相 同的元素,或者它可以参照一个外部的 resultMap。

1、基本内容 id 和 result

属性 property,column,javaType,jdbcType,typeHandler

注意:jdbcType="VARCHAR" 上表列出的类型,必须大写

<id property="id" column="post_id"/>
<result property="subject" column="post_subject" jdbcType="VARCHAR"/>

2、构造方法 constructor

顺序决定选择那个构造器

构造器,注意:id 不能用 int 而要用 Integer 对象

    public Blog(Integer id, String title, String content) {
this.id = id;
this.title = title;
this.content = content;
}

映射

    <resultMap id="blogResultMap" type="Blog">
<constructor>
<idArg column = "id" javaType = "int"/>
<arg column = "title" javaType = "String"/>
<arg column = "content" javaType = "String"/>
</constructor>
</resultMap>

参数名决定选择那个构造器,与顺序无关

方式一、构造器添加 @Param 注解

    public Blog(@Param("id") Integer id, @Param("title") String title, @Param("content") String content) {
this.id = id;
this.title = title;
this.content = content;
}

映射顺序随意

    <resultMap id="blogResultMap" type="Blog">
<constructor>
<idArg column = "id" javaType = "int" name = "id"/>
<arg column = "content" javaType = "String" name = "content"/>
<arg column = "title" javaType = "String" name = "title"/>
</constructor>
</resultMap>

方式二、使用 '-parameters' 编译选项并启用 useActualParamName 选项(默认开启)来编译项目

  <settings>
<setting name="useActualParamName" value="true"/>
</settings>

useActualParamName 允许使用方法签名中的名称作为语句参数名称。 为了使用该特性,你的工程必须采用Java 8编译,并且加上-parameters选项。(从3.4.1开始)默认值是true

命令行:javac -parameters 文件

eclipse:Window->Preferences->Java->Compiler 下勾选 Store information about method parameters(usable via reflection)

    public Blog(Integer id, String title, String content) {
this.id = id;
this.title = title;
this.content = content;
}
    <resultMap id="blogResultMap" type="Blog">
<constructor>
<idArg column = "id" javaType = "int" name = "id"/>
<arg column = "title" javaType = "String" name = "title"/>
<arg column = "content" javaType = "String" name = "content"/>
</constructor>
</resultMap>

3、关联 association

用到的实体

public class Blog {
private int id;
private String title;
private String content;
protected Author author;
} public class Author {
protected int id;
protected String name;
}

①嵌套查询

映射

public interface BlogMapper {
public Blog selectBlog(int id);
}
    <resultMap id="blogResultMap" type="Blog">
<constructor>
<idArg column = "id" javaType = "int" name = "id"/>
<arg column = "title" javaType = "String" name = "title"/>
<arg column = "content" javaType = "String" name = "content"/>
</constructor>
<association property="author" column="author_id" javaType="Author" select="selectAuthor"/>
</resultMap>
<!-- 下面的参数通过上面 association 的 column 字段指定 -->
<select id="selectBlog" resultMap="blogResultMap" >
select id,title,content,author_id from blog where id = #{id}
</select>
<select id="selectAuthor" resultType="Author">
select id,name from author where id = #{id}
</select>

嵌套的 select author 参数 是通过 association 的 column 传递进去

②嵌套结果

<resultMap id="blogResultMap" type="Blog">
<constructor>
<idArg column = "id" javaType = "int" name = "id"/>
<arg column = "title" javaType = "String" name = "title"/>
<arg column = "content" javaType = "String" name = "content"/>
</constructor>
<!-- column 不需要 -->
<association property="author" javaType="Author">
<id property="id" column="author_id"/>
<result property="name" column="author_name"/>
</association>
</resultMap>
<select id="selectBlog" resultMap="blogResultMap" >
select b.id, b.title, b.content, b.author_id, a.name as author_name
from blog b
left outer join author a on b.author_id = a.id
where b.id = #{id}
</select>

引用外部结果映射

<resultMap id="blogResultMap" type="Blog">
<constructor>
<idArg column = "id" javaType = "int" name = "id"/>
<arg column = "title" javaType = "String" name = "title"/>
<arg column = "content" javaType = "String" name = "content"/>
</constructor>
<!-- 外部结果映射来映射关联 -->
<association property="author" javaType="Author" resultMap="authorResultMap"/>
</resultMap> <resultMap id="authorResultMap" type="Author">
<id property="id" column="author_id"/>
<result property="name" column="author_name"/>
</resultMap>
<select id="selectBlog" resultMap="blogResultMap" >
select b.id, b.title, b.content, b.author_id, a.name as author_name
from blog b
left outer join author a on b.author_id = a.id
where b.id = #{id}
</select>

使用 columnPrefix 共用外部结果映射

<resultMap id="blogResultMap" type="Blog">
<constructor>
<idArg column = "id" javaType = "int" name = "id"/>
<arg column = "title" javaType = "String" name = "title"/>
<arg column = "content" javaType = "String" name = "content"/>
</constructor>
<!-- 外部结果映射 -->
<association property="author" javaType="Author" resultMap="authorResultMap"/>
<association property="ycxAuthor" javaType="Author" resultMap="authorResultMap" columnPrefix="ycx_"/>
</resultMap>
<resultMap id="authorResultMap" type="Author">
<id property="id" column="author_id"/>
<result property="name" column="author_name"/>
</resultMap>
<select id="selectBlog" resultMap="blogResultMap" >
select b.id, b.title, b.content, b.author_id, a.name as author_name, ycx.id as ycx_author_id, ycx.name as ycx_author_name
from blog b
left outer join author a on b.author_id = a.id
left outer join author ycx on b.author_id = ycx.id
where b.id = #{id}
</select>

4、集合 collection

实体

public class Blog {
private int id;
private String title;
private String content;
protected Author author;
protected List<Post> posts;
}
public class Author {
protected int id;
protected String name;
}
public class Post {
protected int id;
protected String subject;
protected String body;
}

①嵌套查询

注意:javaType="ArrayList" 是 Blog 属性的的类型为 ArrayList,新字段 ofType="Post" 是集合 ArrayList 包含类型为 Post。javaType 可以省略 MyBatis 可以推算出来。

<resultMap id="blogResultMap" type="Blog">
<constructor>
<idArg column = "id" javaType = "int" name = "id"/>
<arg column = "title" javaType = "String" name = "title"/>
<arg column = "content" javaType = "String" name = "content"/>
</constructor>
<!-- 嵌套查询 column 是要传入子查询的值 -->
<association property="author" javaType="Author" column="author_id" select="selectAuthor"/>
<collection property="posts" javaType="ArrayList" column="id" ofType="Post" select="selectPost"/>
</resultMap>
<resultMap id="authorResultMap" type="Author">
<id property="id" column="author_id"/>
<result property="name" column="author_name"/>
</resultMap>
<select id="selectBlog" resultMap="blogResultMap">
select * from blog where id = #{id}
</select>
<select id="selectAuthor" resultType="Author">
select * from author where id = #{id}
</select>
<select id="selectPost" resultType="Post">
select * from post where blog_id = #{id}
</select>

②嵌套结果

    <resultMap id="blogResultMap" type="Blog">
<constructor>
<idArg column = "id" javaType = "int" name = "id"/>
<arg column = "title" javaType = "String" name = "title"/>
<arg column = "content" javaType = "String" name = "content"/>
</constructor>
<!-- 嵌套结果 -->
<association property="author" javaType="Author">
<id property="id" column="author_id"/>
<result property="name" column="author_name"/>
</association>
<collection property="posts" javaType="ArrayList" ofType="Post">
<id property="id" column="post_id"/>
<result property="subject" column="subject"/>
<result property="body" column="body"/>
</collection>
</resultMap>
<select id="selectBlog" resultMap="blogResultMap">
select b.id, b.title, b.content,
a.id as author_id, a.name as author_name,
p.id as post_id, p.subject as post_subject, p.body as post_body
from blog b
left outer join author a on b.author_id = a.id
left outer join post p on p.blog_id = b.id
where b.id = #{id}
</select>

引用外部结果映射

  <resultMap id="authorResultMap" type="Author">
<id property="id" column="author_id"/>
<result property="name" column="author_name"/>
</resultMap>
<resultMap id="postResultMap" type="Post">
<id property="id" column="post_id"/>
<result property="subject" column="post_subject"/>
<result property="body" column="post_body"/>
</resultMap>
<resultMap id="blogResultMap" type="Blog">
<constructor>
<idArg column = "id" javaType = "int" name = "id"/>
<arg column = "title" javaType = "String" name = "title"/>
<arg column = "content" javaType = "String" name = "content"/>
</constructor>
<!-- 外部结果映射 -->
<association property="author" javaType="Author" resultMap="authorResultMap"/>
<collection property="posts" javaType="ArrayList" ofType="Post" resultMap="postResultMap"/>
</resultMap>
<select id="selectBlog" resultMap="blogResultMap">
select b.id, b.title, b.content,
a.id as author_id, a.name as author_name,
p.id as post_id, p.subject as post_subject, p.body as post_body
from blog b
left outer join author a on b.author_id = a.id
left outer join post p on p.blog_id = b.id
where b.id = #{id}
</select>

使用 columnPrefix 共用外部结果映射

    <resultMap id="authorResultMap" type="Author">
<id property="id" column="id"/>
<result property="name" column="name"/>
</resultMap>
<resultMap id="postResultMap" type="Post">
<id property="id" column="id"/>
<result property="subject" column="subject"/>
<result property="body" column="body"/>
</resultMap>
<resultMap id="blogResultMap" type="Blog">
<constructor>
<idArg column = "id" javaType = "int" name = "id"/>
<arg column = "title" javaType = "String" name = "title"/>
<arg column = "content" javaType = "String" name = "content"/>
</constructor>
<!-- 外部结果映射 -->
<association property="author" javaType="Author" resultMap="authorResultMap" columnPrefix="author_"/>
<collection property="posts" javaType="ArrayList" ofType="Post" resultMap="postResultMap" columnPrefix="post_"/>
</resultMap>
<select id="selectBlog" resultMap="blogResultMap">
select b.id, b.title, b.content,
a.id as author_id, a.name as author_name,
p.id as post_id, p.subject as post_subject, p.body as post_body
from blog b
left outer join author a on b.author_id = a.id
left outer join post p on p.blog_id = b.id
where b.id = #{id}
</select>

5、鉴别器 discriminator

实体

public class Blog {
protected int id;
protected String title;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return "Blog [id=" + id + ", title=" + title + "]";
}
} public class BlogContent extends Blog{
protected String content;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return super.toString() + "\r\n" + "BlogContent [content=" + content + "]";
}
} public class BlogImage extends Blog {
protected String path;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
@Override
public String toString() {
return super.toString() + "\r\n" + "BlogImage [path=" + path + "]";
}
}

简洁方式

    <resultMap id="blogResultMap" type="Blog">
<id property="id" column="id"/>
<result property="title" column="title"/>
<discriminator javaType="int" column="type">
<case value="1" resultType="BlogContent">
<result property="content" column="content"/>
</case>
<case value="2" resultType="BlogImage">
<result property="path" column="path"/>
</case>
</discriminator>
</resultMap>
<select id="selectBlog" resultMap="blogResultMap">
select * from blog where id = #{id}
</select>

外部结果映射

    <resultMap id="blogContentResultMap" type="BlogContent">
<result property="content" column="content"/>
</resultMap>
<resultMap id="blogImageResultMap" type="BlogImage">
<result property="path" column="path"/>
</resultMap>
<resultMap id="blogResultMap" type="Blog">
<id property="id" column="id"/>
<result property="title" column="title"/>
<discriminator javaType="int" column="type">
<case value="1" resultMap="blogContentResultMap"/>
<case value="2" resultMap="blogImageResultMap"/>
</discriminator>
</resultMap>
<select id="selectBlog" resultMap="blogResultMap">
select * from blog where id = #{id}
</select>

【MyBatis】ResultMap的更多相关文章

  1. 【Mybatis】MyBatis之Sql配置文件的使用(四)

    上一章[Mybatis]MyBatis对表执行CRUD操作(三),已经讲了基本操作,本章介绍Sql配置文件中常用功能 1.插入返回主键 2.参数值的获取方式 3.resultMap使用 插入返回主键 ...

  2. 【Mybatis】MyBatis之动态SQL(六)

    MyBatis 的强大特性之一便是它的动态 SQL,本章介绍动态 SQL 查看本章,请先阅读[Mybatis]MyBatis对表执行CRUD操作(三). 本例表结构 CREATE TABLE `emp ...

  3. 【Mybatis】MyBatis对表执行CRUD操作(三)

    本例在[Mybatis]MyBatis配置文件的使用(二)基础上继续学习对表执行CRUD操作 使用MyBatis对表执行CRUD操作 1.定义sql映射xml文件(EmployeeMapper.xml ...

  4. 【Mybatis】MyBatis配置文件的使用(二)

    本例在[Mybatis]MyBatis快速入门(一)基础上继续学习XML映射配置文件 MyBatis 的配置文件包含了会深深影响 MyBatis 行为的设置(settings)和属性(properti ...

  5. 【Mybatis】1、Mybatis拦截器学习资料汇总

    MyBatis拦截器原理探究 http://www.cnblogs.com/fangjian0423/p/mybatis-interceptor.html [myBatis]Mybatis中的拦截器 ...

  6. 【MyBatis】动态 SQL

    [MyBatis]动态 SQL 转载: 目录 ========================================== 1.if 2.choose when otherwise 3.tri ...

  7. 【MyBatis】配置文件提示

    [MyBatis]配置文件提示 官方帮助文档:http://www.mybatis.org/mybatis-3/zh/index.html config配置 http://mybatis.org/dt ...

  8. 【Mybatis】MyBatis之配置自定义数据源(十一)

    本例是在[Mybatis]MyBatis之配置多数据源(十)的基础上进行拓展,查看本例请先学习第十章 实现原理 1.扩展Spring的AbstractRoutingDataSource抽象类(该类充当 ...

  9. 【Mybatis】MyBatis之配置多数据源(十)

    在做项目的过程中,有时候一个数据源是不够,那么就需要配置多个数据源.本例介绍mybatis多数据源配置 前言 一般项目单数据源,使用流程如下: 单个数据源绑定给sessionFactory,再在Dao ...

随机推荐

  1. zabbix template

    1. template 是分层级的,所有很多修改需要到父级去修改,而且影响所有子级.

  2. UDP 协议的那点事儿

    最近在回顾计算机网络的知识,以前上课没有认真学,只记得几个高大上的术语,所以趁着这次回顾,把学到的知识用博客的形式记录下来,一来加深自己的印象,二来希望让你对这些基础知识有一个更深入的了解.当然,我会 ...

  3. python3 之 文件read方法(read、readline、readlines)

    目录 一.read方法 二.readline方法 三.readlines方法 正文 python3中,读取文件有三种方法:read().readline().readlines(). 此三种方法,均支 ...

  4. Mysql数据库调优和性能优化的21条最佳实践

    Mysql数据库调优和性能优化的21条最佳实践 1. 简介 在Web应用程序体系架构中,数据持久层(通常是一个关系数据库)是关键的核心部分,它对系统的性能有非常重要的影响.MySQL是目前使用最多的开 ...

  5. Linux高级命令-sort、uniq、 cut、sed、grep、find、awk

    sort(参考学习网站:http://www.cnblogs.com/dong008259/archive/2011/12/08/2281214.html) 功能:根据不同的数据类型进行排序 格式:s ...

  6. [ch04-03] 用神经网络解决线性回归问题

    系列博客,原文在笔者所维护的github上:https://aka.ms/beginnerAI, 点击star加星不要吝啬,星越多笔者越努力. 4.3 神经网络法 在梯度下降法中,我们简单讲述了一下神 ...

  7. c语言博客专业05

    问题 答案 这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-2/homework/8655 我在 ...

  8. MATLAB工具包——curvelet变换的理解(转)

    curvelet下载的curvelet工具包,有以下三个文件:fdct_usfft_matlab.fdct_wrapping_matlab.mecv三个文件夹添加到matlab路径中即可. curve ...

  9. Android Jni开发,报com.android.ide.common.process.ProcessException: Error configuring 错误解决方案

    今天在练习JNI项目时,Android studio版本为:3.1.3,Gradle版本为4.4.由于Android studio 3.X弃用了 android.useDeprecatedNdk=tr ...

  10. 更适合Pythoner的标记语言Yaml学习总结

    pythonic的标记语言 之前总结过一篇关于小数据存储文件大比拼,当时着重介绍了json,因为它在各类编程语言的通用性较强.但今天,我想给大家介绍一款更加适合pythoner使用的语言Yaml. Y ...