MyBatis的collection集合的分布查询
DepartmentMapper.java
package com.cn.mybatis.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Param;
import com.cn.zhu.bean.Department;
import com.cn.zhu.bean.Employee;
public interface DepartmentMapper {
public Department getDeptById(Integer id);
public Department getDeptByIdPlus(Integer id);
public Department getDeptByIdStep(Integer id);
public List getEmpsByDeptId(Integer deptId);
}
EmployeeMapperPlus.java
1. package com.cn.mybatis.dao;
2.
3. import java.util.List;
4. import java.util.Map;
5.
6.
7. import org.apache.ibatis.annotations.MapKey;
8. import org.apache.ibatis.annotations.Param;
9.
10. import com.cn.zhu.bean.Employee;
11.
12. public interface EmployeeMapperPlus {
13. public Employee getEmpById(Integer id);
14. public Employee getEmpAndDept(Integer id);
15. public Employee getEmpByIdStep(Integer id);
16. }
Department.java
1. package com.cn.zhu.bean;
2.
3. import java.util.List;
4.
5. public class Department {
6. private Integer id;
7. private String departmentName;
8. private List emps;
9.
10.
11.
12. public List getEmps() {
13. return emps;
14. }
15. public void setEmps(List emps) {
16. this.emps = emps;
17. }
18. public Integer getId() {
19. return id;
20. }
21. public void setId(Integer id) {
22. this.id = id;
23. }
24. public String getDepartmentName() {
25. return departmentName;
26. }
27. public void setDepartmentName(String departmentName) {
28. this.departmentName = departmentName;
29. }
30. @Override
31. public String toString() {
32. return "Department [departmentName=" + departmentName + ", id=" + id
33. "]";
34. }
35.
36. }
DepartmentMapper.xml
select id,dept_name
departmentName from tbl_dept where id=#{id}
select id,dept_name departmentName from tbl_dept where id=#{id}
EmployeeMapperPlus.xml
select * from tbl_employee where d_id=#{deptId}
mybatis-config.xml
1.
2.
3.
4.