这篇文章我们将来学习一些 association 用法
表结构
DROP TABLE IF EXISTS `student`; CREATE TABLE `student` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(10) NOT NULL,
`gender` char(1) NOT NULL,
`major` varchar(20) NOT NULL,
`grade` char(4) NOT NULL,
`supervisor_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of student -- ---------------------------- INSERT INTO `student` VALUES ('1', 'zhangsan', '男', '计算机科学与技术', '2011', '1'); -- ---------------------------- -- Table structure for teacher -- ---------------------------- DROP TABLE IF EXISTS `teacher`; CREATE TABLE `teacher` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(10) NOT NULL,
`gender` char(1) NOT NULL,
`research_area` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of teacher -- ---------------------------- INSERT INTO `teacher` VALUES ('1', 'laowu', '男', '软件工程');
对应的java bean
public class Student {
private int id;
private String name; // 姓名
private String gender; // 性别
private String major; // 专业
private String grade; // 年级
private Teacher supervisor; //指导教师
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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public Teacher getSupervisor() {
return supervisor;
}
public void setSupervisor(Teacher supervisor) {
this.supervisor = supervisor;
}
}
public class Teacher {
private int id;
private String name; // 教师姓名
private String gender; // 教师性别
private String researchArea; // 研究领域
private List<Student> supStudents; // 指导学生
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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getResearchArea() {
return researchArea;
}
public void setResearchArea(String researchArea) {
this.researchArea = researchArea;
}
public List<Student> getSupStudents() {
return supStudents;
}
public void setSupStudents(List<Student> supStudents) {
this.supStudents = supStudents;
}
}
mapper
<?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="net.zaodk.mybatis.mapper.StudentOperationMapper">
<!-- 定义java Bean的属性与数据库的列之间的映射 -->
<resultMap type="Student" id="studentResultMap">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="gender" property="gender" />
<result column="major" property="major" />
<result column="grade" property="grade"/>
<!-- association处理has-a关系 -->
<!--注意:这种方式javaType必须指定,表示supervisor的类型是Teacher,否则会报错 -->
<association property="supervisor" javaType="Teacher">
<!-- 教师自身的属性与数据库字段的映射 -->
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
<result property="gender" column="t_gender"/>
<result property="researchArea" column="research_area"/>
</association>
</resultMap>
<!-- SQL语句中以"#{}"的形式引用参数 -->
<select id="getById" parameterType="int" resultMap="studentResultMap">
SELECT st.id,st.name,st.gender,st.major,st.grade,t.id t_id,t.name t_name,
t.gender t_gender,t.research_area
FROM student st, teacher t
WHERE st.supervisor_id = t.id
AND st.id=#{id}
</select> </mapper>
测试
public void testAssociation(){
SqlSession session = sqlSessionFactory.openSession();
StudentOperationMapper studentOperationMapper = session.getMapper(StudentOperationMapper.class);
Student student = studentOperationMapper.getById(1);
System.out.println(student.getName()+"的指导老师是:"+student.getSupervisor().getName());
}
myBatis association的两种形式
嵌套的resultMap
这种方法的本质就是把教师实体映射从association元素中提取出来,用一个resultMap元素表示。然后association元素再引用这个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="net.zaodk.mybatis.mapper.StudentOperationMapper">
<!-- 定义java Bean的属性与数据库的列之间的映射 -->
<resultMap type="Teacher" id="teacherResultMap">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
<result property="gender" column="t_gender"/>
<result property="researchArea" column="research_area"/>
</resultMap>
<resultMap type="Student" id="studentResultMap">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="gender" property="gender" />
<result column="major" property="major" />
<result column="grade" property="grade"/>
<!-- 引用teacherResultMap -->
<association property="supervisor" resultMap="teacherResultMap"/>
</resultMap>
<!-- SQL语句中以"#{}"的形式引用参数 -->
<select id="getById" parameterType="int" resultMap="studentResultMap">
SELECT st.id,st.name,st.gender,st.major,st.grade,t.id t_id,t.name t_name,
t.gender t_gender,t.research_area
FROM student st, teacher t
WHERE st.supervisor_id = t.id
AND st.id=#{id}
</select> </mapper>
嵌套的select语句
这种方式是使用一条单独的select语句来加载关联的实体(本例中就是教师实体),然后在association元素中引用此select语句(注:此方法会产生N+1问题,尽量不要用嵌套的select语句)
<?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="net.zaodk.mybatis.mapper.StudentOperationMapper">
<!-- 定义java Bean的属性与数据库的列之间的映射 -->
<resultMap type="Teacher" id="supervisorResultMap">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
<result property="gender" column="t_gender"/>
<result property="researchArea" column="research_area"/>
</resultMap>
<resultMap type="Student" id="studentResultMap">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="gender" property="gender" />
<result column="major" property="major" />
<result column="grade" property="grade"/>
<!-- 引用teacherResultMap -->
<association property="supervisor" column="supervisor_id" select="selectSupervisor"/>
</resultMap>
<!-- SQL语句中以"#{}"的形式引用参数 -->
<select id="getById" parameterType="int" resultMap="studentResultMap">
select id,name,gender,major,grade,supervisor_id from student where id =#{id}
</select>
<select id="selectSupervisor" parameterType="int" resultMap="supervisorResultMap">
select id,name,gender,research_area
from teacher where id = #{id}
</select> </mapper>