1、两个实体类(部门表、员工表)一个部门包含许多员工(一对多关系),将员工表信息写到部门实体类中
点击查看代码
package com.bh.po;
import java.util.Set;
public class Dept {
private int deptno;
private String dname;
private String loc;
//因为一个部门有很多员工所以用集合,因为员工不能有重复数据所以用Set集合
private Set<Emp> emps;
public Set<Emp> getEmps() {
return emps;
}
public void setEmps(Set<Emp> emps) {
this.emps = emps;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
点击查看代码
package com.bh.po;
public class Emp {
private int empno;
private String ename;
private String job;
private String salary;
private int deptno;
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getSalary() {
return salary;
}
public void setSalary(String salary) {
this.salary = salary;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
}
点击查看代码
package com.bh.dao;
import com.bh.po.Dept;
import java.util.List;
public interface IDeptDAO {
public List<Dept> findAllWithEmp();
}
点击查看代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bh.dao.IDeptDAO">
<resultMap id="dept" type="com.bh.po.Dept">
<!--第一个实体类对应表-->
<id property="deptno" column="deptno"/>
<result property="dname" column="dname"/>
<result property="loc" column="loc"/>
<!--第二个实体类对应表-->
<!--一对多用集合类型使用collection标签--><!--javaType这里数据类型为Set集合--><!--ofType表示集合里面的数据类型-->
<collection property="emps" javaType="java.util.Set" ofType="com.bh.po.Emp" column="deptno"><!--column表示两表关联字段-->
<id property="empno" column="empno"/>
<result property="ename" column="ename"/>
<result property="job" column="job"/>
<result property="salary" column="salary"/>
<result property="deptno" column="deptno"/>
</collection>
</resultMap>
<select id="findAllWithEmp" resultMap="dept">
select a1.*, a2.* from dept a1 inner join emp a2 on a1.deptno = a2.deptno
</select>
</mapper>
点击查看代码
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--DB的四大步骤-->
<properties>
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/class?characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</properties>
<environments default="development">
<environment id="development">
<transactionManager type="jdbc"></transactionManager>
<dataSource type="POOLED">
<property value="${driver}" name="driver"/>
<property value="${url}" name="url"/>
<property value="${username}" name="username"/>
<property value="${password}" name="password"/>
</dataSource>
</environment>
</environments>
<!--导入mapper-->
<mappers>
<!--<mapper resource="mapper/StudentMapper.xml"/>-->
<!--<mapper resource="mapper/Student1Mapper.xml"/>-->
<mapper resource="mapper/DeptMapper.xml"/>
<!-- <mapper resource="mapper/TableMapper.xml" />-->
</mappers>
</configuration>
点击查看代码
package com.bh.Test;
import com.bh.dao.IDeptDAO;
import com.bh.dao.IStudent1DAO;
import com.bh.dao.IStudentDAO;
import com.bh.po.Dept;
import com.bh.po.Emp;
import com.bh.po.Student;
import com.bh.po.Student1;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
import java.util.List;
import java.util.Set;
public class Test {
public static void main(String[] args) {
System.out.println("start--------");
//读取配置文件
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("mybatis.xml");
//获取工厂的builder
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//创建工厂
SqlSessionFactory sqlSessionFactory = builder.build(in);
//创建mybatis的sqlSqlsession
SqlSession sqlSession = sqlSessionFactory.openSession();
/***********************************单表*********************************************/
//IStudentDAO studentDAO = sqlSession.getMapper(IStudentDAO.class);
/* List<Student> all = studentDAO.findAll();
for (int i = 0; i < all.size(); i++) {
Student stu = all.get(i);
System.out.println(stu.getId() + "_" + stu.getName());
}*/
//Student stu = new Student();
//stu.setName("kuan");
//stu.setId(23);
/*studentDAO.save(stu);*/
/*studentDAO.modify(stu);*/
// studentDAO.remove(stu);
//sqlSession.commit();
//System.out.println("end---------");
/***********************************多表联查一对一*********************************************/
/* IStudent1DAO student1DAO = sqlSession.getMapper(IStudent1DAO.class);
List<Student1> all = student1DAO.findAll();
for (Student1 stu:all) {
System.out.println(stu.getStuid() +"----"+ stu.getStuname()+"----"+stu.getSd().getStuaddress()+"---"+stu.getSd().getStuage());
}
System.out.println("end---------");*/
/***********************************多表联查一对多*********************************************/
IDeptDAO mapper = sqlSession.getMapper(IDeptDAO.class);
List<Dept> all = mapper.findAllWithEmp();
for (Dept dep:all) {
Set<Emp> emps = dep.getEmps();
for (Emp em:emps) {
System.out.println(em.getDeptno()+"---"+em.getEmpno()+"---"+em.getEname()+"---"+em.getJob()+"---"+dep.getDname());
}
}
}
}