• SpringMVC+MyBatis+EasyUI 实现分页查询


    user_list.jsp

    <%@ page import="com.ssm.entity.User" %>
    <%@ page pageEncoding="UTF-8" import="java.util.List" %>
    <html>
    <head>
        <%@ include file="meta.jsp" %>
    
        <meta charset="UTF-8">
        <title>用户列表</title>
        <link rel="stylesheet" type="text/css" href="jquery-easyui-1.4/themes/default/easyui.css">
        <link rel="stylesheet" type="text/css" href="jquery-easyui-1.4/themes/icon.css">
        <script type="text/javascript" src="jquery-easyui-1.4/jquery.min.js"></script>
        <script type="text/javascript" src="jquery-easyui-1.4/jquery.easyui.min.js"></script>
    
    </head>
    <body>
        <table id="dataTable"  style="400px;height:250px">
            <thead>
            <tr>
                <th data-options="field:'id'">ID</th>
                <th data-options="field:'name'">姓名</th>
                <th data-options="field:'age'">年龄</th>
            </tr>
            </thead>
        </table>
    <a href="/ssm">回到首页</a>
    </body>
    <script>
        $(document).ready(function() {
            page_init();
        });
        function page_init() {
    
            $("#dataTable").datagrid({
                url: "listUserWithPageForJson", //actionName
                queryParams:{pageNumber:2,pageSize:10},//查询参数
                loadMsg:"正在加载数据...",
                rownumbers:true,//查询结果在表格中显示行号
                fitColumns:true,//列的宽度填满表格,防止下方出现滚动条。
                pageNumber:2,   //初始页码,得在这设置才效果,pagination设置没效果。
                pagination: true//分页控件
                //如果后端返回的json的格式直接是data={total:xx,rows:{xx}},不需要设置loadFilter了,
                //如果有多层封装,比如data.jsonMap = {total:xx,rows:{xx}},则需要在loadFilter处理一下。
                /*
                loadFilter: function(data){
                    if(data.jsonMap) {
                        return data.jsonMap;
                    }
                }*/
            });
    
            var p = $('#dataTable').datagrid('getPager');
            $(p).pagination({
                pageSize: 10,//每页显示的记录条数,默认为10
                pageList: [5,10,15,20],//可以设置每页记录条数的列表
                beforePageText: '',//页数文本框前显示的汉字
                afterPageText: '页    共 {pages} 页',
                displayMsg: '共 {total} 条记录',
                onSelectPage: function (pageNumber, pageSize) {//分页触发
                    find(pageNumber, pageSize);
                }
            });
    
        }
    
        function find(pageNumber, pageSize)
        {
                $("#dataTable").datagrid('getPager').pagination({pageSize : pageSize, pageNumber : pageNumber});//重置
                $("#dataTable").datagrid("loading"); //加屏蔽
                $.ajax({
                    type : "POST",
                    dataType : "json",
                    url : "listUserWithPageForJson",
                    data : {
                        pageNumber : pageNumber,
                        pageSize : pageSize
                    },
                    success : function(data) {
                        $("#dataTable").datagrid('loadData',data);
                        $("#dataTable").datagrid("loaded"); //移除屏蔽
                    },
                    error : function(err) {
                        $.messager.alert('操作提示', '获取信息失败...请联系管理员!', 'error');
                        $("#dataTable").datagrid("loaded"); //移除屏蔽
                    }
                });
    
        }
    
    
    </script>
    </html>

    UserController.java

    package com.ssm.controller;
    
    import com.alibaba.fastjson.JSONObject;
    import com.ssm.common.PageModel;
    import com.ssm.dao.UserDao;
    import com.ssm.entity.JsonEntity;
    import com.ssm.entity.User;
    import com.ssm.service.UserService;
    import com.ssm.tools.BlankUtil;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.servlet.mvc.support.RedirectAttributes;
    
    import javax.annotation.Resource;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    /**
     * Created by Administrator on 2015-10-15.
     */
    @Controller
    public class UserController {
    
    
        @Resource
        private UserService userService;
    
        @RequestMapping(value = "listUserWithPageForJson")
        @ResponseBody
        public Object listUserWithPageForJson(PageModel pageModel,User user) {
            Object jsonObject = null;
            try {
                pageModel.setQueryObj(user);
                userService.listUserWithPage(pageModel);
                jsonObject = JSONObject.toJSON(pageModel);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return jsonObject;
        }
    
    
    }

    PageModel.java

    package com.ssm.common;
    
    import java.util.List;
    
    /**
     * Created by hbd on 2015/10/22.
     */
    public class PageModel<T> {
    
        private Integer pageNumber; //当前页数
        private Integer pageSize;   //一页显示数量
        private Integer startRow;   //查询起始行
        private Integer total;      //总记录行数
        private List<T> rows;       //查询结果数据
        private T queryObj;         //查询对象
    
        public Integer getStartRow() {
            if(pageNumber!=null && pageSize!=null) {
                return (pageNumber - 1) * pageSize;
            } else {
                return 0;
            }
        }
    
        public Integer getPageNumber() {
            return pageNumber;
        }
    
        public void setPageNumber(Integer pageNumber) {
            this.pageNumber = pageNumber;
        }
    
        public Integer getPageSize() {
            return pageSize;
        }
    
        public void setPageSize(Integer pageSize) {
            this.pageSize = pageSize;
        }
    
        public Integer getTotal() {
            return total;
        }
    
        public void setTotal(Integer total) {
            this.total = total;
        }
    
        public List<T> getRows() {
            return rows;
        }
    
        public void setRows(List<T> rows) {
            this.rows = rows;
        }
    
        public void setQueryObj(T queryObj) {
            this.queryObj = queryObj;
        }
    
        public T getQueryObj() {
            return queryObj;
        }
    }

    UserServiceImpl.java

    package com.ssm.service.impl;
    
    import com.ssm.common.PageModel;
    import com.ssm.dao.UserDao;
    import com.ssm.entity.User;
    import com.ssm.service.UserService;
    import org.springframework.stereotype.Service;
    
    import javax.annotation.Resource;
    import java.util.List;
    
    /**
     * Created by Administrator on 2015-10-16.
     */
    @Service("userService")
    public class UserServiceImpl implements UserService {
    
        @Resource
        private UserDao userDao;
        
        @Override
        public User getUserById(int userId) {
            return userDao.findUserById(userId);
        }
    
        @Override
        public void listUserWithPage(PageModel<User> pageModel) {
            pageModel.setRows(userDao.selectUserListWithPage(pageModel));
            pageModel.setTotal(userDao.selectUserCountWithPage(pageModel));
        }
    
    }

    UserMapper.xml

    <?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">
    <!--这块等于dao接口的实现 namespace必须和接口的类路径一样-->
    <mapper namespace="com.ssm.dao.UserDao">
    
        <sql id="sql_count">
            SELECT COUNT(*)
        </sql>
        <sql id="sql_select">
            SELECT *
        </sql>
        <sql id="sql_where">
            FROM USER
            <where>
                <if test="queryObj != null">
                    <if test="queryObj.name != null and queryObj.name != ''">
                        AND name like CONCAT('%',#{queryObj.name},'%')
                    </if>
                    <if test="queryObj.age != null">
                        AND age = #{queryObj.age}
                    </if>
                </if>
            </where>
        </sql>
    
        <select id="selectUserListWithPage" parameterType="com.ssm.common.PageModel" resultType="User">
            <include refid="sql_select"></include>
            <include refid="sql_where"></include>
            <if test="pageNumber != null and  pageSize!= null">
                limit #{startRow},#{pageSize}
            </if>
        </select>
    
        <select id="selectUserCountWithPage" parameterType="com.ssm.common.PageModel" resultType="Integer">
            <include refid="sql_count"></include>
            <include refid="sql_where"></include>
        </select>
    
        
    
    </mapper>
  • 相关阅读:
    使用 Spring 2.5 注释驱动的 IoC 功能
    分页、排序SQL 通用存储过程(转)
    MySQL中group_concat函数
    文本内容分页
    jQuery Pagination Ajax分页插件中文详解(转)
    Cesium组件:Cesiumdraw,基于Vue开发的Cesium基础标绘插件
    tileset格式详解Part2
    css实现div的隐藏
    eltable不透明度&边框
    1rem,1em,1vh,1px含义
  • 原文地址:https://www.cnblogs.com/cocoat/p/4903481.html
Copyright © 2020-2023  润新知