日期:2014-05-16  浏览次数:20487 次

spring jdbctemplate自定义分页的小白实现(大神轻拍)
高手请看这里:http://blog.csdn.net/YUHEN78/article/details/5910317




我的分页代码:

从前台传来的值:

final String type(查询条件), final int recordsInOnePage(一页的数量), final int recordsCount(第几页)


DAO代码:


    public List<Goods> getGoodsList(final String type, final int recordsInOnePage, final int recordsCount) {

final List<Goods> result = new Vector<Goods>();
final int[] types = new int[]{Types.INTEGER, Types.VARCHAR};(参数类型)
PreparedStatementCreatorFactory psc = new PreparedStatementCreatorFactory("select top(?)* from Goods where GoodsType=? Order by goodsID", types);
psc.setResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE);
psc.setUpdatableResults(false);
PreparedStatementCreator ps = psc.newPreparedStatementCreator(new Object[]{recordsCount * recordsInOnePage, type});(添加参数)

return this.jdbcTemplate.execute(ps, new PreparedStatementCallback<List>() {
    @Override
    public List doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
int start = (recordsCount - 1) * recordsInOnePage;(开始位置)
int end = start + recordsInOnePage;(结束位置)
ps.setMaxRows(end ); (处理的数据量,控制输出记录最大数量)
ResultSet rs = ps.executeQuery();
rs.first();(移动游标到第一行)
rs.relative(start - 1);(移动游标到开始行集的位置)
while (rs.next()) {(处理得到的数据)
    Goods goods = new Goods(rs.getString("goodsID"), rs.getString("goodsName"), rs.getDouble("goodsPrice"), rs.getInt("goodsQuantity"), rs.getString("goodsType"), rs.getString("goodsPicture"), rs.getString("goodsDescription"));
    result.add(goods);
    System.out.println(goods.getGoodsID());
}
return result;
    }
});
    }