这里有两张Emp数据表、Dept数据表
雇员表:Emp(empno,ename,job,deptno)
部门表:Dept(deptno,dname,loc)
一个雇员对应着一个部门,是1对1的关系;
而反过来看,一个部门对应着多个雇员,是1对多的关系;
association: 一对一关联(has one)
collection:一对多关联(has many)
1对1的关系
一个雇员对应着一个部门,是1对1的关系;
一个Emp对象里有一个Dept对象
Pojo类
Emp
package com.zsl.pojo;
import java.util.List;
public class Emp {
private Integer empno;
private String ename;
private String job;
private Dept dept;
public Integer getEmpno() {
return empno;
}
public void setEmpno(Integer 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 Emp() {
super();
// TODO Auto-generated constructor stub
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public Emp(Integer empno, String ename, String job) {
super();
this.empno = empno;
this.ename = ename;
this.job = job;
}
@Override
public String toString() {
return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job + "]";
}
}
EmpMapper接口
public interface EmpMapper {
public List<Emp> queryE();
}
EmpMapper.xml映射文件
<resultMap type="emp" id="baseMap">
<!-- 主表数据 -->
<id column="empno" property="empno"/>
<result column="ename" property="ename"/>
<result column="job" property="job"/>
<!--1对1的关系, property: 指的是Emp对象的属性值(定义从表的变量名,Dept对象为Emp属性之一), javaType:指的是属性的类型,这里使用了别名 -->
<association property="dept" javaType="dept">
<!--从表数据 -->
<!—-column:数据表字段名,property:属性值 -->
<id column="deptno" property="deptno"/>
<result column="dname" property="dname"/>
<result column="loc" property="loc"/>
</association>
</resultMap>
<select id="queryE" resultMap="baseMap">
select
t1.empno
,t1.ename
,t1.job
,t2.deptno
,t2.dname
,t2.loc
from emp t1
left join dept t2
on t1.deptno = t2.deptno
</select>
1对多关系
查询出所有的部门信息及该部门下所有员工的信息
一个部门对应着多个雇员,是1对多的关系;
一个Dept对象里有一个Emp的集合
pojo类
Dept
package com.zsl.pojo;
import java.util.List;
public class Dept {
private Integer deptno;
private String dname;
private String loc;
private List<Emp> emps;
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer 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;
}
public List<Emp> getEmps() {
return emps;
}
public void setEmps(List<Emp> emps) {
this.emps = emps;
}
public Dept() {
super();
// TODO Auto-generated constructor stub
}
public Dept(Integer deptno, String dname, String loc) {
super();
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
@Override
public String toString() {
return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]";
}
}
DeptMapper接口
public interface DeptMapper {
public List<Dept> queryD();
}
DeptMapper.xml映射文件
<resultMap type="dept" id="baseMap">
<!-- 主表数据 -->
<id column="deptno" property="deptno"/>
<result column="dname" property="dname"/>
<result column="loc" property="loc"/>
<!-- 1 对 多的关联关系通过 collection配置
property 对应的就是对象中的多的那方的变量名称
ofType:就是集合中的泛型的类型 -->
<!-- 从表数据 -->
<!-- column:数据表字段名,property:属性值-->
<collection property="emps" ofType="emp">
<id column="empno" property="empno"/>
<result column="ename" property="ename"/>
<result column="job" property="job"/>
</collection>
</resultMap>
<select id="queryD" resultMap="baseMap">
select
t1.deptno
,t1.dname
,t1.loc
,t2.empno
,t2.ename
,t2.job
from dept t1
left join emp t2
on t1.deptno = t2.deptno
</select>
全局配置文件
mybatis-cfg.xml
<?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>
<!-- 导入外部资源 -->
<properties resource="db.properties"/>
<!-- <settings> 开启延迟加载
<setting name="lazyLoadingEnabled" value="true" />
<setting name="aggressiveLazyLoading" value="false" />
</settings> -->
<!-- 自定义别名 -->
<typeAliases>
<typeAlias type="com.zsl.pojo.Emp" alias="emp"/>
<typeAlias type="com.zsl.pojo.Dept" alias="dept"/>
</typeAliases>
<!-- 环境 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${userName}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!-- 注册映射文件 -->
<mappers>
<mapper resource="EmpMapper.xml"/>
<mapper resource="DeptMapper.xml"/>
<!-- <package name="包名"/> -->
</mappers>
</configuration>
DBUtils
package com.zsl.utils;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class DbUtils {
public static SqlSession getSession() throws IOException {
// TODO Auto-generated method stub
// 1.通过Resources对象加载配置文件
InputStream inputStream = Resources.getResourceAsStream("mybatis-cfg.xml");
// 2.获取SqlSessionFactory对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
// 3.通过SqlSessionFactory对象获取SQLSession对象
SqlSession session = factory.openSession();
return session;
}
public static void commit(SqlSession session) {
// TODO Auto-generated method stub
session.commit();
}
public static void close(SqlSession session) {
// TODO Auto-generated method stub
session.close();
}
}
测试类Test
package com.zsl.test;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.zsl.dao.DeptMapper;
import com.zsl.dao.EmpMapper;
import com.zsl.pojo.Dept;
import com.zsl.pojo.Emp;
import com.zsl.utils.DbUtils;
public class Test {
public static void main(String[] args) throws IOException {
Test test = new Test();
System.out.println(test.queryD());
}
public List<Emp> queryE() throws IOException {
// TODO Auto-generated method stub
SqlSession session = DbUtils.getSession();
EmpMapper mapper = session.getMapper(EmpMapper.class);
List<Emp> list = mapper.queryE();
session.close();
return list;
}
public List<Dept> queryD() throws IOException {
// TODO Auto-generated method stub
SqlSession session = DbUtils.getSession();
DeptMapper mapper = session.getMapper(DeptMapper.class);
List<Dept> list = mapper.queryD();
session.close();
return list;
}
}
多对多关系
双向的1对多既是多对多关系
就是两个对象互相以集合的形式为对方的属性
然后两个映射文件里配置resultMap 使用collection