Employee类
public class Employee {
private int id;
private String name;
private int age;
private String sex;
private Department department;
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public Employee() {
super();
// TODO Auto-generated constructor stub
}
public Employee(String name, int age, String sex) {
super();
this.name = name;
this.age = age;
this.sex = sex;
}
public Employee(int id, String name, int age, String sex) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age
+ ", sex=" + sex + "]";
}
}
EmployeeDao
public interface EmployeeDao {
List<Employee> getEmployees() throws Exception;
Employee getEmployeeById(int id) throws Exception;
}
EmployeeDao的映射文件
<?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.hsj.dao.EmployeeDao">
<resultMap type="employee" id="resultMap_Employee">
<id column="e_id" property="id"/>
<result column="e_name" property="name"/>
<result column="e_age" property="age"/>
<result column="e_sex" property="sex"/>
**<!--
映射实体类中普通的POJO对象时使用的标签
property="department":指定复杂属性的名字
column="depart_id":指定外键字段的名字
javaType="department":复杂属性的数据类型
-->**
<**association** property="department" column="depart_id" javaType="department">
<id column="d_id" property="id"/>
<result column="d_name" property="name"/>
</association>
</resultMap>
<!-- 查询所有雇员和部门信息 -->
<select id="getEmployees" resultMap="resultMap_Employee">
select * from t_employee e inner join t_department d on e.depart_id=d.d_id
</select>
<!-- 根据雇员编号好像雇员及其部门信息 -->
<select id="getEmployeeById" parameterType="int" resultMap="resultMap_Employee">
select * from t_employee e inner join t_department d on e.depart_id=d.d_id and e_id=#{id}
</select>
</mapper>
Department类
public class Department {
private int id;
private String name;
private Set<Employee> employees;
public Set<Employee> getEmployees() {
return employees;
}
public void setEmployees(Set<Employee> employees) {
this.employees = employees;
}
public Department() {
super();
// TODO Auto-generated constructor stub
}
public Department(int id, String name) {
super();
this.id = id;
this.name = name;
}
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;
}
@Override
public String toString() {
return "Department [id=" + id + ", name=" + name + "]";
}
}
DepartmentDao
public interface DepartmentDao {
List<Department> getDepartments() throws Exception;
Department getDepartmentById(int id) throws Exception;
}
DepartmentDao的映射文件
<?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.hsj.dao.DepartmentDao">
<resultMap type="department" id="resultMap_Department">
<id column="d_id" property="id"/>
<result column="d_name" property="name"/>
**<!--
如果映射的复杂属性是集合,则可以使用collection标签来映射
property="employees":复杂属性的名字
column="depart_id":指定外键字段的名字
ofType="employee":指定集合中元素的数据类型
-->**
<collection property="employees" column="depart_id" ofType="employee">
<id column="e_id" property="id"/>
<result column="e_name" property="name"/>
<result column="e_age" property="age"/>
<result column="e_sex" property="sex"/>
</collection>
</resultMap>
<select id="getDepartments" resultMap="resultMap_Department">
select * from t_department d inner join t_employee e on d.d_id=e.depart_id
</select>
<select id="getDepartmentById" resultMap="resultMap_Department">
select * from t_department d inner join t_employee e on d.d_id=e.depart_id and d_id=#{id}
</select>
</mapper>
其中一个测试类
public class DepartmentDaoTest {
private DepartmentDao departmentDao;
@Before
public void setUp() throws Exception {
SqlSession sqlSession=MyBatisUtils.getSqlSession();
this.departmentDao=sqlSession.getMapper(DepartmentDao.class);
}
@Test
public void testGetDepartments() {
try {
List<Department> departments=this.departmentDao.getDepartments();
for(Department department:departments){
System.out.println(department);
System.out.println("雇员信息:");
for(Employee e :department.getEmployees()){
System.out.println(e);
}
System.out.println("============");
}
System.out.println();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testGetDepartmentById() {
int id=1;
try {
Department department=this.departmentDao.getDepartmentById(id);
System.out.println(department);
System.out.println("雇员信息:");
for(Employee e:department.getEmployees()){
System.out.println(e);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}