这几天,百度mybatis突然看不到官网了,不知道百度怎么整的。特此贴出mybatis中文官网:

http://www.mybatis.org/mybatis-3/zh/index.html

一个学习mybatis的英文网站:http://mybatis.co.uk/

一.概论

大类里面有一个小类用association,大类里面有多个小类用collection。之前学习过json或者xml-rpc,里面的组合数据类型都是两种:数组和结构体。数组就是集合,就是序列。结构体就是映射,就是键值对。

二.关于typeAliases属性

_byte映射的是byte(值类型),byte映射的是Byte(引用类型)。系统已经为许多常见的 Java 类型内建了相应的类型别名。它们都是大小写不敏感的,需要注意的是由基本类型名称重复导致的特殊处理。下面是一些非基本数据类型的别名定义,左边是mybatis的,右边是对应的java中的对象。

date

Date

decimal

BigDecimal

bigdecimal

BigDecimal

object

Object

map

Map

hashmap

HashMap

list

List

arraylist

ArrayList

collection

Collection

iterator

Iterator

三.association的用法

大类里面有一个小类,那就用association。用association有两种方式:一种是查询一次,直接映射;另一种方式是:通过association的select属性,再次发起一个查询,这就是传说中的1+N问题。前者相当于迈了一大步,后者相当于小碎步走了好几下。迈一大步的好处在于:一次完成任务,避免了多次请求数据库,缺点是这一个大请求可能占用了数据库大量的资源,别人没法进行写操作,造成等待;迈小碎步的好处在于:灵活控制,语句简单,缺点是可能会很费时间。凡是各有利弊,只得得愿老天保佑。

迈一大步:列出博客的详细信息,包括blog_id,blog_title及作者的id,用户名,密码,性别等信息,也就是class Blog里有一个成员变量User author。

<select id="test2" parameterType="int" resultMap="test2Map" >

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,

A.favourite_section as author_favourite_section

from

Blog B

left join Author A on (B.author_id = A.id)

where

B.id = #{id}

</select>

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

<id property="id" column="blog_id" javaType="int"/>

<result property="title" column="blog_title" javaType="string"/>

<association property="author" column="blog_author_id" javaType="Author">

<id property="id" column="author_id" javaType="_int"/>      <result property="username" column="author_username" javaType="string"/>      <result property="password" column="author_password" javaType="string"/>      <result property="email" column="author_email" javaType="string"/>      <result property="bio" column="author_bio" javaType="string"/>      <result property="favouriteSection" column="author_favourite_section" javaType="string"/>

</association></resultMap>

迈两次小碎步:首先获取用户的id,再发起一次查询。

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

<id property="id" column="blog_id" javaType="int"/>

<result property="title" column="blog_title" javaType="string"/>

<association property="author" column="blog_author_id" javaType="Author" select="test2DivideSelect">

</association>

</resultMap>

<select id="test2DivideSelect" parameterType="int" resultType="Author">

select * from author where id = #{id}</select>

不一定是大类里面包含一个小类,可能是两三个个小类,这时迈一大步就需要通过association标签的columnPrefix属性(相对应的查询语句也要多用as,记住typeAlias和as可以给一个东西重命名,这个很有用)。如果一个大类里面包含的是更多的小类,那就要用List<小类>了。

<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>

四.resultMap的collections属性

一个大类里面包含着多个小类:class User里面有一个成员变量List<Blog>blogs也就是说一个用户对应多篇文章。

<select id="test3" parameterType="int" resultMap="test3Map">

select

A.id as author_id,

A.username as author_username,

A.email as author_email,

B.id as blog_id,

B.title as blog_title,

B.author_id as blog_author_id

from

Author A

left join Blog B on (A.id = B.author_id)

where

A.id = #{id}

</select>

<resultMap type="Author" id="test3Map">

<id column="author_id" property="id" javaType="_int"/>

<result column="author_username" property="username" javaType="string"/>

<result column="author_email" property="email" javaType="string"/>

<collection column="blog_author_id" property="blogs" javaType="ArrayList" ofType="Blog">

<id column="blog_id" property="id" javaType="_int"/>

<result column="blog_title" property="title" javaType="string"/>

</collection></resultMap>

就如上述所说, collection 即表示“多个 ” 的关系, 必须注意的是,一定要指定ofType 属性,这个 ofType 属性指的是集合的元素类型,缺少这个属性, MyBatis 会报出设定参数错误的提示 。

就如同 association 一样, collection 也分两种:一种为嵌套查询 select ,另一种为嵌套结果 resultMap ,用法也跟 association 一致,在这里就不再详细述说。

转载于:https://www.cnblogs.com/weiyinfu/p/5365574.html

mybatis resultMap映射学习笔记相关推荐

  1. 【BackEnd--SSM 框架详解】Mybatis+Spring+SpringMVC学习笔记(完整详细版)

    BackEnd 学习笔记 1 Java 2 SSM框架(Mybatis+Spring+SpringMVC) 3 SpringBoot(SpringBoot 3.x+MybatisPlus) 4 Spr ...

  2. mybatis resultMap映射详解

    <resultMap>是Maybatis的结果集封装,搭配<select><association>等标签的resultMap属性使用属性:id:该封装规则的唯一标 ...

  3. SpringMVC与Mybatis整合---SpringMVC学习笔记(六)

    SpringMVC整合Mybatis的系统架构: 整合思路 第一步:整合dao层 mybatis和spring整合,通过spring管理mapper接口. 使用mapper的扫描器自动扫描mapper ...

  4. [OpenCV] cv.remap() 重映射学习笔记/map1 map2易混点

    文章目录 一.背景 二.Notes (一)更新几个小例子方面理解remap 1. 利用remap 进行 copy 2.进行翻转 学习remap的时候发现map1和map2的参数跟我的直观理解略有差别, ...

  5. MyBatis源码学习笔记(从设计模式看源码)

    文章目录 1.源码分析概述 ①.Mybatis架构分析 ②.门面模式 ③.设计模式的原则 2.日志模块分析 ①.适配器模型 ②.动态代理 ③.日志模块分析 3.数据源模块分析 ①.工厂模式 ②.数据源 ...

  6. mmap内存映射学习笔记

    1.进行虚拟内存结构体 前文<虚拟内存管理>我们讲到每个进程的结构体task_struct中都有一个内存描述结构体mm_struct. mm_struct此结构体描述了进程占用虚拟内存的情 ...

  7. Mybatis源码学习笔记

    Mybatis核心概念: Configuration : 管理 mysql-config.xml 全局配置关系类 SqlSessionFactory: Session 管理工厂接口 Session:  ...

  8. Mybatis源码学习笔记之Mybatis二级缓存

    简介   Mybatis一级缓存是会话级的缓存,而二级缓存则是应用级别的缓存,默认关闭,二级缓存使用不慎可能会导致脏读. 开启方式(SpringBoot+Mybatis)   application. ...

  9. mybatis学习笔记(7)-输出映射

    2019独角兽企业重金招聘Python工程师标准>>> mybatis学习笔记(7)-输出映射 标签: mybatis [TOC] 本文主要讲解mybatis的输出映射. 输出映射有 ...

最新文章

  1. FE.ES-终结0.1+0.2,答到点上的那种
  2. Java 锁机制 synchronized
  3. Direct Byte Buffer的操作
  4. linux 改目录前缀,Linux修改终端显示前缀及环境变量
  5. Log4j.properties的简单配置
  6. X86汇编语言从实模式到保护模式08:中断和动态时钟显示
  7. tcpdf最新版 6.2版
  8. PX4无人机配置4G空地多机组网系统
  9. Android 通过联系人姓名查询联系人号码
  10. 关于在新办纳税人中实行增值税专用发票电子化有关事项的公告
  11. 嵌入式高速串行并行技术_推荐(张锋)
  12. Java金额转换_阿拉伯数字转换成中国大写数字
  13. uva 10827 Maximum sum on a torus
  14. git提交规范图-提问的智慧图谱-React 学习路线图- 达克效应
  15. 长沙民政职业技术学院计算机网络技术专业,长沙民政职业技术学院计算机网络技术专业...
  16. 光时域反射仪都有什么功能
  17. 基于spring boot的毕业设计论文选题申报管理系统设计与实现 毕业论文+项目源码、
  18. windows核心编程 第四章 进程
  19. c语言双边滤波算法,浅析bilateral filter双边滤波器的理解
  20. 323篇论文被ACM全部撤稿!放眼望去都是中国作者,原因令人无语...

热门文章

  1. [luogu3198] 玩具装箱
  2. 【笔记】LR录制方式和常用函数
  3. Bootstraptable源码
  4. 跟我一起学Angular2(1)-了解ng模块化
  5. Angular JS(二) 指令部分
  6. Linux操作Oracle(8)——Oracle数据库迁移全纪录(1) — 表空间 用户 权限迁移
  7. 一年了,写点关于人生的东西吧
  8. Github图片无法打开的问题解决【2020.07.20更新】
  9. 股票资金净流入和净流出
  10. ubuntu中bash,sh,./,bash区别