[MyBatis日记](5)结果集映射ResultMaps

简介: 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SunnyYoona/article/details/50664756 ResultMaps被用来将SQL SELECT语句的结果集映射到JavaBean的属性中。
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SunnyYoona/article/details/50664756
ResultMaps被用来将SQL SELECT语句的结果集映射到JavaBean的属性中。

1. 简单ResultMap

如果Student类中属性名称与数据库中对应的列名称不相同,在查询填充时不会自动填充,即属性值不会被列值填充。这是因为 MyBatis自动对JavaBean中和列名称匹配的属性进行填充。如果有属性和列名称匹配,我们可以使用ResultMap进行填充。
为了更好的演示效果,我们把ID与name属性改成与列名称不匹配的stuID和stuName,运行上一篇文章中的getStudentByID方法得到:

  ID:0   name:null   age:24   school:西安电子科技大学  
从上面可以看出ID和name两项值都没有得到填充,就是因为属性与列名称不匹配造成的。

一个映射查询结果和Student JavaBean的简单的resultMap定义如下:
 
   
<?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.sjf.mapper.StudentMapper">
<resultMap id="StudentResultMap" type="com.sjf.bean.Student" >
<id property="stuID" column="ID"/>
<result property="stuName" column="name"/>
<result property="age" column="age"/>
<result property="school" column="school"/>
</resultMap>
<select id="getStudentByID" parameterType="int" resultMap="StudentResultMap">
select * from Student where ID = #{ID}
</select>
<select id="getAllStudents" resultMap="StudentResultMap">
SELECT ID,name,age,school FROM Student
</select>
</mapper>
表示resultMap的StudentResult id值在命名空间中是唯一的,type属性是对应查询结果Java Bean的完全限定类名或者是返回类型的别名。 <result>子元素都用来将一个resultset列映射到JavaBean的一个属性中。<id>元素和<result>元素功能相同,不过用来映射唯一标示属性(一般和主键列相对应)

在<select>语句中,我们使用了resultMap属性来代替之前的resultType属性,resultMap属性引用StudentResultMap映射。当<select>语句配置了resultMap属性,MyBatis会使用resultMap中的数据库列名与对象属性之间的映射关系来填充JavaBean中的属性

2. 拓展ResultMap

我们可以从另外一个<resultMap>拓展出一个新的<resultMap>,这样,原先的属性映射就可以继承过来。
Address实体类:
 
   
package com.sjf.bean;
 
public class Address {
private int addressID;
private String country;
private String province;
private String city;
public int getAddressID() {
return addressID;
}
public void setAddressID(int addressID) {
this.addressID = addressID;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "[ ID:" + addressID + " country:" + country + " province:" + province + " city:" + city +" ]";
}
}

Student实体类:
 
   
package com.sjf.bean;
 
public class Student {
private int stuID;
private String stuName;
private int age;
private String school;
private Address address;
public Student() {
}
public Student(int ID, String name, int age, String school) {
stuID = ID;
this.stuName = name;
this.age = age;
this.school = school;
}
public int getStuID() {
return stuID;
}
public void setStuID(int stuID) {
this.stuID = stuID;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "ID:" + stuID + " name:" + stuName + " age:" + age + " school:" + school + " address:" + (address!=null ? address.toString() : "null");
}
}
我们看一下的拓展的<resultMap>的配置:
 
   
<?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.sjf.mapper.StudentMapper">
<resultMap id="StudentResultMap" type="com.sjf.bean.Student" >
<id property="stuID" column="ID"/>
<result property="stuName" column="name"/>
<result property="age" column="age"/>
<result property="school" column="school"/>
</resultMap>
<resultMap id="StudentWithAddressResultMap" type="com.sjf.bean.Student" extends="StudentResultMap">
<result property="address.addressID" column="addressID"/>
<result property="address.country" column="country"/>
<result property="address.province" column="province"/>
<result property="address.city" column="city"/>
</resultMap>
<select id="getStudentByID" parameterType="int" resultMap="StudentResultMap">
select * from Student where ID = #{ID}
</select>
<select id="getAllStudents" resultMap="StudentWithAddressResultMap">
SELECT S.ID,name,age,school,A.ID as addressID,country,province,city
FROM Student S LEFT OUTER JOIN Address A ON S.addressID = A.ID
</select>
</mapper>
id为StudentResultMap在上面我们已经介绍过了,我主要看一下id为StudentWithAdressResultMap的resultMap。这个resultMap中使用了一个extends属性,来继承我们StudentResultMap,这样 StudentWithAdressResultMap就拥有了 StudentResultMap的属性,相当于:
 
   
<resultMap id="StudentWithAddressResultMap" type="com.sjf.bean.Student">
<id property="stuID" column="ID"/>
<result property="stuName" column="name"/>
<result property="age" column="age"/>
<result property="school" column="school"/>
<result property="address.addressID" column="addressID"/>
<result property="address.country" column="country"/>
<result property="address.province" column="province"/>
<result property="address.city" column="city"/>
</resultMap>
这样就跟我们之前的resultMap基本一样了。数据库列名ID与属性stuID相匹配,列名name 与属性stuName相匹配, 列名age 与属性age相匹配, 列名school 与属性school相匹配,那么 property属性中的"address.XXX"形式是怎么回事?例如 <result property = "address.country" column = "country" /> 标示数据库列名country与address类的country属性相匹配。

如果只想映射Student数据,可以使用id为 StudentResultMap的resultMap,如下面所示:
 
    
<select id="getStudentByID" parameterType="int" resultMap="StudentResultMap">
select * from Student where ID = #{ID}
</select>

运行结果:

  ID:1   name:yoona   age:24   school:西安电子科技大学 address:null  
如果想映射Student数据 和 Address数据,可以使用id为 StudentWithAdressResultMap的resultMap,如下面所示:
 
    
<select id="getAllStudents" resultMap="StudentWithAddressResultMap">
SELECT S.ID,name,age,school,A.ID as addressID,country,province,city
FROM Student S LEFT OUTER JOIN Address A ON S.addressID = A.ID
</select>

运行结果:

所有学生信息如下:
ID:3   name:sunn   age:20   school:山东大学 address:[ ID:1   country:中国   province:山东   city:淄博 ]
ID:4   name:sun   age:21   school:山东科技大学 address:[ ID:1   country:中国   province:山东   city:淄博 ]
ID:1 name:yoona  age:24  school:西安电子科技大学 address:[ ID:2 country:中国   province:陕西   city:西安 ]
ID:2  name:sunny  age:20  school:西安电子科技大学 address:[ ID:2 country:中国  province:陕西  city:西安 ]


程序地址: 点击打开链接

参考:《Java Persistence with MyBatis 3》

下载:点击打开链接



目录
相关文章
|
6月前
|
SQL Java 数据库连接
MyBatis 的映射关系
MyBatis 核心功能之一是映射关系,支持一对一、一对多和多对多三种 ORM 映射。通过实体类与配置文件结合,开发者可灵活实现数据关联,提升数据库操作效率。
383 4
|
10月前
|
SQL XML Java
菜鸟之路Day35一一Mybatis之XML映射与动态SQL
本文介绍了MyBatis框架中XML映射与动态SQL的使用方法,作者通过实例详细解析了XML映射文件的配置规范,包括namespace、id和resultType的设置。文章还对比了注解与XML映射的优缺点,强调复杂SQL更适合XML方式。在动态SQL部分,重点讲解了`&lt;if&gt;`、`&lt;where&gt;`、`&lt;set&gt;`、`&lt;foreach&gt;`等标签的应用场景,如条件查询、动态更新和批量删除,并通过代码示例展示了其灵活性与实用性。最后,通过`&lt;sql&gt;`和`&lt;include&gt;`实现代码复用,优化维护效率。
1023 5
|
SQL XML Java
七、MyBatis自定义映射resultMap
七、MyBatis自定义映射resultMap
376 6
|
Java 数据库连接 mybatis
MyBatis篇-映射关系(1-1 1-n n-n)
本文介绍了MyBatis中四种常见关系映射的配置方法,包括一对一、一对多、多对一和多对多。**一对一**通过`resultMap`实现属性与字段的映射;**一对多**以用户-角色为例,使用`&lt;collection&gt;`标签关联集合数据;**多对一**以作者-博客为例,利用`&lt;association&gt;`实现关联;**多对多**则通过引入第三方类(如UserForDept)分别在User和Dept类中添加集合属性,并配置对应的`&lt;collection&gt;`标签完成映射。这些方法解决了复杂数据关系的处理问题,提升了开发效率。
|
XML Java 数据库连接
Mybatis映射关系
简介:本文介绍了MyBatis框架中四种常见的关系映射方式,包括一对一、一对多、多对一及多对多。一对一通过简单属性映射实现;一对多通过在主对象中添加集合属性并使用`&lt;collection&gt;`标签映射子对象集合;多对一则利用`&lt;association&gt;`标签在主对象中映射单个子对象;多对多需引入第三方类,分别在两个主对象中添加对方的集合属性,并通过`&lt;collection&gt;`标签实现映射。
269 32
|
SQL 缓存 Java
【详细实用のMyBatis教程】获取参数值和结果的各种情况、自定义映射、动态SQL、多级缓存、逆向工程、分页插件
本文详细介绍了MyBatis的各种常见用法MyBatis多级缓存、逆向工程、分页插件 包括获取参数值和结果的各种情况、自定义映射resultMap、动态SQL
【详细实用のMyBatis教程】获取参数值和结果的各种情况、自定义映射、动态SQL、多级缓存、逆向工程、分页插件
mybatis复习04高级查询 一对多,多对一的映射处理,collection和association标签的使用
文章介绍了MyBatis中高级查询的一对多和多对一映射处理,包括创建数据库表、抽象对应的实体类、使用resultMap中的association和collection标签进行映射处理,以及如何实现级联查询和分步查询。此外,还补充了延迟加载的设置和用法。
mybatis复习04高级查询 一对多,多对一的映射处理,collection和association标签的使用
|
9月前
|
Java 数据库连接 数据库
Spring boot 使用mybatis generator 自动生成代码插件
本文介绍了在Spring Boot项目中使用MyBatis Generator插件自动生成代码的详细步骤。首先创建一个新的Spring Boot项目,接着引入MyBatis Generator插件并配置`pom.xml`文件。然后删除默认的`application.properties`文件,创建`application.yml`进行相关配置,如设置Mapper路径和实体类包名。重点在于配置`generatorConfig.xml`文件,包括数据库驱动、连接信息、生成模型、映射文件及DAO的包名和位置。最后通过IDE配置运行插件生成代码,并在主类添加`@MapperScan`注解完成整合
1504 1
Spring boot 使用mybatis generator 自动生成代码插件
|
XML Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于注解的整合
本文介绍了Spring Boot集成MyBatis的两种方式:基于XML和注解的形式。重点讲解了注解方式,包括@Select、@Insert、@Update、@Delete等常用注解的使用方法,以及多参数时@Param注解的应用。同时,针对字段映射不一致的问题,提供了@Results和@ResultMap的解决方案。文章还提到实际项目中常结合XML与注解的优点,灵活使用两者以提高开发效率,并附带课程源码供下载学习。
929 0
|
前端开发 Java 数据库连接
Java后端开发-使用springboot进行Mybatis连接数据库步骤
本文介绍了使用Java和IDEA进行数据库操作的详细步骤,涵盖从数据库准备到测试类编写及运行的全过程。主要内容包括: 1. **数据库准备**:创建数据库和表。 2. **查询数据库**:验证数据库是否可用。 3. **IDEA代码配置**:构建实体类并配置数据库连接。 4. **测试类编写**:编写并运行测试类以确保一切正常。
706 2