Spring: A Developer's Notebook筆記和小結(13)

/**
作者:Willpower
來源:Rifoo Technology(
http://www.rifoo.com
時間:2006-02-18
備註:轉載請保留以上聲明
**/

在這一節裏,我們將編寫一個簡單的查詢對象(query object),並將它包裝到一個可重用的表單中。Spring提供一個叫做RDBMS operational objects的API來幫助我們包裝存儲過程,查詢語句或更新語句。

假設在這個場景裏,我們要查詢一家旅館的預訂情況(reservation)情況,我們編寫一個MappingSqlQuery類來對每種類型的預訂情況進行查詢。對每個查詢指定其參數,然後設置參數的類型。和上一節一樣,我們這裏將以內部類的形式來指定一個方法映射每一行記錄。

Example 4-9. JDBCRentABike.java
abstract class FindReservations extends MappingSqlQuery {
  protected List reservations = new ArrayList( );//保存所有預訂記錄的數組
  protected FindReservations(DataSource dataSource, String query) {
    super(dataSource, query);
  }

  protected Object mapRow(ResultSet rs, int rownum)
    throws SQLException {
    //取得表中的行記錄
    int resId = rs.getInt(1);
    int bikeId = rs.getInt(2);
    int custId = rs.getInt(3);
    Date resDate = rs.getDate(4);

    Bike bike = getBike(bikeId);
    Customer customer = getCustomer(custId);
    //構造一個預訂記錄對象
    Reservation reservation = new Reservation(resId, bike,
      customer, resDate);
    reservations.add(reservation);
    return reservation;
  }

  abstract List findReservations(int param);
}

class FindReservationsByCustomer extends FindReservations {
  public FindReservationsByCustomer(DataSource dataSource) {
    super(dataSource,
      "SELECT * FROM reservations WHERE custId = ?");//提供查詢語句
    declareParameter(new SqlParameter(Types.INTEGER)); //聲明參數類型
    compile( );

  }

  public List findReservations(int param) {
    execute(param);
    return this.reservations;
  }
}

class FindReservationsByBike extends FindReservations {
  public FindReservationsByBike(DataSource dataSource) {
   
super(dataSource,
      "SELECT * FROM reservations WHERE bikeId = ?");
    declareParameter(new SqlParameter(Types.INTEGER));
    compile( );
  }

  public List findReservations(int param) {
    execute(param);
    return reservations;
  }
}


接着我們可以在該類中繼續添加一下方法來獲得指定的預訂信息:
Example 4-10. JDBCRentABike.java
public List getReservations(Customer customer) {
    return new FindReservationsByCustomer(dataSource).
      findReservations(customer.getCustId( ));
  }

  public List getReservations(Bike bike) {
    return new FindReservationsByBike(dataSource).
      findReservations(bike.getBikeId( ));
  }


在本場景中,我們還需要添加兩個domain class,分別表示顧客(customers)和預訂情況(reservations)。

Example 4-11. Customer.java
package com.springbook;
import java.util.Set;

public class Customer {
  private int custId;//顧客ID
  private String firstName;//姓
  private String lastName;//名
  private Set reservations;//顧客的預訂情況(顧客和預訂情況是1對多關係)

  public Set getReservations( ) { return reservations; }

  public void setReservations(Set reservations)
    { this.reservations = reservations; }

  public int getCustId( ) { return custId; }

  public void setCustId(int custId) { this.custId = custId; }

  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 Customer(int custId, String firstName, String lastName) {
    this.CustId = custId;
    this.firstName = firstName;
    this.lastName = lastName;
  }

  public Customer( ) {}

  public String toString( ) {
    return "Customer : " +
        "custId -- " + custId +
        "/n: firstName --" + firstName +
        "/n: lastName --" + lastName +
        "./n";
  }
}


Example 4-12. Reservation.java
package com.springbook;
import java.util.Date;

public class Reservation {
  private int reservationId;//預訂ID
  private Date reservationDate;//預訂的日期
  private Bike bike;//預訂的山地車對象
  private Customer customer;//預訂的顧客對象

  public Reservation( ) {}

  public int getReservationId( ) { return reservationId; }

  public void setReservationId(int reservationId)
    { this.reservationId = reservationId; }

  public Date getReservationDate( ) { return reservationDate; }

  public void setReservationDate(Date reservationDate)
    { this.reservationDate = reservationDate; }

  public Bike getBike( ) { return bike; }

  public void setBike(Bike bike) { this.bike = bike; }

  public Customer getCustomer( ) { return customer;}

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

  public Reservation(int id, Bike bike, Customer customer, Date date) {
    this.reservationId = id;
    this.bike = bike;
    this.customer = customer;
    this.reservationDate = date;
  }

  public String toString( ) {
    return "Reservation : " +
        "reservationId -- " + reservationId +
        "/n: reservationDate -- " + reservationDate +
        "/n: bike -- " + bike +
        "/n: customer -- " + customer +
        "./n";
  }
}


工作原理總結:
我們創建了一個可重用的參數化的類,實現也很簡單。其中MapRow方法轉換每行數據爲一個對象。然後指定的query查詢返回我們指定的所預訂的山地車列表。我們還提供了通過日期,顧客和日期等方式查詢預訂情況的方法,它們都是以FindReservations類的子類形式存在於代碼中的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章