源头质量 PageHelper(分页),导出功能

Posted

Mr.Duanxj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了源头质量 PageHelper(分页),导出功能相关的知识,希望对你有一定的参考价值。

今天星期五,本来想直接关电脑走人的,但想想自己弄出来的,写写留个记忆吧。两个功能 导出 Mybatis的插件 PageHelper 分页

一,导出功能代码实现:这里是需要jar包的啊

<!--poi-->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>3.14</version>
    </dependency>

 

前端:

<div>
        <form id="searchFrom">
            用户姓名:<input type="text" name="userName" class="easyui-validatebox"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <a id="serach" class="easyui-linkbutton" iconCls="icon-search" onclick="serach();">查询</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <a id="export" class="easyui-linkbutton" iconCls="icon-edit" onclick="exportFrom();">导出</a>
        </form>
    </div>

<!--js部分-->
//导出
    function exportFrom() {
        $("#searchFrom").attr({action: "${ctx}/user/exportExcel"}).submit();
    }

后台代码:

//导出
    @RequestMapping(value = "exportExcel")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response,People p) {
        List<People> userInfo = userInfoService.findByConditions(p);
        // 第一步,创建一个webbook,对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("用户信息");
        // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFRow row = sheet.createRow(0);
        // 第四步,创建单元格,并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

        // 设置表头
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("账号");
        cell.setCellStyle(style);
        cell = row.createCell( 1);
        cell.setCellValue("密码");
        cell.setCellStyle(style);
        cell = row.createCell( 2);
        cell.setCellValue("真实姓名");
        cell.setCellStyle(style);
        cell = row.createCell(3);
        cell.setCellValue("性别");
        cell.setCellStyle(style);
        cell = row.createCell(4);
        cell.setCellValue("家庭住址");
        cell.setCellStyle(style);
        cell = row.createCell(5);
        cell.setCellValue("电话");
        cell.setCellStyle(style);
        cell = row.createCell(6);
        cell.setCellValue("工作");
        cell.setCellStyle(style);
        cell = row.createCell(7);
        cell.setCellValue("备注");
        cell.setCellStyle(style);



        for (int i = 0; i < userInfo.size(); i++) {
            row = sheet.createRow((int) i + 1);
            People people = userInfo.get(i);

            if (StringUtils.isNotEmpty(people.getUserName())) {
                row.createCell(0).setCellValue(people.getUserName());
            }

            if (StringUtils.isNotEmpty(people.getPassword())) {
                row.createCell(1).setCellValue(people.getPassword());
            }

            if (StringUtils.isNotEmpty(people.getRealName())) {
                row.createCell(2).setCellValue(people.getRealName());
            }

            if (StringUtils.isNotEmpty(people.getSex())) {
                row.createCell(3).setCellValue(people.getSex());
            }

            if (StringUtils.isNotEmpty(people.getAddress())) {
                row.createCell(4).setCellValue(people.getAddress());
            }
            row.createCell(5).setCellValue(people.getPhone());
            row.createCell(6).setCellValue(people.getJob());
            row.createCell(7).setCellValue(people.getBL01());
        }
        // 第六步,将文件配置
        try {
            Date d = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("YYYYmmDDHHmmss");
            String fileName = sdf.format(d) + ".xls";
            fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);// 指定下载的文件名
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            OutputStream output = response.getOutputStream();
            wb.write(output);
            output.flush();
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

二,Mybatis的插件 PageHelper 分页

先说配置文件

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:mappers/*.xml"/>
        <property name="typeAliasesPackage" value="cn.test.model"/>
<!--这里就是 PageHelper 配置--> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageHelper"> <property name="properties"> <value> dialect=oracle offsetAsPageNum=true pageSizeZero=true rowBoundsWithCount=true reasonable=true </value> </property> </bean> </array> </property> </bean>

Controller层:

    @ResponseBody
    @RequestMapping("/userList")
    public PageBean userListToJson(People userInfo, Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        List<People> userInfoList = userInfoService.findAll(userInfo);
        int total = userInfoService.getTotal();
        PageBean pageBean = new PageBean();
        pageBean.setTotal(total);
        pageBean.setRows(userInfoList);
        return pageBean;
    }
PageBean 类:
package cn.test.model;

import java.util.List;

public class PageBean {
    private  int total;  //总数
    private List rows;  //数据集合

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public List getRows() {
        return rows;
    }

    public void setRows(List rows) {
        this.rows = rows;
    }
}

是不是很简单呀。哈哈,下班了,以后看到了再改改。感觉写的有点草率

以上是关于源头质量 PageHelper(分页),导出功能的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot2.0系列教程Springboot框架添加PageHelper分页查询功能

Spring AOP 实现PageHelper自动分页

SpringBoot整合Pagehelper分页插件

分页插件:PageHelper+BootStrap+Vue+axios实现分页功能

pageHelper----Mybaits分页插件

Mybatis的分页插件PageHelper分页失效的原因