您的位置:首页 > 其它

mybatis resultMap映射学习笔记

2016-04-07 21:33 429 查看
这几天,百度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 一致,在这里就不再详细述说。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: