spring jdbctemplate自定义分页的小白实现(大神轻拍)

本文详细介绍了如何使用Java和数据库进行分页查询,包括参数设置、预处理语句创建和执行过程,以及如何处理查询结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[color=red]高手请看这里:http://blog.youkuaiyun.com/YUHEN78/article/details/5910317

[/color]


我的分页代码:

从前台传来的值:

[color=red]final String type(查询条件), final int recordsInOnePage(一页的数量), final int recordsCount(第几页)
[/color]

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};[color=red](参数类型)[/color]
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});[color=red](添加参数)[/color]

return this.jdbcTemplate.execute(ps, new PreparedStatementCallback<List>() {
@Override
public List doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
int start = (recordsCount - 1) * recordsInOnePage;[color=red](开始位置)[/color]
int end = start + recordsInOnePage;[color=red](结束位置)[/color]
ps.setMaxRows(end ); [color=red](处理的数据量,控制输出记录最大数量)[/color]
ResultSet rs = ps.executeQuery();
rs.first();[color=red](移动游标到第一行)[/color]
rs.relative(start - 1);[color=red](移动游标到开始行集的位置)[/color]
while (rs.next()) {[color=red](处理得到的数据)[/color]
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;
}
});
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值