Mybatis之resultMap详解

本文详细介绍了在MyBatis框架中使用resultMap的两种常见场景:当类字段与数据库字段不匹配时,如何通过resultMap进行映射;以及在属性为另一实体类对象时,如何利用resultMap实现一对多的关系映射。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需要使用resultMap的两种情况

  1. 如果类中的字段名和数据库中的字段名不相符是,需要是使用resultMap (id-stuno)
  2. 如果类中的属性和数据库中的属性不相符时,需要是使用resultMap(String-VarChar2)

配置流程(第一种情况id-stuno)

所需要用到的jar包   mybatis-3.5.1.jar 和 mysql-connector-java-5.1.0-bin.jar

1.配置mybatis-config.xml

创建mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
        <!-- 资源配置 -->
	<properties resource="database.properties"></properties>

	<!-- 别名配置 -->
	<typeAliases>
		<!--批量设置 默认将包中的所有实体类设置别名,类名小写即别名 -->
		<package name="com.zy.pojo" />
	</typeAliases>

	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="${driver}" />
				<property name="url" value="${url}" />
				<property name="username" value="${username}" />
				<property name="password" value="${password}" />
			</dataSource>
		</environment>
	</environments>

        <!-- mapper配置 -->
	<mappers>
		<mapper resource="com/zy/mapper/StudentMapper.xml" />
	</mappers>
</configuration>

2.创建类--表映射

创建Student.java类

package com.zy.pojo;

public class Student {
	private Integer id;
	private String stuName;
	private String stuClass;
	private int stuAge;
	private boolean stuSex;
	private StudentAddress stuAddress;
	
	public Student() {
		super();
	}

	public Integer getId() {
		return id;
	}

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

	public String getStuName() {
		return stuName;
	}

	public void setStuName(String stuName) {
		this.stuName = stuName;
	}

	public String getStuClass() {
		return stuClass;
	}

	public void setStuClass(String stuClass) {
		this.stuClass = stuClass;
	}

	public int getStuAge() {
		return stuAge;
	}

	public void setStuAge(int stuAge) {
		this.stuAge = stuAge;
	}

	public boolean isStuSex() {
		return stuSex;
	}

	public void setStuSex(boolean stuSex) {
		this.stuSex = stuSex;
	}

	public StudentAddress getStuAddress() {
		return stuAddress;
	}

	public void setStuAddress(StudentAddress stuAddress) {
		this.stuAddress = stuAddress;
	}

	public Student(Integer id, String stuName, String stuClass, int stuAge, boolean stuSex) {
		super();
		this.id = id;
		this.stuName = stuName;
		this.stuClass = stuClass;
		this.stuAge = stuAge;
		this.stuSex = stuSex;
	}

	public Student(Integer id, String stuName, String stuClass, int stuAge, boolean stuSex, StudentAddress stuAddress) {
		super();
		this.id = id;
		this.stuName = stuName;
		this.stuClass = stuClass;
		this.stuAge = stuAge;
		this.stuSex = stuSex;
		this.stuAddress = stuAddress;
	}

		@Override
	public String toString() {
		return "Student [id=" + id + ", stuName=" + stuName + ", stuClass=" + stuClass + ", stuAge=" + stuAge
				+ ", stuSex=" + stuSex + ", stuAddress=" + stuAddress + "]";
	}

	
	
	
}

创建StudentAddress.java类

package com.zy.pojo;

public class StudentAddress {
	private String homeAddress;
	private String schoolAddress;
	public String getHomeAddress() {
		return homeAddress;
	}
	public void setHomeAddress(String homeAddress) {
		this.homeAddress = homeAddress;
	}
	public String getSchoolAddress() {
		return schoolAddress;
	}
	public void setSchoolAddress(String schoolAddress) {
		this.schoolAddress = schoolAddress;
	}
	
	public StudentAddress(String homeAddress, String schoolAddress) {
		super();
		this.homeAddress = homeAddress;
		this.schoolAddress = schoolAddress;
	}
	public StudentAddress() {
		super();
	}
	
	@Override
	public String toString() {
		return "StudentAddress [homeAddress=" + homeAddress + ", schoolAddress=" + schoolAddress + "]";
	}
	
}

3.创建mapper接口

创建StudentMapper.java接口

package com.zy.mapper;

import java.util.List;

import com.zy.pojo.Student;

public interface StudentMapper {
	List<Student> queryAllStudent();
}

4.创建mapper映射文件

创建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.zy.mapper.StudentMapper">
        <!--查询学生全部信息-->
	<select id="queryAllStudent" resultMap="studentAddressMap">
		select * from Student
	</select>
        <!-- 这里的类中id和数据库中的字段stuNo不一致 所以使用resultMap -->
	<resultMap type="student" id="studentAddressMap">
		<!-- 主键用id 非主键用result property代表着实体类中,column代表着数据库中的字段 -->
		<id property="id" column="stuNo" />
		<result property="stuName" column="stuName" />
		<result property="stuClass" column="stuClass" />
		<result property="stuAge" column="stuAge" />
		<result property="stuSex" column="stuSex" />
		<!-- 一对多 -->
		<collection property="stuAddress" ofType="com.zy.pojo.StudentAddress">
			<result property="homeAddress" column="homeAddress" />
			<result property="schoolAddress" column="schoolAddress" />
		</collection>
	</resultMap>
</mapper>

 

5.测试类

创建测试类StudentTest.java

package com.zy.test;

import java.io.IOException;
import java.io.Reader;
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 com.zy.mapper.StudentMapper;
import com.zy.pojo.Student;
import com.zy.pojo.StudentAddress;

public class StudentTest {
	public static void main(String[] args) throws IOException {
		queryAllStudent() ;

	}
	
//	全部查询(使用了类型转换器)
	private static void queryAllStudent() throws IOException {
		Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
		
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
		SqlSession sqlSession = factory.openSession();
		
		List<Student> allStudent = sqlSession.getMapper(StudentMapper.class).queryAllStudent();
		
		for (Student student : allStudent) {
			System.out.println(student);
		}
		
		sqlSession.close();
	}
}

6.运行结果

运行结过

7.项目结构

项目结构

9.数据库中的字段

分析一下:

  • 类中的属性名id和数据库中的字段stuNo名不一致,需要使用resultMap将它们对应起来

  • 查询的类属性是另外一个实体类对象,可以使用resultMap的一对多

第二种情况的可以使用类型转换器更好一点Mybatis之类型转换器的配置

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值