一.多对一的映射关系
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.aaa.mybatis.dao.EmpDao"> <!---创建resultMap映射关联关系--> <!--type对应resultMap中的实体类的类型 ,需要写实体类的完全限定名--> <resultMap id="empMap" type="com.aaa.mybatis.entity.Emp"> <!--id标签用来映射实体类的主键 column对应sql语句返回值的别名 不区分大小写 property对应实体类中属性名 区分大小写 --> <id column="empno" property="empno"></id> <!--result映射普通的属性--> <result column="ename" property="ename"></result> <result column="sal" property="sal"></result> <!--配置多对一的关联映射 property对应实体类中持有的一的一方的属性名 javaType 对应持有的一的一方的属性的类型,即该属性对应类的完全限定名 --> <association property="dept" javaType="com.aaa.mybatis.entity.Dept"> <id column="deptno" property="deptno"></id> <result column="dname" property="dname"></result> <result column="loc" property="loc"></result> </association> </resultMap> <select id="findByEmpno" resultMap="empMap"> select e.empno as empno, e.ename as ename,e.sal as sal,d.deptno as deptno ,d.dname as dname ,d.loc as loc from emp e inner join dept d on e.deptno=d.deptno where e.empno=#{empno} </select> </mapper>
第四步,测试
public static void main(String[] args) { SqlSession session = SqlSessionFactoryUtil.getSession(); EmpDao empdao = session.getMapper(EmpDao.class); Emp emp = empdao.findByEmpno(7369L); System.out.println("员工编号:"+emp.getEmpno()); System.out.println("员工姓名:"+emp.getEname()); System.out.println("基本工资"+emp.getSal()); //获取员工的部门信息 Dept dept = emp.getDept(); System.out.println("部门对象:"+dept); System.out.println("部门编号:"+dept.getDeptno()); System.out.println("部门名称:"+dept.getDname()); System.out.println("部门地址:"+dept.getLoc()); System.out.println(); session.close(); }
二.一对多的映射关系
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.aaa.mybatis.dao.DeptDao"> <!--创建resultMap--> <resultMap id="deptMap" type="com.aaa.mybatis.entity.Dept"> <id column="deptno" property="deptno"></id> <result column="dname" property="dname"></result> <result column="loc" property="loc"></result> <!--配置一对多的映射关系 property 一的一方持有的多的一方的属性的名称 ofType 一的一方持有的多的一方的属性的集合类型中的实体类的完全限定名 --> <collection property="emps" ofType="com.aaa.mybatis.entity.Emp"> <id column="empno" property="empno"></id> <result column="ename" property="ename"></result> <result column="sal" property="sal"></result> </collection> </resultMap> <select id="findByDeptno" resultMap="deptMap"> select d.deptno,d.dname,d.loc,e.empno,e.ename,e.sal from dept d inner join emp e on d.deptno =e.deptno where d.deptno=#{deptno} </select> </mapper>
/** * 配置一对多的关联 */ public class Test01 { public static void main(String[] args) { SqlSession session = SqlSessionFactoryUtil.getSession(); DeptDao deptDao = session.getMapper(DeptDao.class); Dept dept = deptDao.findByDeptno(10L); System.out.println("部门编号:"+dept.getDeptno()); System.out.println("部门名称:"+dept.getDname()); System.out.println("部门位置:"+dept.getLoc()); System.out.println("====="); List<Emp> emps = dept.getEmps(); for (Emp e : emps) { System.out.println("员工编号:"+e.getEmpno()); System.out.println("员工姓名:"+e.getEname()); System.out.println("员工基本工资:"+e.getSal()); System.out.println("---------"); } session.close(); } }
原文:https://www.cnblogs.com/duguangming/p/10889568.html