第一次使用velocity模板,springjdbc的jdbcTemplate.queryForList获取的list是List<Map<String, Object>>类型,而不是常用的List<Object>类型,简单的写了一下数据显示以及分页。
velocity分页简单代码:
#foreach($ele in $pageBean) <tr #if($foreach.index%2==0) class="even" #else class="odd" #end> #foreach($param in ${ele.keySet()}) <td> #if(${ele.get($param)}) ${ele.get($param)} #end </td> #end </tr> #end </table> 共$!allRow条记录 #if($allpage>1) 共$!allpage页 #set($prepage=$page - 1) #set($nextpage=$page + 1) <a href="$contextPath/data/list.do?cunId=$lastcun.cunId&page=1"><span>第一页</span></a> #if($page <= 1 ) <span>上一页</span> #else <a href="$contextPath/data/list.do?cunId=$lastcun.cunId&page=$prepage"><span> 上一页 </span></a> #end 第$!page页 #if($page >= $allpage ) <span>下一页</span> #else <a href="$contextPath/data/list.do?cunId=$lastcun.cunId&page=$nextpage"><span>下一页</span></a> #end <a href="$contextPath/data/list.do?cunId=$lastcun.cunId&page=$allpage"><span>最后一页</span></a> #end
springmvc部分代码:
int pageSize= 10; String page = request.getParameter("page"); int pageInt = 1; if (page != null) { pageInt = Integer.parseInt(page); if(pageInt <1) pageInt = 1 ; } // 获取数据 List pageBean = dataService.getRowsOfCun(cunId ,pageInt ,pageSize); int allRow = dataService.getRowCountOfCun(cunId); int allpage = (allRow % pageSize == 0) ? (allRow / pageSize) : (allRow / pageSize + 1); mv.addObject("pageBean", pageBean); mv.addObject("page", pageInt); mv.addObject("allRow", allRow); mv.addObject("allpage", allpage);
springjdbc部分代码
public List getRowsOfCun(final String cunId) { List<Map<String, Object>> rows = jt .queryForList( "select name from data where cunId=?", new Object[] {cunId}); return rows; } public List getRowsOfCun(String cunId, int pageInt, int pageSize) { if(getRowsOfCun(cunId).size()==0) { return null; } List<Map<String, Object>> rows = jt .queryForList( //mysql版本 /** "select name from data where cunId=? limit ?,?;", new Object[] {cunId,(pageInt * pageSize - pageSize),pageSize}); */ //ms sql版本 "select TOP (?) name from data where (id NOT IN (SELECT TOP (?) id from data where cunId = ? ORDER BY id) ) and cunId = ? ORDER BY ID", new Object[] {pageSize,(pageInt * pageSize - pageSize),cunId,cunId}); return rows; }