ALL is Well!

          敏捷是一條很長的路,摸索著前進著

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            30 隨筆 :: 23 文章 :: 71 評論 :: 0 Trackbacks

          主表:MASTER

          字段:ORDER_ID  --主鍵

                   RELATE_ID --申請單位

                   STOCK_ADDRESS --倉庫地址

                   TERMINAL_VENDER --供應(yīng)商

                   PROVINCE_ID --省分ID

                   STATE --狀態(tài)

           

          子表:DETAIL

          字段:ORDER_ID   --與主表ORDER_ID關(guān)聯(lián)

                   PROPERTY_CODE  --屬性編碼

                   SALE_PRICE  --價格

                   TERMINAL_VENDER --供應(yīng)商 與主表TERMINAL_VENDER關(guān)聯(lián)

                   PROVINCE_ID --省分ID 與主表PROVINCE_ID關(guān)聯(lián)

           

          主鍵為 ORDER_ID + PROPERTY_CODE

           

          要求,取得 主表:MASTER 中STATE為1的記錄,并映射成易于操作的java對象。

          并關(guān)聯(lián)子表,ORDER_ID、TERMINAL_VENDER、PROVINCE_ID作為查詢子表的條件。

          將查詢出的子表數(shù)據(jù)映射成List<Object> ,作為 主表映射成對象的一個 成員變量。

          以便后續(xù)操作。

           

          定義java對象 Master,對應(yīng)主表數(shù)據(jù):

           1package com.test.ibatis.po;
           2
           3import java.util.List;
           4
           5/**
           6 * 主表對應(yīng)數(shù)據(jù)
           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    /** 對應(yīng)子表數(shù)據(jù) */
          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類,對應(yīng)子表數(shù)據(jù):

           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    /** 產(chǎn)品編號 */
          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,

          執(zhí)行

           

          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" />

          的配置,

          即 將主表中的值傳到 子查詢當中作為查詢條件,

          這樣取得的數(shù)據(jù)就是 有關(guān)系的了。

          本文為原創(chuàng),歡迎轉(zhuǎn)載,轉(zhuǎn)載請注明出處BlogJava

          posted on 2010-09-01 12:32 李 明 閱讀(8952) 評論(5)  編輯  收藏 所屬分類: MyBatis/iBatis

          評論

          # re: iBatis/MyBatis 主子表關(guān)聯(lián)查詢 2011-03-15 00:50 孟天歌
          要是反過來怎么辦?


          Detail .java里面

          有個master屬性

          private Master master;

          怎么能查找到某Detail 對應(yīng)的master信息
            回復  更多評論
            

          # re: iBatis/MyBatis 主子表關(guān)聯(lián)查詢 2011-03-16 09:07 李 明
          @孟天歌
          是一樣的道理啊。
          我之所以把它們叫做Master和detail的原因是想體現(xiàn)一種 主子表 的關(guān)系。
          如果你想定義類 Detail里包含Master對象,那么就是 表 Detail表是主表,而Master表是子表了。

          這些java類是你自己設(shè)計的,你的目的是要符合表的關(guān)系才行啊。并不是隨意設(shè)定的。
          如果有其他問題也請聯(lián)系。  回復  更多評論
            

          # re: iBatis/MyBatis 主子表關(guān)聯(lián)查詢[未登錄] 2011-11-04 17:47 simon
          {province=PROVINCE_CODE,id=ORDER_ID,VENDER=TERMINAL_VENDER}


          為啥我指定了多列就報錯.?
            回復  更多評論
            

          # re: iBatis/MyBatis 主子表關(guān)聯(lián)查詢[未登錄] 2013-05-22 23:51 Ryan
          mybatis3不能這樣吧?result沒有select參數(shù)?  回復  更多評論
            

          # re: iBatis/MyBatis 主子表關(guān)聯(lián)查詢[未登錄] 2014-06-24 11:34 你好
          好像真沒有select參數(shù),result中加了select就報錯。@Ryan
            回復  更多評論
            


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 伊通| 隆林| 临汾市| 永和县| 长汀县| 光山县| 富平县| 合水县| 红桥区| 西宁市| 宜兰县| 岳池县| 云安县| 桐乡市| 友谊县| 张家川| 周口市| 启东市| 金沙县| 福州市| 广汉市| 连云港市| 永吉县| 庄浪县| 舒兰市| 新化县| 吉隆县| 类乌齐县| 萝北县| 秭归县| 简阳市| 孟连| 乌鲁木齐县| 隆安县| 甘孜县| 长武县| 中江县| 绵竹市| 原阳县| 周宁县| 定兴县|