二.mybatis高级结果映射

2017-11-24  本文已影响0人  蜗牛1991

一.介绍与应用场景

二准备工作

public class Course {
    private Integer id;
    private String cousreName;
    List<Student> students;
}

学生

public class Student {
    private Integer id;
    private String stuName;
    private Address  address;
}

家庭情况

public class  Address {
    private Integer id;
    private String address;
}
<resultMap id="testMap" type="course">
  <id column="id" jdbcType="INTEGER" property="id" />
  <result column="cousre_name" jdbcType="VARCHAR"property="cousreName" />
  <collection property=" students" ofType="student">
    <id column="s_id" jdbcType="INTEGER" property="id" />
    <result column="stu_name " jdbcType="VARCHAR" property="stuName" />
    <association property="address" javaType="Address">
     <id column="a_id" jdbcType="INTEGER" property="id" />
      <result column="address" jdbcType="VARCHAR" property="address" />
    </association>
  </collection>
</resultMap>

<select id="findCommodityPageById" resultMap="commodityPageModal" >
  SELECT
  c.id ,
  c.course_name ,
  s.id as s_id,
  s.stu_name ,
  a.id,
  a.address
  FROM  course c 
  LEFT JOIN student s
  ON c.id=s.course_id
  LEFT JOIN  address a
  ON s.id=a.stu_id
</select>

三.分析

上一篇 下一篇

猜你喜欢

热点阅读