一聚教程网:一个值得你收藏的教程网站

详述 DB2 分页查询及 Java 实现的示例

时间:2017-11-06 16:06:45 编辑:猪哥 来源:转载

博主说:有时候,我们需要对数据库中现有的数据进行大量处理操作(例如表中的某个字段需要全部更新等),如果直接使用select * from tableName很容易出现问题,因此我们可以选择分页查询,批量处理数据。

DB2

  • startNum:起始数
  • endNum:结尾数

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

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.hit.store.dao.StoreEntityDao" >
 <resultMap id="BaseResultMap" type="StoreEntity" >
  <id column="ID" property="id" jdbcType="BIGINT" />
  <result column="CREATE_TIME" property="createTime" jdbcType="TIMESTAMP" />
  <result column="OWNER" property="owner" jdbcType="VARCHAR" />
  <result column="DESCRIPTION" property="description" jdbcType="VARCHAR" />
 </resultMap>

 <select id="query4encrypt" parameterType="Map" resultMap="BaseResultMap">
  <!--- 在映射文件中 SQL 语句末尾不应该加分号,防止解析错误 --->
  SELECT * FROM 
  (
  SELECT B.*, ROWNUMBER() OVER() AS TN FROM  
  (
  SELECT * FROM TBL_STORE
  ) AS B
  ) AS A 
  WHERE A.TN BETWEEN #{startNum} AND #{endNum}
 </select>
</mapper>

Java

/**
 * Dao 层代码
 */
@Repository("storeEntityDao")
public interface StoreEntityDao {
  List<StoreEntity> query4encrypt(Map<String, Object> 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<String, Object> paramMap = new HashMap<String, Object>();
    while (flag) {
      endNum = startNum + 100;
      paramMap.put("startNum", startNum);
      paramMap.put("endNum", endNum);
      List<StoreEntity> 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;
      }
    }
  }
}

文章评论

热门栏目