分页查询
- controller
package com.lz.crowdfunding.manager.controller;
import com.lz.crowdfunding.bean.User;
import com.lz.crowdfunding.manager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.HashMap;
import java.util.List;
@Controller
@RequestMapping("/manag/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/list")
public String list(@RequestParam(required = false, defaultValue = "1") Integer pageno,
@RequestParam(required = false, defaultValue = "3") Integer pagesize,
Model model) {
HashMap<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("start", (pageno - 1) * pagesize);
paramMap.put("size", pagesize);
List<User> users = userService.queryPageUser(paramMap);
int totalsize = userService.queryPageCount();
int totalno = 0;
if (totalsize % pagesize == 0) {
totalno = totalsize / pagesize;
} else {
totalno = totalsize / pagesize + 1;
}
model.addAttribute("users", users);
model.addAttribute("totalno", totalno);
model.addAttribute("pageno", pageno);
return "/manag/user/list";
}
}
- Dao
<select id="queryPageUser" resultType="user">
select *
from t_user
limit #{start}, #{size}
</select>
<select id="querypageCount" resultType="int">
select count(*)
from t_user
</select>
- jsp
<table class="table table-bordered">
<thead>
<tr>
<th width="30">#</th>
<th width="30"><input type="checkbox"></th>
<th>账号</th>
<th>名称</th>
<th>邮箱地址</th>
<th width="100">操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${users}" varStatus="status" var="user">
<tr>
<td>${status.count}</td>
<td><input type="checkbox"></td>
<td>${user.loginacct}</td>
<td>${user.username}</td>
<td>${user.email}</td>
<td>
<button type="button" class="btn btn-success btn-xs"><i
class=" glyphicon glyphicon-check"></i></button>
<button type="button" class="btn btn-primary btn-xs"><i
class=" glyphicon glyphicon-pencil"></i></button>
<button type="button" class="btn btn-danger btn-xs"><i
class=" glyphicon glyphicon-remove"></i></button>
</td>
</tr>
</c:forEach>
</tbody>
<tfoot>
<tr>
<td colspan="6" align="center">
<ul class="pagination">
<c:if test="${pageno != 1}">
<li><a style="cursor: pointer" onclick="changePageno(${pageno-1})">上一页</a>
</li>
</c:if>
<c:forEach begin="1" end="${totalno}" varStatus="status">
<li
<c:if test="${pageno == status.count}">
class="active"
</c:if>
><a style="cursor:pointer;"
onclick="changePageno(${status.count})">${status.count}</a></li>
</c:forEach>
<c:if test="${pageno != totalno}">
<li><a style="cursor: pointer" onclick="changePageno(${pageno+1})">下一页</a>
</li>
</c:if>
</ul>
</td>
</tr>
</tfoot>
</table>
<script>
function changePageno(pageno) {
window.location.href = "${APP_PATH}/manag/user/list.htm?pageno=" + pageno;
}
</script>