MyBatis学习(八):ResultMap结果集映射

ResultMap结果集映射

resultMap 元素是 MyBatis 中最重要最强大的元素。它的作用是告诉 MaBatis 将从结果集中取出的数据转化成开发者所需要的对象。

下面是最简单的映射语句示例:

<select id="selectAllStudent" resultType="map">
	select * from student
</select>

selectAllStudent 的 < select> 元素执行一条查询语句,查询 student 表的所有数据。resultType=“map” 表示返回的数据是一个 map 集合(使用类名作为 key,列值作为 value)

示例:测试 ResultMap

测试类如下:

package com.zhang.work;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zhang.pojo.Student;
import com.zhang.service.impl.StudentServiceImpl;

public class MainApp {

	public static void main(String[] args) {
		
	
	 ApplicationContext applicationContext  = new ClassPathXmlApplicationContext("applicationContext.xml");
	
	 StudentServiceImpl studentService = applicationContext.getBean("studentService", StudentServiceImpl.class);
	 
	 List<Map<String,Object>> studentList = studentService.findAllStudent();
	 
	 for(Map<String,Object> row:studentList) {
		 System.out.println(row);
	 }
	}
}

运行MainApp 类 的 main方法,控制台显示如下:
在这里插入图片描述
可以看到,查询语句返回的每一条数据都被封装成一个Map集合,列名作为Map集合的key,而列的值作为Map 的 value

虽然数据被封装成 Map 集合返回,但是 Map 集合并不能很好的描述一个领域模型,可读性稍差。
有的开发者不太喜欢使用 Map,更多时候喜欢使用 POJO 的方式。例如:

<select id="selectAllStudent" resultType="Student">
	select * from student
</select>

默认情况下,MyBatis 会将查询到的数据的列和需要返回的对象(Student) 的属性 逐一进行匹配赋值,但是如果查询到的数据与需要返回的对象(Student) 的属性不一致,则MyBatis 就不会自动赋值了,这时,可以使用resultMap 进行处理。

进入数据库,创建一个表 student_test,并插入几条测试语句

DROP TABLE IF EXISTS `student_test`;
CREATE TABLE `student_test`  (
  `sid` int(11) NOT NULL,
  `sname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `ssex` varchar(4) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `sage` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`sid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

INSERT INTO `student_test` VALUES (1, 'Jack', '男', 22);
INSERT INTO `student_test` VALUES (2, 'Rose', '女', 18);
INSERT INTO `student_test` VALUES (3, 'Tom', '男', 21);
INSERT INTO `student_test` VALUES (4, 'Mary', '女', 20);

创建一个 Student 对象映射 student_test 表

package com.zhang.pojo;

public class Student {

	private int id;
	
	private String name;
	
	private String sex;
	
	private int age;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + "]";
	}
}

StudentMapper.xml

<?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.zhang.mapper.StudentMapper">
    
    
    <resultMap type="Student" id="studentResultMap">
    	<id property="id" column="sid"/>
    	<result property="name" column="sname"/>
    	<result property="sex" column="ssex"/>
    	<result property="age" column="sage"/>
    </resultMap>
    
    <!-- 查询所有学生信息 -->
    <select id="selAllStudent" resultMap="studentResultMap">
    	select * from student_test
    </select>
    
</mapper>

上面使用了 < resultMap> 元素,该元素常用属性如下:

  • id: resultMap 的唯一标识,用来引用此结果集
  • type: resultMap 实际返回的类型

上面使用了< result>的两个子元素 idresult

  • id: 表示数据库表的主键,其中,column 属性表示数据库表的列名,property 属性表示数据库映射返回类型的属性
  • result: 表示数据库表的普通列,其中,column 属性表示数据库表的列名,property 属性表示数据库映射返回类型的属性

MainApp 类内容如下:

package com.zhang.work;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zhang.pojo.Student;
import com.zhang.service.impl.StudentServiceImpl;

public class MainApp {

	public static void main(String[] args) {
		
	
	 ApplicationContext applicationContext  = new ClassPathXmlApplicationContext("applicationContext.xml");
	
	 StudentServiceImpl studentService = applicationContext.getBean("studentService", StudentServiceImpl.class);
	 
	 List<Student> studentList = studentService.findAllStudent();
	 
	 for(Student student:studentList) {
		 System.out.println(student);
	 }
	}
}

运行MainApp 类 的 main方法,控制台显示如下:
在这里插入图片描述
可以看到,student_test 的列名虽然与 Student 对象的属性名不一致,数据依然被正确封装到 Student 对象当中。

在实际项目开发中,还有更加复杂的情况,例如执行的是一个多表查询,而返回的对象关联到另一个对象中,此时简单的映射已经无法解决问题,必须使用< resultMap>元素来完成映射。

进入数据库创建两个表 classesstudent,并分别插入几条测试数据。

DROP TABLE IF EXISTS `classes`;
CREATE TABLE `classes`  (
  `id` int(11) NOT NULL,
  `code` char(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

INSERT INTO `classes` VALUES (1, 'yd1815925');
INSERT INTO `classes` VALUES (2, 'yd1915925');

DROP TABLE IF EXISTS `student`;
CREATE TABLE `student`  (
  `id` int(11) NOT NULL,
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `sex` varchar(4) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `age` int(11) NULL DEFAULT NULL,
  `classes_id` int(11) NOT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `classes_id`(`classes_id`) USING BTREE,
  CONSTRAINT `student_ibfk_1` FOREIGN KEY (`classes_id`) REFERENCES `classes` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

INSERT INTO `student` VALUES (1, 'Jack', '男', 22, 1);
INSERT INTO `student` VALUES (2, 'Rose', '女', 18, 1);
INSERT INTO `student` VALUES (3, 'Tom', '男', 21, 2);
INSERT INTO `student` VALUES (4, 'Mary', '女', 20, 2);

接下来创建一个 Classes 对象 和 Student 对象分别映射 classes 表 和 student

  1. Classes.java

    package com.zhang.pojo;
    public class Classes {
    
    	private Integer id;
    	
    	private String code;
    
    	public Integer getId() {
    		return id;
    	}
    
    	public void setId(Integer id) {
    		this.id = id;
    	}
    
    	public String getCode() {
    		return code;
    	}
    
    	public void setCode(String code) {
    		this.code = code;
    	}
    
    	@Override
    	public String toString() {
    		return "Classes [id=" + id + ", code=" + code + "]";
    	}
    	
    	
    }
    
    
  2. Student.java

    package com.zhang.pojo;
    
    public class Student {
    
    	private Integer id;
    	
    	private String name;
    	
    	private String sex;
    	
    	private int age;
    
    	private Classes classes;
    
    	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 getSex() {
    		return sex;
    	}
    
    	public void setSex(String sex) {
    		this.sex = sex;
    	}
    
    	public int getAge() {
    		return age;
    	}
    
    	public void setAge(int age) {
    		this.age = age;
    	}
    
    	public Classes getClasses() {
    		return classes;
    	}
    
    	public void setClasses(Classes classes) {
    		this.classes = classes;
    	}
    
    	@Override
    	public String toString() {
    		return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + ", classes=" + classes + "]";
    	}
    }
    

StudentMapper.xml

<?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.zhang.mapper.StudentMapper">
    
    
    <resultMap type="Student" id="studentResultMap">
    	<id property="id" column="id"/>
    	<result property="name" column="name"/>
    	<result property="sex" column="sex"/>
    	<result property="age" column="age"/>
    	<association property="classes" column="classes_id"
    	javaType="Classes" select="selectClassesById"></association>
    </resultMap>
    
    <!-- 根据班级id查询班级 -->
    
    <select id="selectClassesById" resultType="Classes">
    	select * from classes where id = #{id}
    </select>
    
    <!-- 查询所有学生信息 -->
    <select id="selAllStudent" resultMap="studentResultMap">
    	select * from student
    </select>
    
</mapper>

上面的映射比之前复杂了一些,具体解释如下:

  1. 首先执行 id 为 selectAllStudent 的 < select/>元素,查询所有的学生数据,此时返回的不是简单的 Student 对象,因为 Student 对象中还包含了 Classes 对象,所以使用 resultMap 去映射返回类型

  2. id 为 studentResultMap 的 < resultMap/>元素返回类型为 com.zhang.pojo.Student 其中,id name sex 和 age 都是简单的属性映射,而查询的班级 id 列 classes_id 则使用了关联映射 < association/>

    < association/>元素的解释如下:

    1. column: 表示数据库表的列名
    2. property: 表示返回类型 Student 的属性名 classes
    3. javaType: 表示该属性对应的类型名称
    4. select: 表示执行一条查询语句,将查询到的数据封装到 property 所代表的类型对象当中。

MainApp.java

package com.zhang.work;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zhang.pojo.Student;
import com.zhang.service.impl.StudentServiceImpl;

public class MainApp {

	public static void main(String[] args) {
		
	
	 ApplicationContext applicationContext  = new ClassPathXmlApplicationContext("applicationContext.xml");
	
	 StudentServiceImpl studentService = applicationContext.getBean("studentService", StudentServiceImpl.class);
	 
	 List<Student> studentList = studentService.findAllStudent();
	 
	 for(Student student:studentList) {
		 System.out.println(student);
	 }
	}
}

运行 MainApp 类的 main 方法,控制台显示如下:

在这里插入图片描述

可以看到,因为使用了关联映射,查询学生信息时学生对应的班级对象也被查询出来了。那如果反过来,查询所有班级时需要查询出班级中的所有学生对象,应该如何映射?

学生通常值对应一个班级,但是班级中会有多个学生存在,所以现在 Classes.java 类中增加一个字段studentList,该字段是一个List集合,表示班级的多个学生

private List<Student> studentList;

public void setStudentList(List<Student> studentList){
	this.studentList = studentList
}

public List<Student> getStudentList(){
	return studentList;
}

ClassesMapper.xml

<?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.zhang.mapper.ClassesMapper">
    
    
    <resultMap type="Classes" id="classesResultMap">
    	<id property="id" column="id"/>
    	<result property="code" column="code"/>
    	<collection property="studentList" column="id"
    		javaType="ArrayList" ofType="Student" select="selectStuentByClass_id"
    	></collection>
    </resultMap>
    
    <!-- 根据班级id查询学生信息 -->
    
    <select id="selectStuentByClass_id" resultType="Student">
    	select * from student where classes_id = #{id }
    </select>
    
    <!-- 查询所有班级信息 -->
    <select id="selAllClasses" resultMap="classesResultMap">
    	select * from classes
    </select>
    
</mapper>

具体解释如下:

  1. 首先执行 id 为 selectAllClasses 的 < select/>元素,查询所有的学生数据,此时返回的不是简单的 Classes 对象,因为 Classes对象中还包含了 Student 对象,所以使用 resultMap 去映射返回类型

  2. id 为 classesResultMap 的 < resultMap/>元素返回类型为 com.zhang.pojo.Classes 其中,id 和 code 都是简单的属性映射,而查询的所有学生时则使用了关联映射 < collection/>

    < collection/>元素的解释如下:

    1. column: 表示使用id作为参数进行之后的select语句查询
    2. property: 表示返回类型 Classes 的属性名 studentList
    3. javaType: 表示该属性对应的类型名称
    4. ofType: 表示集合当中的类型
    5. select: 表示执行一条查询语句,将查询到的数据封装到 property 所代表的类型对象当中。

MainApp.java

package com.zhang.work;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zhang.pojo.Classes;
import com.zhang.pojo.Student;
import com.zhang.service.impl.StudentServiceImpl;

public class MainApp {

	public static void main(String[] args) {
		
	
	 ApplicationContext applicationContext  = new ClassPathXmlApplicationContext("applicationContext.xml");
	
	 StudentServiceImpl studentService = applicationContext.getBean("studentService", StudentServiceImpl.class);
	 
	 List<Classes> classesList = studentService.findAllClasses();
	 
	 for(Classes classes:classesList) {
		 System.out.println(classes);
	 }
	}
}

运行 MainApp 类的 main 方法,控制台显示如下:

在这里插入图片描述
可以看到,因为使用了集合映射,所以查询班级信息时对应的所有学生对象也被查询出来了。

参考文章:

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值