Mybatis学习-关联查询(员工和部门一对一的关系查询association)

mybatis-config.xml文件


 

	
	
	
	
	
	





	
	
	
	
	
	
	

	

	 

	
		
		
			
			
			
			
		
	
	
	
		
		
			
			
			
			
		
	





	
	
	
	






	
	
	
	
	

员工实体类Employee package com.ldp.mybatis.bean;

import java.util.Date;

public class Employee {

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;

public Employee() {
}

public Employee(Integer id, String lastname, String email, Date birth, Date createtime, String emp_image,
		Department department) {
	super();
	this.id = id;
	this.lastname = lastname;
	this.email = email;
	this.birth = birth;
	this.createtime = createtime;
	this.emp_image = emp_image;
	this.department = department;
}

public Integer getId() {
	return id;
}

public void setId(Integer id) {
	this.id = id;
}

public String getLastname() {
	return lastname;
}

public void setLastname(String lastname) {
	this.lastname = lastname;
}

public String getEmail() {
	return email;
}

public void setEmail(String email) {
	this.email = email;
}

public Date getBirth() {
	return birth;
}

public void setBirth(Date birth) {
	this.birth = birth;
}

public Date getCreatetime() {
	return createtime;
}

public void setCreatetime(Date createtime) {
	this.createtime = createtime;
}

public String getEmp_image() {
	return emp_image;
}

public void setEmp_image(String emp_image) {
	this.emp_image = emp_image;
}

public Department getDepartment() {
	return department;
}

public void setDepartment(Department department) {
	this.department = department;
}

@Override
public String toString() {
	return "Employee [id=" + id + ", lastname=" + lastname + ", email=" + email + ", birth=" + birth
			+ ", createtime=" + createtime + ", emp_image=" + emp_image + ", department=" + department + "]";
}

}

部门的实体类department
package com.ldp.mybatis.bean;

public class Department {

private Integer id;				//部门编号	
private String departmentName;	//部门名称

public Integer getId() {
	return id;
}

public void setId(Integer id) {
	this.id = id;
}

public String getDepartmentName() {
	return departmentName;
}

public void setDepartmentName(String departmentName) {
	this.departmentName = departmentName;
}

@Override
public String toString() {
	return "Department [id=" + id + ", departmentName=" + departmentName
			+ "]";
}

}
员工和部门是一对一的关联关系:
从resultmap引入开始,一步一步的联合查询



	
	
	
	
	
	
	
	






	
	
	
	
	
	






	
	
	
	
	
	
	
	
	
	
	
		
		
		
	


 
 


	
	
	
	
	
	
	
	
	
	
	
	
	


departmentMapper.xml



测试代码缓存加载的作用: association还可以使用缓存加载(懒加载)
	前提配置:显示的指定每个我们需要更改的配置的值,即使他是默认的。防止版本更新带来的问题 	
	
	
	
@Test
	public void testResultMap() throws IOException {
		//获得sql
		SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
		SqlSession sqlSession = sqlSessionFactory.openSession(true);
		//获取接口代理类
		try {
			EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
			/*Employee reEmployee = employeeMapper.reEmployee(1);
			System.out.println(reEmployee);*/
			//分步查询
			Employee empAndDeptSept = employeeMapper.getEmpAndDeptSept(1);
			//测试懒加载,不需要查询部门信息,那么就不会进行查询
			System.out.println(empAndDeptSept.getLastname());
		} finally {
			sqlSession.close();
		}
	}

测试结果

DEBUG 04-15 18:57:03,450 ==> Preparing: select * from employee where id=? (BaseJdbcLogger.java:145)
DEBUG 04-15 18:57:03,536 > Parameters: 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 04-15 18:57:03,819 <
Total: 1 (BaseJdbcLogger.java:145)
刘东平

如果需要部门信息

//获取接口代理类

		try {
			EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
			/*Employee reEmployee = employeeMapper.reEmployee(1);
			System.out.println(reEmployee);*/
			//分步查询
			Employee empAndDeptSept = employeeMapper.getEmpAndDeptSept(1);
			//测试懒加载,不需要查询部门信息,那么就不会进行查询
			//System.out.println(empAndDeptSept.getLastname());
			//如果需要部门信息
			System.out.println(empAndDeptSept.getDepartment());
		} finally {
			sqlSession.close();
		}

测试结果
DEBUG 04-15 19:09:34,357 ==> Preparing: select * from employee where id=? (BaseJdbcLogger.java:145)
DEBUG 04-15 19:09:34,449 > Parameters: 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 04-15 19:09:34,676 <
Total: 1 (BaseJdbcLogger.java:145)
DEBUG 04-15 19:09:34,678 ==> Preparing: select * from department where id=? (BaseJdbcLogger.java:145)
DEBUG 04-15 19:09:34,679 > Parameters: 888(Integer) (BaseJdbcLogger.java:145)
DEBUG 04-15 19:09:34,682 <
Total: 1 (BaseJdbcLogger.java:145)
Department [id=888, departmentName=董事长]

你可能感兴趣的:(mybatis学习)