最近項目用到中間表,則遇到如何聯查映射的問題,以前一直都是一個表頭,多個明細或者一對一這樣的關係,沒遇到這樣的問題,因此趁機找了下資料解決了這個問題。java
表結構設計以下:sql
主表:mybatis
CREATE TABLE `process_line_detail` ( `id` char(32) NOT NULL COMMENT 'ID', `serial_no` int(11) NOT NULL COMMENT '序號', `process_id` char(32) DEFAULT '' COMMENT '工序id' )
中間表:編碼
REATE TABLE `process_line_detail_device` ( `process_line_detail_id` char(32) NOT NULL COMMENT '工序線詳情id', `device_id` char(32) NOT NULL COMMENT '設備id' )
CREATE TABLE `process_line_detail_mould` ( `process_line_detail_id` char(32) NOT NULL COMMENT '工序線詳情id', `mould_id` char(32) NOT NULL COMMENT '模具id' )
中間表關聯表:spa
CREATE TABLE `device` ( `id` char(32) NOT NULL COMMENT 'ID', `name` varchar(32) NOT NULL COMMENT '名稱' )
CREATE TABLE `mould` ( `id` char(32) NOT NULL COMMENT 'ID', `code` varchar(64) NOT NULL COMMENT '編碼', `serial_no` varchar(32) DEFAULT '' COMMENT '序列號', `name` varchar(32) DEFAULT '' COMMENT '模具名稱', `model` varchar(64) DEFAULT '' COMMENT '模具型號' )
一對一的關聯表:設計
CREATE TABLE `process` ( `id` char(32) NOT NULL COMMENT 'ID', `name` varchar(64) DEFAULT '' COMMENT '工序名稱' )
實體類則不須要關聯表,只須要在主表中添加關聯表的集合或者單個對象便可(簡寫,省略 get,set):code
public class ProcessLineDetail{
private String id; private String serialNo; // 序號 private Process process; // 工序 private List<Device> deviceList = Collections.emptyList();
private List<Mould> mouldList = Collections.emptyList();
}
public class Device { private String name; // 名稱 private String id; // id }
public class Mould { private String id; private String code; // 編碼 private String serialNo; // 序列號 private String name; // 模具名稱 private String model; // 模具型號 }
public class Process { private String id; // id private String name; // 工序名稱
mybatis映射文件以下(簡寫,沒包含具體實體的路徑):對象
<sql id="baseColumns"> a.id, a.serial_no, a.process_id, process.name AS "process_name", device.name AS "device_name", device.id AS "device_id", mould.id AS "mould_id", mould.name AS "mould_name" </sql>
<resultMap id="AllResultMap" type="ProcessLineDetail"> <id property="id" column="id" /> <result property="serialNo" column="serial_no" /> <association property="process" column="process_id" javaType="Process"> <id column="process_id" property="id" /> <result column="process_name" property="name" /> </association> <collection property="deviceList" ofType="Device" > <id column="device_id" property="id" /> <result column="device_name" property="name"/> </collection> <collection property="mouldList" ofType="Mould"> <id column="mould_id" property="id" /> <result column="mould_name" property="name" /> </collection> </resultMap>
<sql id="allJoins"> left join process_line_detail_device detailDevice on detailDevice.process_line_detail_id = a.id left join device device on device.id = detailDevice.device_id left join process_line_detail_mould detailMould on detailMould.process_line_detail_id = a.id left join mould mould on mould.id = detailMould.mould_id left join process process ON process.id = a.process_id </sql>
<select id="get" resultMap="AllResultMap" > SELECT <include refid="baseColumns"/> FROM process_line_detail a <include refid="allJoins"/> WHERE a.id = #{id} </select>
如此便可,mybaits會自動將各數據集合一一對應上。blog