官方文档地址:http://mybatis.github.io/mybatis-3/zh/sqlmap-xml.html
resultMap 元素是 MyBatis 中最重要最强大的元素。它就是让你远离 90%的需要从结果 集中取出数据的 JDBC 代码的那个东西, 而且在一些情形下允许你做一些 JDBC 不支持的事 情。 事实上, 编写相似于对复杂语句联合映射这些等同的代码, 也许可以跨过上千行的代码。 ResultMap 的设计就是简单语句不需要明确的结果映射,而很多复杂语句确实需要描述它们 的关系。
resultMap
- constructor - 类在实例化时,用来注入结果到构造方法中
- idArg - ID 参数;标记结果作为 ID 可以帮助提高整体效能
- arg - 注入到构造方法的一个普通结果
- id – 一个 ID 结果;标记结果作为 ID 可以帮助提高整体效能
- result – 注入到字段或 JavaBean 属性的普通结果
- association – 一个复杂的类型关联;许多结果将包成这种类型
- 嵌入结果映射 – 结果映射自身的关联,或者参考一个
- collection – 复杂类型的集
- discriminator – 使用结果值来决定使用哪个结果映射
- case – 基于某些值的结果映射
- 嵌入结果映射 – 这种情形结果也映射它本身,因此可以包含很多相 同的元素,或者它可以参照一个外部的结果映射。
association:
表示“有一个”的关系
我们新添加一个类
- package org.ygy.model;
-
- import java.util.Date;
-
- import org.ygy.util.Configs;
- import org.ygy.util.DateUtil;
-
-
-
-
-
-
- public class Blog {
- private int id;
- private String title;
- private String content;
- private Date reportTime;
- private User author;
-
- 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;
- }
-
- public String getContent() {
- return content;
- }
-
- public void setContent(String content) {
- this.content = content;
- }
-
- public Date getReportTime() {
- return reportTime;
- }
-
- public void setReportTime(Date reportTime) {
- this.reportTime = reportTime;
- }
-
- public User getAuthor() {
- return author;
- }
-
- public void setAuthor(User author) {
- this.author = author;
- }
-
- @Override
- public String toString() {
- return "Blog [id=" + id + ", title=" + title + ", content=" + content
- + ", report_time=" + DateUtil.fromDate(reportTime, Configs.CURRENTTIME) + ", author=" + author + "]";
- }
-
- }
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-
- <mapper namespace="org.ygy.mapper.BlogMapper">
-
- <resultMap type="Blog" id="BlogMap">
- <id column="blog_id" property="id"/>
- <result column="title" property="title"/>
- <result column="b_content" property="content"/>
- <result column="reportTime" property="reportTime"></result>
-
-
- <association column="author_id" property="author" javaType="User">
- <id column="user_id" property="id"/>
- <result column="name" property="name"></result>
- </association>
- </resultMap>
-
- <select id="queryAll" resultMap="BlogMap">
- select b.id as blog_id, b.title , b.b_content,b.reportTime , u.id as user_id , u.name from t_blog b
- left outer join t_user u on b.author_id = u.id
- </select>
-
- <sql id="baseColumn">
- title , b_content , author_id , reportTime
- </sql>
-
-
- <insert id="insert" parameterType="Blog">
- insert into t_blog
- (<include refid="baseColumn"/>)
- values(#{title} , #{content} , #{author.id} , #{reportTime})
- </insert>
-
-
- <update id="update" parameterType="Blog">
- update t_blog
- <set>
- <if test="title != null">title = #{title}</if>
- <if test="content != null">b_content = #{content}</if>
- <if test="report_time != null">reportTime = #{reportTime}</if>
- </set>
- where id=#{id}
- </update>
- </mapper>
关联元素处理“有一个”类型的关系。比如,在我们的示例中,一个博客有一个用户。关联映射就工作于这种结果之上。你指定了目标属性,来获取值的列,属性的 java 类型(很多情况下 MyBatis 可以自己算出来),如果需要的话还有 jdbc 类型,如果你想覆盖或获取的结果值还需要类型控制器。
关联中不同的是你需要告诉 MyBatis 如何加载关联。MyBatis 在这方面会有两种不同的方式:
- 嵌套查询:通过执行另外一个 SQL 映射语句来返回预期的复杂类型。
- 嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集。首先,然让我们来查看这个元素的属性。所有的你都会看到,它和普通的只由 select 和
resultMap 属性的结果映射不同。
上面使用的应该算是嵌套结果,下面写一个嵌套查询的例子:
- <resultMap type="Blog" id="blogMap_2">
- <id column="id" property="id"/>
- <result column="title" property="title"/>
- <result column="b_content" property="content"/>
- <result column="reportTime" property="reportTime"></result>
-
- <association column="author_id" property="author" javaType="User" select="selectUser" />
- </resultMap>
-
- <select id="selectBlog" resultMap="blogMap_2">
- select * from t_blog
- </select>
-
- <select id="selectUser" parameterType="int" resultType="User">
- select id , name from t_user where id=#{id}
- </select>
collection
- <resultMap type="User" id="userMap">
- <id column="id" property="id"/>
- <result column="name" property="name"/>
-
- <collection property="blogList" ofType="Blog">
- <id column="blog_id" property="id"/>
- <result column="title" property="title"/>
- <result column="b_content" property="content"/>
- <result column="reportTime" property="reportTime"/>
- </collection>
- </resultMap>
-
- <select id="selectUserBlog" resultMap="userMap">
- select u.id , u.name , b.id as blog_id, b.title , b.b_content ,b.reportTime from t_user u
- left outer join t_blog b on u.id=b.author_id
- </select>
在User类中添加的List<Blog> blogList;
- package org.ygy.model;
-
- import java.util.List;
-
-
-
-
-
-
- public class User {
- private Integer id;
- private String name;
- private String password;
- private String email;
- private Integer age;
- private Integer gender;
- private List<Blog> blogList;
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
-
- public String getEmail() {
- return email;
- }
-
- public void setEmail(String email) {
- this.email = email;
- }
-
- public Integer getAge() {
- return age;
- }
-
- public void setAge(Integer age) {
- this.age = age;
- }
-
- public Integer getGender() {
- return gender;
- }
-
- public void setGender(Integer gender) {
- this.gender = gender;
- }
-
- public List<Blog> getBlogList() {
- return blogList;
- }
-
- public void setBlogList(List<Blog> blogList) {
- this.blogList = blogList;
- }
-
- @Override
- public String toString() {
- return "User [id=" + id + ", name=" + name + ", password=" + password
- + ", email=" + email + ", age=" + age + ", gender=" + gender
- + ", blogList=" + blogList + "]";
- }
-
- }