resultMap是mybatis中最复杂的元素之一,它描述如何从结果集中加载对象,主要作用是定义映射规则、级联的更新、定制类型转化器。
我们就以学生跟班级的关系来测试
学生表:
班级表:
1.创建一个学生实体类
package cn.et.demo03.xml.model;
public class Student {
private String id;
private String name;
private Grade grade;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
}
2.创建一个班级类
package cn.et.demo03.xml.model;
import java.util.ArrayList;
import java.util.List;
public class Grade {
private String id;
private String name;
private List list =new ArrayList();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
在后台的JavaBean中,如果遵守规范的话,属性名和列名一致,那么我们就不需要手动做字段映射,MyBatis会自动帮我们把值填充到Bean中。但现在情况不一样,Bean的属性名和数据库列名对应不上。所以我们需要自己去映射
用xml的方式实现多对一的例子
id : 唯一标识,一般用来标识id
column: 用来标识数据库的字段名
property: 用来标识实体的字段名
association: 代表一对多的关系
selele: 用来标识sql语句所在的位置
写一个测试类
package cn.et.demo03.xml.controller;
import cn.et.demo03.xml.mapper.GradeMapper;
import cn.et.demo03.xml.mapper.StudentMapper;
import cn.et.demo03.xml.model.Grade;
import cn.et.demo03.xml.model.Student;
import cn.et.tools.DBTools;
import java.io.IOException;
public class TestController {
public static void main(String[] args) throws IOException {
// 一对多的例子
Grade grade =OneToMany();
System.out.println("班级名称:"+grade.getName());
for (int i=0; i
结果:
用xml的方式实现一对多的例子
collection:用来标识一对多的关系
javaType:用来指定返回的类型
select: 用来标识sql语句的路径
编写一个测试类
package cn.et.demo03.xml.controller;
import cn.et.demo03.xml.mapper.GradeMapper;
import cn.et.demo03.xml.mapper.StudentMapper;
import cn.et.demo03.xml.model.Grade;
import cn.et.demo03.xml.model.Student;
import cn.et.tools.DBTools;
import java.io.IOException;
public class TestController {
public static void main(String[] args) throws IOException {
// 多对一的例子
Student student = ManyToOne();
System.out.println(student.getName()+"===="+student.getGrade().getName());
}
/**
* 多对一的例子
* @return
* @throws IOException
*/
public static Student ManyToOne() throws IOException {
StudentMapper studentMapper = DBTools.getSession().getMapper(StudentMapper.class);
Student student =studentMapper.ManyToOneById("11");
return student;
}
}
效果:
package cn.et.demo03.annotation.model;
import java.util.ArrayList;
import java.util.List;
public class Grade {
private String id;
private String name;
private List list =new ArrayList();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
package cn.et.demo03.annotation.model;
public class Student {
private String id;
private String name;
private Grade grade;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
}
package cn.et.demo03.annotation.mapper;
import cn.et.demo03.annotation.model.Grade;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface GradeMapper {
/**
* 演示一对多的接口
* @param id
* @return
*/
@Results({
@Result(property = "id",column = "gid"),
@Result(property = "name",column = "gname"),
@Result(property = "list",column ="gid",javaType = List.class, many = @Many(select = "cn.et.demo03.annotation.mapper.StudentMapper.studentByGid"))
})
@Select("select * from grade where gid =#{id}")
Grade oneToMany(@Param("id") String id);
}
package cn.et.demo03.annotation.mapper;
import cn.et.demo03.annotation.model.Student;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface StudentMapper {
/**
* 演示一对多
* @param id
* @return
*/
@Results({
@Result(property = "id",column = "sid"),
@Result(property = "name",column = "sname")
})
@Select("select * from student where gid=#{id}")
List studentByGid(@Param("id") String id);
}
package cn.et.demo03.annotation.mapper;
import cn.et.demo03.annotation.model.Student;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface StudentMapper {
/**
* 演示多对一
* @param id
* @return
*/
@Results({
@Result(property = "id",column = "sid"),
@Result(property = "name",column = "sname"),
@Result(property = "grade",column = "gid",one = @One(select = "cn.et.demo03.annotation.mapper.GradeMapper.gradeById"))
})
@Select("select * from student where sid= #{id}")
Student ManyToOneById(@Param("id") String id);
}
package cn.et.demo03.annotation.mapper;
import cn.et.demo03.annotation.model.Grade;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface GradeMapper {
/**
* 演示多对一的接口
* 通过班级id查找班级详细信息
* @param id
* @return
*/
@Results({
@Result(property = "id",column = "gid"),
@Result(property = "name",column = "gname")
})
@Select("select * from grade where gid= #{id}")
Grade gradeById(@Param("id") String id);
}