日期:2014-05-20  浏览次数:20739 次

请问一下Ibatis3/Mybatis该如何进行一对一联合查询且不出现N+1问题
我的两个表:
SQL code

CREATE TABLE `t_customers` (
  `customer_id` int(32) NOT NULL auto_increment,
  `customer_name` varchar(50) default NULL,
  `customer_address` varchar(200) default NULL,
  PRIMARY KEY  (`customer_id`)
)
CREATE TABLE `t_orders` (
  `ORDER_ID` int(32) NOT NULL auto_increment,
  `ORDER_NUMBER` varchar(32) default NULL,
  `ORDER_DATE` date default NULL,
  `ORDER_TOTAL` double(10,2) default NULL,
  `PAYMENT_STATE` int(1) default NULL,
  `SEND_STATE` int(1) default NULL,
  `ORDER_CUSTOMER_ID` int(32) default NULL,
  PRIMARY KEY  (`ORDER_ID`),
  KEY `fk_orders_customers` (`ORDER_CUSTOMER_ID`),
  CONSTRAINT `fk_orders_customers` FOREIGN KEY (`ORDER_CUSTOMER_ID`) REFERENCES `t_customers` (`customer_id`)
-- 每一个Order(订单)对应一个Customer(客户),t_customer的主键是t_orders的一个外键
) 


Order.java:
Java code

package com.yuwen.domain;

import java.math.BigDecimal;
import java.util.Date;

public class Order {
    
    private long orderId;
    
    private String orderNumber;
    
    private Date orderDate;
    
    private BigDecimal orderTotal;
    
    private int paymentState;
    
    private int sendState;
    
    private Customer customer;

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public long getOrderId() {
        return orderId;
    }

    public void setOrderId(long orderId) {
        this.orderId = orderId;
    }

    public String getOrderNumber() {
        return orderNumber;
    }

    public void setOrderNumber(String orderNumber) {
        this.orderNumber = orderNumber;
    }

    public Date getOrderDate() {
        return orderDate;
    }

    public void setOrderDate(Date orderDate) {
        this.orderDate = orderDate;
    }

    
    public BigDecimal getOrderTotal() {
        return orderTotal;
    }

    public void setOrderTotal(BigDecimal orderTotal) {
        this.orderTotal = orderTotal;
    }

    public int getPaymentState() {
        return paymentState;
    }

    public void setPaymentState(int paymentState) {
        this.paymentState = paymentState;
    }

    public int getSendState() {
        return sendState;
    }

    public void setSendState(int sendState) {
        this.sendState = sendState;
    }
    
}


Customer.java:
Java code

package com.yuwen.domain;

public class Customer {

    private long customerId;
    
    private String customerName;
    
    private String customerAddress;
    
    public long getCustomerId() {
        return customerId;
    }

    public void setCustomerId(long customerId) {
        this.customerId = customerId;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String getCustomerAddress() {
        return customerAddress;
    }

    public void setCustomerAddress(String customerAddress) {
        this.customerAddress = customerAddress;
    }    
}


dao-configuration.xml:
XML code

<?xml version="1.0" encoding="UTF-8"?>  
  
<!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"   
"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">  
  
<configuration>  
      <properties resource="properties/config.properties"/>
      <settings>
          <setting name="lazyLoadingEnabled" value="true"/>      
      </settings>
      <typeAliases>  
        <typeAlias type="com.yuwen.domain.Order" alias="order" />  
        <typeAlias type="com.yuwen.domain.Customer" alias="customer"/>
    </typeAliases>  
    <environments default="development">  
        <environment id="development">  
            <transactionManager type="JDBC"/>  
            <dataSource type="POOLED">
                <property name="driver" value="${driver}" />  
                <property name="url" value="${url}" />  
                <property name="username" value="${username}" />  
                <property name="password" value="${password}" />  
            </dataSource>  
        </environment>  
    </environments>  
    <mappers>  
        <mapper resource="com/yuwen/domain/Order.xml" />
    </mappers>  
       
</configuration>