sqlServer2008 分页

jndi方式:

基础SQL:select top 10
m.str_modelName,m.dt_creatTime,m.str_creatorName,m.str_standard,m.int_parentID from t_modelLevel m
where   m.int_isFact != 1  and m.str_standard ='gsm'
and m.int_ID not in
(select top 0 mm.int_ID from t_modelLevel mm where  int_isFact != 1 and  str_standard ='gsm' order by mm.dt_creatTime
)
order by m.dt_creatTime

=====================================

public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {

   int currPageIndex = 1;
   int jumpPageIndex = 1;
   int pageSize = 20;
   int pageCount = 0;

   int index = 0;
   Map<String, Object> modelMap = null;
   String sql = getQuerySql(standard, queryModelName,
     queryModelAuthor, currPageIndex, pageSize);

   String jndi = "java:comp/env/jndi/ibms/NetworkModelDatabase";
   try {
    conn = GetConnection.getConnection(jndi);
    ps = conn.prepareStatement(sql);
    rs = ps.executeQuery();
    while (rs.next()) {
     
     modelMap = new HashMap<String, Object>();
     index = index + 1;
     
     modelName = rs.getString("name");
     createTime = rs.getTimestamp("crdate");
     createModelDateStr = simpleDateFormat.format(createTime);
     createAuthor = rs.getString("str_creatorName");
     standard = rs.getString("str_standard");
     viewTimes = rs.getInt("int_viewTimes");
     parentID = rs.getInt("int_parentID");
     remark = rs.getString("str_modelSummary");
     levelName = getLevelNameByParentID(indisvr1,parentID);
     modelMap.put("index", String.valueOf(index));
     modelMap.put("remark", remark);
     modelMap.put("standard", standard);
     modelMap.put("viewTimes", viewTimes);
     modelMap.put("modelName", modelName);
     modelMap.put("createTime", createModelDateStr);
     modelMap.put("createAuthor", createAuthor);
     modelMap.put("levelName", levelName);
     
     modelNameList.add(modelMap);

    }

   } catch (Exception e) {
    e.printStackTrace();
   } finally {
    CloseConnection.closeConnection(rs, ps, conn);
   }

   pageCount = getPageCount(standard, queryModelName,queryModelAuthor, pageSize);

    request.setAttribute("pageSize", pageSize);
    request.setAttribute("pageCount", pageCount);
    request.setAttribute("modelList", modelNameList);
    request.setAttribute("currPageIndex", currPageIndex);

    return mapping.findForward(FORWARD_SUCCESS);

}

=============================================

/**
  * 拼接查询sql
  *
  * @param standard
  * @param queryModelName
  * @param queryModelAuthor
  * @return
  */
 private String getQuerySql(String standard, String queryModelName,String queryModelAuthor, int pageIndex, int pageSize) {

  int page = (pageIndex - 1) * pageSize;

  String sql = "select top "
    + pageSize
    + " s.name,s.crdate,m.str_creatorName,m.str_standard,m.int_viewTimes,m.int_parentID,m.str_modelSummary from sysobjects s "
    + "left join t_modelLevel m on m.str_modelName = s.name "
    + "where s.xtype='U' " + "and s.name != 't_comment' "
    + "and s.name != 't_modelAndIndicatorIndex' "
    + "and s.name != 't_modelLevel' "
    + "and s.name != 't_onlyIndicatorInfo' "
    + "and s.name != 't_primIndicatorInfomation' "
    + "and s.name != 't_referenceInformation' "
    + "and s.name != 't_resultSet' "
    + "and s.name != 't_tableAndColumnIndex' "
    + "and m.int_edit = 2";
  
  String sql2 = "select top " + page + " s.crdate from sysobjects s "
    + "left join t_modelLevel m on m.str_modelName = s.name "
    + "where s.xtype='U' " + "and s.name != 't_comment' "
    + "and s.name != 't_modelAndIndicatorIndex' "
    + "and s.name != 't_modelLevel' "
    + "and s.name != 't_onlyIndicatorInfo' "
    + "and s.name != 't_primIndicatorInfomation' "
    + "and s.name != 't_referenceInformation' "
    + "and s.name != 't_resultSet' "
    + "and s.name != 't_tableAndColumnIndex' "
    + "and m.int_edit = 2";

  if (!CheckUtils.isNullOrBlank(queryModelName)) {
   sql = sql + " and m.str_modelName like N'%" + queryModelName + "%'";
   sql2 = sql2 + " and m.str_modelName like N'%" + queryModelName
     + "%'";
  }
  if (!CheckUtils.isNullOrBlank(queryModelAuthor)) {
   sql = sql + " and m.str_creatorName like N'%" + queryModelAuthor
     + "%'";
   sql2 = sql2 + " and m.str_creatorName like N'%" + queryModelAuthor
     + "%'";
  }
  sql = sql + " and s.crdate not in (" + sql2
    + " order by s.crdate) order by s.crdate";
  return sql;

 }

===============================================================

// 获取总分页数
 private int getPageCount(String standard, String queryModelName,String queryModelAuthor, int pageSize) {
  int pageCount = 0;
  int count = 0;
  
  PreparedStatement ps = null;
  Connection conn = null;
  ResultSet rs = null;
  
  String jndi = "java:comp/env/jndi/ibms/NetworkModelDatabase";
  conn = GetConnection.getConnection(jndi);
  String sql = "select COUNT(*) from sysobjects s "
    + "left join t_modelLevel m on m.str_modelName = s.name "
    + "where s.xtype='U' " + "and s.name != 't_comment' "
    + "and s.name != 't_modelAndIndicatorIndex' "
    + "and s.name != 't_modelLevel' "
    + "and s.name != 't_onlyIndicatorInfo' "
    + "and s.name != 't_primIndicatorInfomation' "
    + "and s.name != 't_referenceInformation' "
    + "and s.name != 't_resultSet' "
    + "and s.name != 't_tableAndColumnIndex' "
    + "and m.int_edit = 2";
  
  if (!CheckUtils.isNullOrBlank(queryModelName)) {
   sql = sql + " and m.str_modelName like N'%" + queryModelName + "%'";

  }
  if (!CheckUtils.isNullOrBlank(queryModelAuthor)) {
   sql = sql + " and m.str_creatorName like N'%" + queryModelAuthor+ "%'";

  }
  try {
   ps = conn.prepareStatement(sql);
   rs = ps.executeQuery();
   while (rs.next()) {
    count = rs.getInt(1);
   }
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   CloseConnection.closeConnection(rs, ps, conn);
  }
  pageCount = count / pageSize;
  if (count % pageSize != 0) {
   pageCount = pageCount + 1;
  }

  return pageCount;
 }

 

 

===============================================================================

 

hibernate方式:

 

@Override
 public List findAllByPage(int pageSize,int currPageIndx,Map<String, String> parameters) {
  // TODO Auto-generated method stub
  int firstRecordIndex = (currPageIndx - 1) * pageSize;
  String noSeeAdmin = " and u.strUserRole != '4'";
  if(parameters.size()>0){
   noSeeAdmin = "";
  }
  String hql = " from TbUserInfoBak as u where 1=1 "+noSeeAdmin+" order by u.IUserId";
  
    Session   session   =   getSession();
    Query query = session.createQuery(hql);
    //query.setMaxResults(10);
    query.setFirstResult(firstRecordIndex);
    query.setFetchSize(pageSize);
    query.setMaxResults(pageSize);

    List list = query.list();
    session.close();
    return list;

   
 }

 @Override
 public int findAllCount(){
   int count = 0;
   String hql = "select count(u) from TbUserInfoBak u";
   Session   session   =   getSession();
   session.beginTransaction(); 
   Object obj = session.createQuery(hql).iterate().next();
   if(obj!=null){
   
    count =((Integer)obj).intValue();
   }
   session.close();
   return   count;
  

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值