博主说:有时候,我们需要对数据库中现有的数据进行大量处理操作(例如表中的某个字段需要全部更新等),如果直接使用select * from tableName很容易出现问题,因此我们可以选择分页查询,批量处理数据。
DB2
SQL 语句
SELECT * FROM ( SELECT B.*, ROWNUMBER() OVER() AS TN FROM ( SELECT * FROM 表名 ) AS B ) AS A WHERE A.TN BETWEEN startNum AND endNum;
如上所示,此即为 DB2 的分页查询语句。
Mapper
Java
/** * Dao 层代码 */ @Repository("storeEntityDao") public interface StoreEntityDao { Listquery4encrypt(Map paramMap); } /** * Service 层接口代码 */ public interface StoreEntityService { public void query4encrypt(); } /** * Service 层实现代码 */ @Service("storeEntityService") public interface StoreEntityServiceImpl implements StoreEntityService { @Override public void query4encrypt() { boolean flag = true; Long startNum = 0L; Long endNum = 0L; Map paramMap = new HashMap (); while (flag) { endNum = startNum + 100; paramMap.put("startNum", startNum); paramMap.put("endNum", endNum); List storeEntityList = StoreEntityDao.query4encrypt(paramMap); if (storeEntityList != null && storeEntityList.size() > 0) { // 遍历加密数据 for (StoreEntity storeEntity : storeEntityList) { // 加密及持久化处理 } } if (storeEntityList != null && storeEntityList.size() >= 100) { startNum = endNum++; } else { flag = false; } } } }