1. 程式人生 > >mybatis 之多表查詢

mybatis 之多表查詢

這次是三張表連查,第一次寫,若有需要改進的地方還請大家多多指點

一、三張表

教師基本資訊表(teacher_info)

教師考勤表(leave)(注:教師一個月的出勤情況對應一條考勤) 

教師工資明細表(teacher_salary)

在給表起名字的時候,注意有些是關鍵字,例如‘leave’在後續的程式碼中可能會造成一些錯誤,大家注意,不要像我的這個

表與表之間的關係:

一名教師有多條考勤,所以teacher_info 和leave之間是一對多的關係   1:n   因此在teacher_info對應的實體類中,添加了leaveTeacher 一個屬性

一條考勤對應一條工資明細,   leave 和 teacher_salary 是一對一的關係  1:1  因此在leave對應的實體類中,添加了teacherSalary 一個屬性

通過mybatis-generator生成對應的實體類是這樣的:

教師基本資訊表(teacher_info):

public class TeacherInfo {

    private Integer tnum;

    private String name;

    private String sex;

    private Date birth;

    private String tel;

    private String grade;

    private String school;

    private String college;

    private String office;

    private String profess;

    private String state;

    private Date createtime;

    private Date changetime;

//    三表聯查    一名教師有多條考勤,所以用List
    private List<LeaveTeacher>  leaveTeacher;
}

教師考勤表(leave)
public class LeaveTeacher {
    private Integer leaveId;

    private Integer tnum;

    private String teacherName;

    private Date startTime;

    private Float workDay;

    private Float sickRelax;

    private Float thingRelax;

    private String approvePerson;

    private String yearMonth;

    private Float fullAttendance;

    private Date createtime;

    private Date changetime;

    //關聯的欄位  考勤表和教師基本資訊進行關聯  考勤倆表聯查
    private String name;

    //考勤表和工資表進行關聯   一條考勤對應一條工資 三表聯查
    private TeacherSalary teacherSalary;

教師工資明細表(teacher_salary)

public class TeacherSalary {
    private Integer salTeacherId;

    private Integer leaveTeacherId;

    private Integer tnum;

    private String name;

    private String tMonth;

    private Float basicWage;

    private Float trafficWage;

    private Float fullReward;

    private Float totalWage;

    private Float kaoqinReduce;

    private Float secureReduce;

    private Float taxReduce;

    private Float totalReduce;

    private Float realWage;

    private Date createtime;

    private Date changetime;
二、mapper.xml檔案
 <resultMap id="BaseResultMap" type="com.aim.graduation.dao.entity.TeacherInfo">
    <id column="tnum" jdbcType="INTEGER" property="tnum" />
    <result column="id" jdbcType="INTEGER" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="sex" jdbcType="VARCHAR" property="sex" />
    <result column="birth" jdbcType="DATE" property="birth" />
    <result column="tel" jdbcType="VARCHAR" property="tel" />
    <result column="grade" jdbcType="VARCHAR" property="grade" />
    <result column="school" jdbcType="VARCHAR" property="school" />
    <result column="college" jdbcType="VARCHAR" property="college" />
    <result column="office" jdbcType="VARCHAR" property="office" />
    <result column="profess" jdbcType="VARCHAR" property="profess" />
    <result column="state" jdbcType="VARCHAR" property="state" />
    <!--<result column="basic_wage" jdbcType="REAL" property="basicWage" />-->
    <result column="createtime" jdbcType="TIMESTAMP" property="createtime" />
    <result column="changetime" jdbcType="TIMESTAMP" property="changetime" />
    <!--assocication可以指定聯合的JavaBean物件   基本資訊表
    property="role"指定哪個屬性是聯合的物件
    javaType:指定這個屬性物件的型別
    -->
    <collection property="leaveTeacher" ofType="com.aim.graduation.dao.entity.LeaveTeacher">
      <id column="leave_id" jdbcType="INTEGER" property="leaveId" />
      <result column="tnum" jdbcType="INTEGER" property="tnum" />
      <!--<result column="teacher_name" jdbcType="VARCHAR" property="teacherName" />-->
      <result column="start_time" jdbcType="DATE" property="startTime" />
      <result column="work_day" jdbcType="FLOAT" property="workDay" />
      <result column="sick_relax" jdbcType="FLOAT" property="sickRelax" />
      <result column="thing_relax" jdbcType="FLOAT" property="thingRelax" />
      <result column="approve_person" jdbcType="VARCHAR" property="approvePerson" />
      <result column="year_month" jdbcType="VARCHAR" property="yearMonth" />
      <result column="full_attendance" jdbcType="FLOAT" property="fullAttendance" />
      
      <association property="teacherSalary" javaType="com.aim.graduation.dao.entity.TeacherSalary">
        <id column="sal_teacher_id" jdbcType="INTEGER" property="salTeacherId" />
        <result column="basic_wage" jdbcType="REAL" property="basicWage" />
        <result column="traffic_wage" jdbcType="REAL" property="trafficWage" />
        <result column="full_reward" jdbcType="REAL" property="fullReward" />
        <result column="secure_reduce" jdbcType="REAL" property="secureReduce" />
        <result column="tax_reduce" jdbcType="REAL" property="taxReduce" />
      </association>
    </collection>
  </resultMap>
 <association>通常用來對映一對一的關係

 <collection >通常用來對映一對多的關係

三、sql語句

我寫的很簡單,能查出來就行

  <!--三表聯查-->
  <select id="selectInfoLeaveSalary" resultMap="BaseResultMap">
    SELECT
    *
    FROM
    teacher_info,
    `leave`,
    teacher_salary
    WHERE
    teacher_info.tnum = `leave`.tnum
    AND teacher_salary.leave_teacher_id = `leave`.leave_id
  </select>

我寫的很簡單,若有什麼需要改進的地方希望大家多多指教!