DepartmentDao 接口
public interface DepartmentDao {
@Select("select * from t_department")
@Results({
@Result(column="d_id",property="id"),
@Result(column="d_name",property="name"),
@Result(column="d_id",property="employees",
javaType=Set.class,many=@Many(select="getEmployeeById")
)
})
List<Department> getDepartments() throws Exception;
@Select("select * from t_employee where depart_id=#{id}")
@Results({
@Result(column="e_id",property="id"),
@Result(column="e_name",property="name"),
@Result(column="e_sex",property="sex"),
@Result(column="e_age",property="age"),
})
Employee getEmployeeById(int id) throws Exception;
@Select("select * from t_department where d_id=#{id}")
@Results({
@Result(column="d_id",property="id"),
@Result(column="d_name",property="name"),
@Result(column="d_id",property="employees",
javaType=Set.class,many=@Many(select="getEmployeeById")
)
})
Department getDepartmentById(int id) throws Exception;
}
EmployeeDao 接口
public interface EmployeeDao {
@Select("select * from t_employee")
@Results({
@Result(column="e_id",property="id"),
@Result(column="e_name",property="name"),
@Result(column="e_age",property="age"),
@Result(column="e_sex",property="sex"),
@Result(column="depart_id",property="department",
javaType=Department.class,
one=@One(select="getDepartmentById")
)
})
List<Employee> getEmployees() throws Exception;
@Select("select * from t_department where d_id=#{id}")
@Results({
@Result(column="d_id",property="id"),
@Result(column="d_name",property="name")
})
public Department getDepartmentById(int id) throws Exception;
@Select("select * from t_employee where e_id=#{id}")
@Results({
@Result(column="e_id",property="id"),
@Result(column="e_name",property="name"),
@Result(column="e_age",property="age"),
@Result(column="e_sex",property="sex"),
@Result(column="depart_id",property="department",
javaType=Department.class,
one=@One(select="getDepartmentById")
)
})
Employee getEmployeeById(int id) throws Exception;
}
本文详细介绍了MyBatis框架中一对多和多对一的关联映射实现方式,通过具体的DAO接口代码示例,展示了如何使用@Select、@Result、@Many和@One注解来实现部门与员工之间的数据关联查询。
472

被折叠的 条评论
为什么被折叠?



