springMVC Mybatis实现分页查询

首先在对应的Mapper.xml中配置:

<!--查询总记录的条数-->
<select id="findTotal" resultType="java.lang.Integer">
    SELECT COUNT(id) FROM Diary
</select>
<!--分页查询十条-->
<select id="pageShare" resultMap="seeShare">
    SELECT * FROM Diary limit #{startRow},10
</select>

对应的Mapper中配置:

@Mapper//说明这是个一个mapper
public interface DiaryMapper {
    public int insertDiary(Diary diary) throws Exception;

    public List<Diary> selectShare();

    public int findTotal();

    public List<Diary> pageShare(int startRow);
}

DAO层调用Mapper查询:

//获得记录总数
    public int findTotal(){
        DiaryMapper diaryMapper = sqlSession.getMapper(DiaryMapper.class);
        int total = diaryMapper.findTotal();
        return total;
    }

    //分页查询
    public List<Diary> pageShare(int startRow){
        List<Diary> list = new ArrayList<>();
        DiaryMapper diaryMapper = sqlSession.getMapper(DiaryMapper.class);
        list = diaryMapper.pageShare(startRow);
        return list;
    }

Service层调用DAO层,省略。

Controller层:

@RequestMapping(value = "/seeShare", method = RequestMethod.GET)
    public String seeshare(Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        List<Diary> pageList = new LinkedList<Diary>();
        //初始显示第一页
        pageList = userService.pageShare(0);
        model.addAttribute("pageList", pageList);
        int total = userService.findTotal();//总记录数
        int pageTotal = total % 10 == 0 ? total / 10 : total / 10 + 1;//根据总记录数算出总页数
        int pageArray[] = new int[pageTotal];//页数数组,供前端遍历
        for (int i = 1; i <= pageTotal; i++) {
            pageArray[i-1] = i;
        }
        session.setAttribute("pageArray",pageArray);
        return "/share/seeShare.html";
    }

    @RequestMapping(value = "/pageController",method = RequestMethod.GET)
    public String pageController(Model model, HttpServletRequest request){
        String pageNum = request.getParameter("pageNum");//要查询的页数
        List<Diary> pageList = new ArrayList<>();
        int startRow = (Integer.parseInt(pageNum) - 1) * 10;//当前页开始的行数
        pageList = userService.pageShare(startRow);
        model.addAttribute("pageList", pageList);
        System.out.println(pageList.toString());
        return "/share/seeShare.html";
    }

前端HTML代码:

<div class="form-group">
                <div th:if="${not #lists.isEmpty(pageList)}">
                <table>
                    <tr>
                        <td width="115" height="37">标题</td>
                        <td width="115" height="37">内容</td>
                        <td width="132">发表时间</td>
                        <td width="180">发表用户</td>
                        <td width="100">操作</td>
                    </tr>
                    <tr th:each="diary:${pageList}">
                        <td th:text="${diary.title}"></td>
                        <td th:text="${diary.fileName}"></td>
                        <td th:text="${diary.writeTime}"></td>
                        <td th:text="${diary.username}"></td>
                        <td><button class="btn btn-small btn-link" type="button" th:onclick="'javascript:download(\''+${diary.fileName}+'\')'">下载</button></td>
                    </tr>
                </table>
            </div>
            </div>
            <ul class="pagination pagination-centered" th:each="pageNum:${session.pageArray}">
                <li class="active"><a th:href="@{'/pageController?pageNum='+${pageNum}}" th:text="${pageNum}"></a></li>
            </ul>

界面显示结果:


猜你喜欢

转载自blog.csdn.net/wanderlustlee/article/details/80714478