方法一:
主表:MASTER
字段:ORDER_ID --主键
RELATE_ID --申请单位
STOCK_ADDRESS --仓库地址
TERMINAL_VENDER --供应商
PROVINCE_ID --省分ID
STATE --状态
子表:DETAIL
字段:ORDER_ID --与主表ORDER_ID关联
PROPERTY_CODE --属性编码
SALE_PRICE --价格
TERMINAL_VENDER --供应商 与主表TERMINAL_VENDER关联
PROVINCE_ID --省分ID 与主表PROVINCE_ID关联
主键为 ORDER_ID + PROPERTY_CODE
要求,取得 主表:MASTER 中STATE为1的记录,并映射成易于操作的java对象。
并关联子表,ORDER_ID、TERMINAL_VENDER、PROVINCE_ID作为查询子表的条件。
将查询出的子表数据映射成List<Object> ,作为 主表映射成对象的一个 成员变量。
以便后续操作。
定义java对象 Master,对应主表数据:
- 1package com.test.ibatis.po;
- 2
- 3import java.util.List;
- 4
- 5/** *//**
- 6 * 主表对应数据
- 7 */
- 8public class Master implements java.io.Serializable {
- 9 private static final long serialVersionUID = 1L;
- 10 /** *//** ID */
- 11 private String channelsId = null;
- 12 /** *//** 地址 */
- 13 private String deliveryLoc = null;
- 14
- 15 /** *//** 对应子表数据 */
- 16 private List<Detail> details = null;
- 17
- 18 public String getChannelsId() {
- 19 return channelsId;
- 20 }
- 21
- 22 public void setChannelsId(String channelsId) {
- 23 this.channelsId = channelsId;
- 24 }
- 25
- 26 public String getDeliveryLoc() {
- 27 if (deliveryLoc == null)
- 28 return "";
- 29 return deliveryLoc;
- 30 }
- 31
- 32 public void setDeliveryLoc(String deliveryLoc) {
- 33 this.deliveryLoc = deliveryLoc;
- 34 }
- 35
- 36 public List<Detail> getDetails() {
- 37 return details;
- 38 }
- 39
- 40 public void setDetails(List<Detail> details) {
- 41 this.details = details;
- 42 }
- 43}
定义Detail类,对应子表数据:
- 1package com.test.ibatis.po;
- 2
- 3import java.text.DecimalFormat;
- 4
- 5public class Detail implements java.io.Serializable {
- 6 private static final long serialVersionUID = 1L;
- 7
- 8 private static final DecimalFormat df = new DecimalFormat("###0.00");
- 9 /** *//** 产品编号 */
- 10 private String partNo = null;
- 11 /** *//** 价格 */
- 12 private String price = null;
- 13
- 14 public String getPartNo() {
- 15 return partNo;
- 16 }
- 17
- 18 public void setPartNo(String partNo) {
- 19 this.partNo = partNo;
- 20 }
- 21
- 22 public String getPrice() {
- 23 if (price == null)
- 24 return "0";
- 25 return df.format(Double.parseDouble(price) / 1000.0);
- 26 }
- 27
- 28 public void setPrice(String price) {
- 29 this.price = price;
- 30 }
- 31}
sql如下配置:
- 1<?xml version="1.0" encoding="gbk" ?>
- 2
- 3<!DOCTYPE sqlMap
- 4 PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
- 5 "http://ibatis.apache.org/dtd/sql-map-2.dtd">
- 6<sqlMap namespace="TEST_SQL">
- 7 <typeAlias alias="HashMap" type="java.util.HashMap" />
- 8
- 9 <!-- Master对象定义 -->
- 10 <resultMap id="master" class="com.test.ibatis.po.Master">
- 11 <result property="channelsId" column="ORDER_CHANNEL" />
- 12 <result property="deliveryLoc" column="DELIVER_ADDRESS" />
- 13 <result property="details" column="{province=PROVINCE_CODE,id=ORDER_ID,VENDER=TERMINAL_VENDER}"
- 14 select="select-dtl" />
- 15 </resultMap>
- 16 <!-- Detail对象定义 -->
- 17 <resultMap id="detail" class="com.linkage.ess.ordercreate.po.OrderDetail">
- 18 <result property="partNo" column="PROPERTY_CODE" />
- 19 <result property="price" column="SALE_PRICE" />
- 20 </resultMap>
- 21 <select id="selectData" resultMap="master">
- 22 <!--[CDATA[
- 23 SELECT T.RELATE_ID ORDER_CHANNEL,
- 24 T.STOCK_ADDRESS DELIVER_ADDRESS
- 25 FROM MASTER T
- 26 WHERE T.PROVINCE_ID = #PROVINCE_ID#
- 27 AND T.STATE = '1'
- 28 ]]>
- 29 </select>
- 30 <statement id="select-dtl" resultMap="detail">
- 31 <![CDATA[
- 32 SELECT D.PROPERTY_CODE,
- 33 D.SALE_PRICE,
- 34 FROM DETAIL D
- 35 WHERE D.ORDER_ID = #id#
- 36 AND D.TERMINAL_VENDER = #VENDER#
- 37 AND D.PROVINCE_ID = #province#
- 38 ]]-->
- 39 </statement>
- 40</sqlMap>
这样所有的工作都OK,
执行
List<Master> masters = (List<Master>) sqlMap.queryForList("selectData", param);
// param 为HashMap, put("PROVINCE_ID", "BJ"); 作为查询条件用。
得到 List<Master>,其中每个Master对象,都会持有 List<Detail>。
要点在于
<result property="details" column= "{province=PROVINCE_CODE,id=ORDER_ID,VENDER=TERMINAL_VENDER}"
select="select-dtl" />
的配置,
即 将主表中的值传到 子查询当中作为查询条件,
这样取得的数据就是 有关系的了。
方法二:
ibatis的子对象查询, 填充到List<E> 中
<resultMap id="memberResult" type="member">
<id property="id" column="ID" />
<id property="memberID" column="MEM_ID" />
<id property="userName" column="MEN_NAME" />
<id property="fullName" column="NAME" />
<id property="memberCode" column="MEM_NUM" />
<id property="activateCode" column="ACTIVATE_CODE" />
<id property="cardNo" column="CARD_NO" />
<id property="cardLevel" column="CARD_LEVEL" />
<id property="memberType" column="MEM_TYPE" />
<id property="memberHierarchy" column="MEM_TIER" />
<id property="registerSource" column="ENROLL_CHANNEL" />
<id property="registerDate" column="ENROLL_DATE" />
<id property="remindQuestion" column="QUESTION" />
<id property="remindAnswer" column="ANSWER" />
<id property="status" column="STATUS" />
<id property="email" column="EMAIL" />
<id property="cellPhone" column="PHONE" />
<id property="password" column="PASSWORD" />
<id property="lastUpd" column="LAST_UPD"/>
<id property="newMemberHierarchy" column="NEW_MEM_TIER" />
<id property="identityType" column="IDENTITY_TYPE" />
<id property="identityNo" column="IDENTITY_NO" />
<id property="title" column="TITLE" />
<id property="mcMemberCode" column="MC_MEMBER_CODE" />
<id property="thirdpartyType" column="THIRDPARTY_TYPE" />
<id property="mCustomerId" column="M_CUSTOMER_ID" />
<collection property="cardList" column="ID" javaType="ArrayList" ofType="memberMemCard" select="queryCardInfoByMemberInfoId" />
<collection property="memberVerfyList" column="ID" javaType="ArrayList" ofType="memberVerfy" select="queryVerifyInfoByInfoId" />
</resultMap>