private Integer id; //部门编号
private String departmentName; //部门名称
private List employees;//部门和员工是多对一的关系
..
private Integer id;
// 不能被修改
private String lastname;
private String email;
// 从前端传入的是 String 类型, 所以需要注意转换
private Date birth;
// 不能被修改
private Date createtime;
//添加头像
private String emp_image;
// 单向 n-1 的关联关系
private Department department;
..
/**
* 测试多对一
* @throws IOException
*/
@Test
public void test01() throws IOException {
//首先获得SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
//获得sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession(true);
try {
DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class);
//进行查询
Department department = departmentMapper.getDeptByidPlus(1);
System.out.println(department);
List list=department.getEmployees();
for (Employee employee : list) {
System.out.println(employee);
}
} finally {
sqlSession.close();
}
}
测试的结果
DEBUG 04-15 19:38:54,444 ==> Preparing: SELECT d.`id`, d.`departmentName`, e.`id` eid, e.`birth`, e.`createtime`, e.`department_id`, e.`email`, e.`emp_image`, e.`lastname` FROM `department` d LEFT JOIN employee e ON d.`id`=e.`department_id` WHERE d.`id`=?; (BaseJdbcLogger.java:145)
DEBUG 04-15 19:38:54,542 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 04-15 19:38:54,592 <== Total: 4 (BaseJdbcLogger.java:145)
Department [id=1, departmentName=行政部]
Employee [id=7, lastname=刘东平7, [email protected], birth=Wed Mar 27 00:00:00 CST 2019, createtime=Wed Mar 27 20:01:59 CST 2019, emp_image=C:/upload/14/2/f0312f6e638248058eaebae3f4d8952f.jpg, department=Department [id=1, departmentName=null]]
Employee [id=201, lastname=杨峰, [email protected], birth=Mon Apr 15 11:44:26 CST 2019, createtime=Mon Apr 15 11:34:26 CST 2019, emp_image=null, department=Department [id=1, departmentName=null]]
Employee [id=202, lastname=杨峰, [email protected], birth=Mon Apr 15 14:40:48 CST 2019, createtime=Mon Apr 15 14:40:48 CST 2019, emp_image=null, department=Department [id=1, departmentName=null]]
Employee [id=203, lastname=杨峰, [email protected], birth=Mon Apr 15 14:42:17 CST 2019, createtime=Mon Apr 15 14:42:17 CST 2019, emp_image=null, department=Department [id=1, departmentName=null]]
com.ldp.mybatis.dao.EmployeeMapper 下面定义一个根据部门id查询员工信息的配置信息
在com.ldp.mybatis.dao.DepartmentMapper.xml 下面的定义信息
/**
* 测试多对一(利用分步查询的方式)
* @throws IOException
*/
@Test
public void test02() throws IOException {
//首先获得SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
//获得sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession(true);
try {
DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class);
//进行查询
Department department = departmentMapper.getDeptBySept(1);
System.out.println(department);
List list=department.getEmployees();
for (Employee employee : list) {
System.out.println(employee);
}
} finally {
sqlSession.close();
}
}
查询结果:会有两条SQL语句
DEBUG 04-15 20:02:35,029 ==> Preparing: select * from department where id=? (BaseJdbcLogger.java:145)
DEBUG 04-15 20:02:35,119 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 04-15 20:02:35,441 <== Total: 1 (BaseJdbcLogger.java:145)
DEBUG 04-15 20:02:35,444 ==> Preparing: select * from employee where department_id=?; (BaseJdbcLogger.java:145)
DEBUG 04-15 20:02:35,446 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 04-15 20:02:35,456 <== Total: 4 (BaseJdbcLogger.java:145)
Department [id=1, departmentName=行政部]
Employee [id=7, lastname=刘东平7, [email protected], birth=Wed Mar 27 00:00:00 CST 2019, createtime=Wed Mar 27 20:01:59 CST 2019, emp_image=C:/upload/14/2/f0312f6e638248058eaebae3f4d8952f.jpg, department=null]
Employee [id=201, lastname=杨峰, [email protected], birth=Mon Apr 15 11:44:26 CST 2019, createtime=Mon Apr 15 11:34:26 CST 2019, emp_image=null, department=null]
Employee [id=202, lastname=杨峰, [email protected], birth=Mon Apr 15 14:40:48 CST 2019, createtime=Mon Apr 15 14:40:48 CST 2019, emp_image=null, department=null]
Employee [id=203, lastname=杨峰, [email protected], birth=Mon Apr 15 14:42:17 CST 2019, createtime=Mon Apr 15 14:42:17 CST 2019, emp_image=null, department=null]
collection也有缓存加载(也称按需加载)
前提条件需要在配置文件中配置以下信息
将测试代码修改为
DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class);
//进行查询
Department department = departmentMapper.getDeptBySept(1);
System.out.println(department.getDepartmentName());
查询的结果只有一条SQL语句
DEBUG 04-15 20:08:02,050 ==> Preparing: select * from department where id=? (BaseJdbcLogger.java:145)
DEBUG 04-15 20:08:02,168 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 04-15 20:08:02,414 <== Total: 1 (BaseJdbcLogger.java:145)
行政部
column{key1=“id”,key=“lasename”}
fetchType=“lazy” 表示使用延迟加载
lazy:表示延迟加载
eager:表示即使加载