MyBatis框架使用接口多对一联合查询方式

Client.java

package client;

import java.io.*;
import java.util.List;
import org.apache.ibatis.io.*;
import org.apache.ibatis.session.*;
import domain.Employee;
import domain.UserMapper;

public class Client {
	public static void main(String args[]) throws IOException {
		InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
		SqlSession sqlSession = factory.openSession();		
		UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
		List list = userMapper.findAllEmployees();
		System.out.println("查询所有员工信息(使用接口多对一联合查询方式):");
		for(Employee emp:list)
			System.out.println("Employee's id: "+emp.getEmpId()+" Employee's name: "+emp.getName()+" Employee's detpId: "+emp.getDeptId()+" Employee's deptName: "+emp.getDepts().getDeptName());
		sqlSession.close();
	}
}

Department.java

package domain;

public class Department {
	private int deptId;
	private String deptName;
	public int getDeptId() {
		return deptId;
	}
	public void setDeptId(int deptId) {
		this.deptId = deptId;
	}
	public String getDeptName() {
		return deptName;
	}
	public void setDeptName(String deptName) {
		this.deptName = deptName;
	}
}

Employee.java

package domain;

public class Employee {
	private int empId;
	private String name;
	private int deptId;
	private Department depts;
	public int getEmpId() {
		return empId;
	}
	public void setEmpId(int empId) {
		this.empId = empId;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getDeptId() {
		return deptId;
	}
	public void setDeptId(int deptId) {
		this.deptId = deptId;
	}
	public Department getDepts() {
		return depts;
	}
	public void setDepts(Department depts) {
		this.depts = depts;
	}
}

UserMapper.java

package domain;

import java.util.List;

public interface UserMapper {
	List findAllEmployees();
}

UserMapper.xml




	
		
		
		
		
			
			
		
	
	


department表:
MyBatis框架使用接口多对一联合查询方式_第1张图片
employee表:
MyBatis框架使用接口多对一联合查询方式_第2张图片
查询结果:
MyBatis框架使用接口多对一联合查询方式_第3张图片

你可能感兴趣的:(My,SQL,MyBatis,mybatis,接口,xml)