<resultMap id="map的标识" type="Java实体类"> <id column="数据库列名" javaType="Java类型" jdbcType="数据库类型" property="Java实体类属性名" typeHandler="类型处理器"/> <result column="数据库列名" javaType="Java类型" jdbcType="数据库类型" property="Java实体类属性 名" typeHandler="类型处理器"/> <association property="Java实体类属性名" column="数据库列名" select="selectid"/>或 <association property="Java实体类属性名" column="数据库列名" resultMap="resultMapId"/> <collection property="Java实体类属性名" column="数据库列名" javaType="ArrayList" ofType="JavaBean" select="selectid"/>或<collection property="Java实体类属性名" column="数据库 列名" javaType="ArrayList" ofType="JavaBean" resultMap="resultMapId"/> <discriminator column="数据库列名(用于比较值的列)" javaType="Java类型" jdbcType="数据库类型"> <case value="值1" resultType="Java实体类"> <result column="数据库列名" javaType="Java类型" jdbcType="数据库类型" property="Java实体类属性 名" typeHandler="类型处理器"/> </discriminator> </resultMap>
属性 |
描述 |
property |
需要映射到JavaBean 的属性名称。 |
column |
数据表的列名或者标签别名。 |
javaType |
一个完整的类名,或者是一个类型别名。如果你匹配的是一个JavaBean,那MyBatis 通常会自行检测到。然后,如果你是要映射到一个HashMap,那你需要指定javaType 要达到的目的。 |
jdbcType |
数据表支持的类型列表。这个属性只在insert,update 或delete 的时候针对允许空的列有用。JDBC 需要这项,但MyBatis 不需要。如果你是直接针对JDBC 编码,且有允许空的列,而你要指定这项。 |
typeHandler |
使用这个属性可以覆写类型处理器。这项值可以是一个完整的类名,也可以是一个类型别名。 |
association关联用于处理“有一个”类型的关系,也就是通常所说的"1对1的情况"比如一个用户有一个博客。
有三种关联方式
通过执行另一个SQL映射语句来返回预期的复杂类型。<因为此方式会引起N+1问题即是执行多次SQL语句,所以不推荐使用此方式>
格式如下:
<association column="column1,column2,column3" javaType="另一个Java类名" property="属性名" select="otherSelectID"/> <select id="otherSelectID" parameterType="int" resultType="另一个Java类名"> select * from otherTable where column1=#{column1} </select>
例子:
Java实体类
package com.myapp.domain import java.io.Serializable; import com.myapp.domain.Author public class Blog implements Serializable { private static final long serialVersionUID = -4268898209820709788L; private Integer id; private Author author; private String blogName; ...... } public class Author implements Serializable { private static final long serialVersionUID = -4268898209820709788L; private Integer id; private String authorName; ...... } 配置文件
<?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="com.myapp.mapper.BlogMapper" > <resultMap id=”blogResult” type=”com.myapp.domain.Blog”> <association property="author" column="blog_author_id" javaType="Author" select=”selectAuthor”/> </resultMap> <select id=”selectBlog” parameterType=”int” resultMap=”blogResult”> SELECT * FROM BLOG WHERE ID = #{id} </select> <select id=”selectAuthor” parameterType=”int” resultType="com.myapp.domain.Author"> SELECT * FROM AUTHOR WHERE ID = #{id} </select> </mapper>
映射关联的嵌套结果到一个合适的对象图中
格式如下:
<association property="Java属性名" column="关联主键字段名" javatype="所嵌套的结果Java类名" resultMap="otherMapID"/>
例子如下:
<?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="com.myapp.mapper.ClassesMapper"> <select id="getClassesOne2One" resultMap="ClassesResultMap" parameterType="int"> select <include refid="Base_Column_List"/> <!--,b.t_id,b.t_name,b.t_c_id from Classes a left join Teacher b on a.c_id=b.t_c_id--> ,b.t_id ,b.t_name,b.t_c_id from Classes a,Teacher b where a.c_id=b.t_c_id </select> <resultMap id="ClassesResultMap" type="com.myapp.domain.Classes" > <id column="c_id" property="id" jdbcType="INTEGER" /> <result column="c_name" property="name" jdbcType="VARCHAR" /> <association property="teacher" column="t_id" javaType="com.myapp.domain.Teacher" resultMap="TeacherMap"/> </resultMap> <resultMap id="TeacherMap" type="com.myapp.domain.Teacher"> <id column="t_id" property="id"/> <result column="t_name" property="name" typeHandler="com.myapp.test.MyTestTypeHandler"></result> </resultMap>
/** * */ package com.myapp.domain; import java.io.Serializable; /** * @author louisliao * */ public class Classes extends BaseObject implements Serializable { private static final long serialVersionUID = -4268898209820709788L; private Integer id; private String name; private Teacher teacher; 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 Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } } /** * */ package com.myapp.domain;import java.io.Serializable;/** * @author louisliao * */ public class Teacher extends BaseObject implements Serializable { private static final long serialVersionUID = -4268898209820709788L; private Integer id; private String name; 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; } }
/** * */ package com.myapp.domain;import java.io.Serializable;/** * @author louisliao * */ public class Teacher extends BaseObject implements Serializable { private static final long serialVersionUID = -4268898209820709788L; private Integer id; private String name; 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; } }
测试类
try { String resource="testConfig.xml"; Reader reader=Resources.getResourceAsReader(resource); sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader); SqlSession sqlSession=sqlSessionFactory.openSession(); ClassesMapper classesMapper=sqlSession.getMapper(ClassesMapper.class); List<Classes> classes=classesMapper.getClassesOne2One(1); System.out.println(classes.size()); reader.close(); } catch (Exception e) { System.out.println(e.getMessage()); // TODO: handle exception } finally { sqlSession.close(); }