ibatis2 解决 n+1 方法

 

方法一.延迟加载

<settings lazyLoadingEnabled="true" useStatementNamespaces="true"/>

方法二
public class Account {

  private long id;
  private String firstName;
  private String lastName;
  private String emailAddress;
 
  private List<AccOrder> accOrder;

  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  public String getEmailAddress() {
    return emailAddress;
  }

  public void setEmailAddress(String emailAddress) {
    this.emailAddress = emailAddress;
  }

public List<AccOrder> getAccOrder() {
 return accOrder;
}

public void setAccOrder(List<AccOrder> accOrder) {
 this.accOrder = accOrder;
}

}


public class AccOrder {
 
 private int accid;
 private String orderName;
 
 public int getAccid() {
  return accid;
 }
 public void setAccid(int accid) {
  this.accid = accid;
 }
 public String getOrderName() {
  return orderName;
 }
 public void setOrderName(String orderName) {
  this.orderName = orderName;
 }

}


<sqlMap namespace="Account">

  <typeAlias alias="Account" type="com.mydomain.pojo.Account"/>
 
  <typeAlias alias="AccOrder" type="com.mydomain.pojo.AccOrder"/>

  <resultMap id="AccOrderResult" class="AccOrder">
    <result property="accid" column="ACC_ID"/>
    <result property="orderName" column="orderName"/>
  </resultMap>
 
    <resultMap id="AccountResult" class="Account" groupBy="ACC_ID">
    <result property="id" column="ACC_ID"/>
    <result property="firstName" column="ACC_FIRST_NAME"/>
    <result property="lastName" column="ACC_LAST_NAME"/>
    <result property="emailAddress" column="ACC_EMAIL"/>
   <result property="accOrder" resultMap="Account.AccOrderResult"/>
  </resultMap>
 
   <select id="getUserById" resultMap="AccountResult" parameterClass="int"> 
       select * from account acc,acc_order ord where acc.acc_id = ord.acc_id
       and acc.ACC_ID = #value# 
   </select> 
</sqlMap>

 


方法3,新建组合类

public class AccAndOrder {

 private Account account;

 private AccOrder accOrder;

 public AccOrder getAccOrder() {
  return accOrder;
 }

 public void setAccOrder(AccOrder accOrder) {
  this.accOrder = accOrder;
 }

 public Account getAccount() {
  return account;
 }

 public void setAccount(Account account) {
  this.account = account;
 }

}

<sqlMap namespace="AccAndOrder">

  <!-- Use type aliases to avoid typing the full classname every time. -->
  <typeAlias alias="AccAndOrder" type="com.mydomain.pojo.AccAndOrder"/>

  <resultMap id="Results" class="AccAndOrder">
    <result property="account.id" column="ACC_ID"/>
    <result property="account.firstName" column="ACC_FIRST_NAME"/>
    <result property="account.lastName" column="ACC_LAST_NAME"/>
    <result property="account.emailAddress" column="ACC_EMAIL"/>
    <result property="accOrder.orderName" column="orderName"/>
  </resultMap>

  <statement id="selectMany" resultMap="Results">
     select acc.acc_id,acc.ACC_FIRST_NAME,acc.ACC_LAST_NAME,acc.ACC_EMAIL,
     ord.orderName
  from ACCOUNT acc,acc_order  ord  where  acc.acc_id = ord.acc_id
  </statement>
</sqlMap>


 

你可能感兴趣的:(ibatis)