这里仍然以系表(department)和学生表(student)为例来讲解。系和学生是典型的一对多的关系。
一对多关联我们使用的是collection 标签。collection 标签如何使用,下面我们来看具体的案例。
========================StudentMapper.xml====================
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"mybatis-3-mapper.dtd">
<mapper namespace="stu">
<!-- 单张表的映射 -->
<!-- autoMapping="true":对于列名和实体类的属性名一致,那么我们可以省略相应的映射关系 -->
<resultMap id="stuMap" type="StudentEntity" autoMapping="true">
<!--id不要省-->
<id property="stuId" column="stuId"/>
</resultMap>
</mapper>
==========================DeptEntity.java=======================
package com.obtk.entitys;
import java.util.*;
public class DeptEntity implements java.io.Serializable {
private static final long serialVersionUID = -2856143193567492289L;
private int deptId;
private String deptName; //注意和列名不同
private String address;
//体现一对多的关联关系
private List<StudentEntity> stuList=new ArrayList<StudentEntity>();
public DeptEntity() {
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public String getDeptName() {
return deptName;
}
public int getDeptId() {
return this.deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
public void setStuList(List<StudentEntity> stuList) {
this.stuList = stuList;
}
public List<StudentEntity> getStuList() {
return stuList;
}
}
======================DeptMapper.xml==========================
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"mybatis-3-mapper.dtd">
<mapper namespace="dept">
<!-- 单张表的映射 -->
<resultMap id="deptMap" type="DeptEntity" autoMapping="true">
<!--id不要省-->
<id property="deptId" column="deptId"/>
<result property="deptName" column="departName"/>
</resultMap>
<!-- 一对多关联 -->
<resultMap id="deptJoinStu" type="DeptEntity" autoMapping="true">
<!--id不要省-->
<id property="deptId" column="deptId"/>
<result property="deptName" column="departName"/>
<!-- javaType是指关联属性多对应的类型,ofType是指arraylist里面所装数据的类型 -->
<!-- resultMap是指关联表的映射类型 -->
<collection property="stuList" javaType="arraylist"
ofType="StudentEntity" resultMap="stu.stuMap">
</collection>
</resultMap>
<select id="selectByDeptStu" parameterType="int" resultMap="deptJoinStu">
select * from department d inner join student s
on d.deptId= s.deptIdd
where d.deptId=#{deptId}
</select>
</mapper>
测试代码:
package com.obtk.test2;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.obtk.entitys.DeptEntity;
import com.obtk.entitys.StudentEntity;
import com.obtk.utils.MybatisUtil;
public class TestOneToMany01 {
public static void main(String[] args) {
SqlSession session=null;
try {
//1.得到session
session=MybatisUtil.getSession();
DeptEntity dept=session.selectOne("dept.selectByDeptStu", 10);
List<StudentEntity> stuList=dept.getStuList();
for(StudentEntity stu : stuList){
System.out.println(stu.getStuName()+","+stu.getGender()
+","+dept.getDeptName()+","+dept.getAddress());
}
} catch (Exception e) {
e.printStackTrace();
}finally{
MybatisUtil.closeSession();
}
}
}