SSM框架实现分页,返回分页的JSON数据

先看一下返回的JSON数据:

{
    "pagNum": 1,
    "pageSize": 5,
    "totalRecord": 12,
    "totalPage": 3,
    "startIndex": 0,
    "dateList": [
        {
            "employee_id": 2,
            "employee_account": "44",
            "username": "44",
            "password": "44",
            "age": 44,
            "address": "0000",
            "departmentname": "生产技术部"
        },
        {
            "employee_id": 3,
            "employee_account": "44",
            "username": "44",
            "password": "44",
            "age": 44,
            "address": "0000",
            "departmentname": "生产技术部"
        },
        {
            "employee_id": 4,
            "employee_account": "897",
            "username": "张红",
            "password": "0989",
            "age": 45,
            "address": "河北省石家庄市公园路45号",
            "departmentname": "系统服务部"
        },
        {
            "employee_id": 6,
            "employee_account": "6767",
            "username": "龙飞",
            "password": "9889789",
            "age": 24,
            "address": "江苏省南京市人民路88号",
            "departmentname": "广告部"
        },
        {
            "employee_id": 10,
            "employee_account": "11111111",
            "username": "测试一",
            "password": "1111111",
            "age": 11111,
            "address": "四川成都市",
            "departmentname": "广告部"
        }
    ],
    "start": 1,
    "end": 3
}

所用到的几个类:
* Employee.java:实体类
* Page.java:分页类
* EmployeeDao.java:持久层接口
* EmployeeMapping.xml:数据库映射文件
* EmployeeService.java:服务层接口
* EmployeeServiceImpl.java:服务层接口实现类
* EmployeeController.java:控制器类

起主要作用的是Page类:

    private int pagNum;// 当前的页数
    private int pageSize;// 每页显示数量,limit函数第二个参数
    private int totalRecord;// 总记录数
    private int totalPage;// 总页数
    private int startIndex;// 开始位置,limit函数第一个参数
    private List<T> dateList;
    private int start;
    private int end;

    public Page() {
    }

    public Page(int pagNum, int pageSize, int totalRecord) {
        this.pagNum = pagNum;
        this.pageSize = pageSize;
        this.totalRecord = totalRecord;
        // 计算总页数
        if (totalRecord % pageSize == 0) {
            this.totalPage = totalRecord / pageSize;
        } else {
            this.totalPage = (totalRecord / pageSize) + 1;
        }
        // 确定limit函数的第一个参数的值
        this.startIndex = pagNum * pageSize;
        this.start = 1;
        this.end = 5;
        if (totalPage <= 5) {
            this.end = this.totalPage;
        } else {// pagNum=6;start=4;end=8
            this.start = pagNum - 2;
            this.end = pagNum + 2;
        }
        if (start < 0) {
            this.start = 1;
            this.end = 5;
        }
        // 举个例子:一共有10页,现在就在第10页,那么根据上面的逻辑end=12,错误;则end=this.totalPage;且下面导航栏还是要出现5个,则start=end-5
        if (end > this.totalPage) {
            this.end = totalPage;
            this.start = end - 5;
        }
    }

从数据库获取相关信息:

  • dao接口:
public interface EmployeeDao {
    //获取数据的总条数,即totalRecord
    int getEmployeeTotal();
    //获取指定的数据
    List<Employee> getAllEmployee(Page<Employee> page);
}
  • mapping文件:
<?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" >
<mapper namespace="com.example.oa.dao.EmployeeDao">
    <select id="getEmployeeTotal" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM
        employee
    </select>
    <select id="getAllEmployee"
        resultType="com.example.oa.domain.Employee"
        parameterType="com.example.oa.domain.Page">
        SELECT
        e.employee_id,
        e.employee_account,
        e.username,
        e.`password`,
        e.age,
        e.address,
        d.departmentname
        FROM
        employee e,
        department d
        WHERE
        e.departmentid = d.departmentid LIMIT
        #{startIndex},#{pageSize}
    </select>
</mapper>
  • service层接口:
public interface EmployeeService {
    //返回分页数据和主题信息
    Page<Employee> getEmployee(Page<Employee> page);
}
  • service实现类:
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeDao employeeDao;
    private Logger log = Logger.getLogger(EmployeeServiceImpl.class);

    @Override
    public Page<Employee> getEmployee(Page<Employee> page) {
        //获取总条数,即totalRecord
        int total = employeeDao.getEmployeeTotal();
        Page<Employee> allpage = new Page<Employee>(page.getPagNum(), page.getPageSize(), total);
        log.info("allpage:" + allpage.toString());
        //将获取到的集合数据,添加到page中的datalist参数中
        allpage.setDateList(employeeDao.getAllEmployee(allpage));
        //返回所有的分页参数和数据
        return allpage;
    }

}
  • 控制层:
@Controller
@RequestMapping(value = "/employeeController")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @RequestMapping(value = "/getAll")
    @ResponseBody
    public Page<Employee> getAll(Page<Employee> page) {
        Page<Employee> page1 = employeeService.getEmployee(page);
        return page1;
    }
}
  • 数据表的话,自己建一张表就行了(以下是我的SQL)
CREATE TABLE `employee` (
  `employee_id` int(11) NOT NULL AUTO_INCREMENT,
  `employee_account` varchar(255) NOT NULL,
  `username` varchar(50) NOT NULL,
  `password` varchar(100) NOT NULL,
  `age` int(11) DEFAULT NULL,
  `address` varchar(255) DEFAULT NULL,
  `departmentid` int(11) DEFAULT NULL,
  `role` int(3) NOT NULL,
  PRIMARY KEY (`employee_id`),
  KEY `departmentid` (`departmentid`) USING BTREE,
  CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`departmentid`) REFERENCES `department` (`departmentid`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8 COMMENT='员工表';

至此,SSM框架的分页功能实现完成。完整代码以上传到码云,有需要请下载:Paging

参考博文:

Java Web(十一) 分页功能的实现
Java 实现分页功能

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lhc_makefunny/article/details/80340418
文章标签: SSM框架 分页
上一篇加密方法初探
下一篇Spring Boot中使用Log4J日志输出Mybatis的查询SQL
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭