Acegi中JdbcDaoImpl内MappingSqlQuery的讲解

MappingSqlQuery is a reusable query in which concrete subclasses must implement the abstractmapRow(..) method to convert each row of the supplied ResultSet into an object of the type specified. The following example shows a custom query that maps the data from the t_actor relation to an instance of the Actor class.

public class ActorMappingQuery extends MappingSqlQuery<Actor> {
        public ActorMappingQuery(DataSource ds) { 
                  super(ds, "select id,first_name, last_name from t_actor           where id = ?");
                  super.declareParameter(new SqlParameter("id",    Types.INTEGER)); 
                  compile();
 } 

@Override protected Actor mapRow(ResultSet rs, int rowNumber) throws SQLException { 
                  Actor actor = new Actor(); 
                  actor.setId(rs.getLong("id"));         
                  actor.setFirstName(rs.getString("first_name"));  
                  actor.setLastName(rs.getString("last_name")); 
                  return actor; 
} 
}
 

 

The class extends MappingSqlQuery parameterized with the Actor type. The constructor for this customer query takes the DataSource as the only parameter. In this constructor you call the constructor on the superclass with the DataSourceand the SQL that should be executed to retrieve the rows for this query. This SQL will be used to create aPreparedStatement so it may contain place holders for any parameters to be passed in during execution.You must declare each parameter using the declareParameter method passing in an SqlParameter. The SqlParameter takes a name and the JDBC type as defined in java.sql.Types. After you define all parameters, you call thecompile() method so the statement can be prepared and later executed. This class is thread-safe after it is compiled, so as long as these instances are created when the DAO is initialized they can be kept as instance variables and be reused.

private ActorMappingQuery actorMappingQuery;

@Autowired public void setDataSource(DataSource dataSource) {        
          this.actorMappingQuery = new ActorMappingQuery(dataSource);
} 
public Customer getCustomer(Long id) {
           return actorMappingQuery.findObject(id); 
}
 

The method in this example retrieves the customer with the id that is passed in as the only parameter. Since we only want one object returned we simply call the convenience method findObjectwith the id as parameter. If we had instead a query that returned a list of objects and took additional parameters then we would use one of the execute methods that takes an array of parameter values passed in as varargs.

 

public List<Actor> searchForActors(int age, String namePattern) {       
          List<Actor> actors = actorSearchMappingQuery.execute(age, namePattern);
          return actors; 
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值