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类的子类形式存在于代码中的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值