【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. .NET Core 3 WPF MVVM框架 Prism系列之数据绑定

    一.安装Prism 1.使用程序包管理控制台 Install-Package Prism.Unity -Version 7.2.0.1367 也可以去掉‘-Version 7.2.0.1367’获取最 ...

  2. think PHP 查询、更改

    最近公司没有什么新项目,故准备搞搞PHP,正好后端有一些小东西需要搞一下,我就来试试吧. PHP 基于think PHP 3 实现功能: 1.为销售绑定虚拟号码分组(查询可以绑定的分组 -> 绑 ...

  3. cenos7搭建gitlab

    git.github和gitlab的区别 git:是一种版本控制系统,是一个命令,是一种工具 gitlib:是基于实现功能的开发库 github:是一个基于git实现的在线代码仓库软件 gitlib可 ...

  4. day 18 random模块 时间模块 sys模块 os模块

    import random 利用random模块可以进行从一个列表或者数字范围之间随机取出一个数字 # 取随机小数 : 数学计算 print(random.random()) # 取0-1之间的小数 ...

  5. 【Android - 控件】之MD - TabLayout的使用

    TabLayout是Android 5.0新特性——Material Design中的一个控件,是一个标签页的导航条,常结合ViewPager完成页面导航. 和其他MD控件一样,使用TabLayout ...

  6. php 第1讲 html介绍 html运行原理①

    1. html (hypertext mark-up language )是 超文本编辑语言,主要的用处是做网页,可以在网页上显示文字.图形.动画.视频... “标记“有时候也称之为“元素” 动态网页 ...

  7. CSS浮动和各种定位

    CSS定位 css定位机制 文档流:元素按照在HTML中的位置决定排布的过程 块级元素是从上到下的,内联元素是从左到右的 浮动 position布局 position css position属性用于 ...

  8. python_MySQL数据库

    MySQL数据库的特点:    1.是关系型数据库        关系型数据库的特点            1.数据是以行和列的的形式存储的            2.这一系列的行和列称为表      ...

  9. linux中安装tensorflow

    liunxsudo apt-get install python-pip python-dev python2.X -> pippython3.X -> pip3 pip --versio ...

  10. Linux下搭建web服务

    第一:安装java 第二:安装tomcat 第三:部署程序 第一:安装java 下载地址: http://www.oracle.com/technetwork/java/javase/download ...