版权声明:本文为博主原创文章,未经博主允许不得转载。 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》
下载:点击打开链接