今天星期五,本来想直接关电脑走人的,但想想自己弄出来的,写写留个记忆吧。两个功能 导出 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(分页),导出功能的更多相关文章

  1. java 分页导出百万级数据到excel

    最近修改了一个导出员工培训课程的历史记录(一年数据),导出功能本来就有的,不过前台做了时间限制(只能选择一个月时间内的),还有一些必选条件, 导出的数据非常有局限性.心想:为什么要做出这么多条件限制呢 ...

  2. Springboot 系列(十二)使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件

    前言 在 Springboot 系列文章第十一篇里(使用 Mybatis(自动生成插件) 访问数据库),实验了 Springboot 结合 Mybatis 以及 Mybatis-generator 生 ...

  3. PageHelper分页插件的使用

    大家好!今天写ssm项目实现分页的时候用到pageHelper分页插件,在使用过程中出现了一些错误,因此写篇随笔记录下整个过程 1.背景:在项目的开发的过程中,为了实现所有的功能. 2.目标:实现分页 ...

  4. 记录pageHelper分页orderby的坑

    pageHelper的count查询会过滤查询sql中的order by条件! pageHelper分页功能很强大,如果开启count统计方法,在你执行查询条件时会再执行一条selet count(* ...

  5. pageHelper分页

    引入jar包 <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pag ...

  6. SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件

    原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...

  7. 如何在实际项目中使用PageHelper分页插件

    PageHelper是一个分页插件,能够简单快速的帮助开发人员完成常见的分页功能,你只需要简单的使用两行代码就可以完成一个分页效果- 最近做一个科创项目,使用Maven+SSM的环境,有分页的功能,于 ...

  8. 【HOW】如何限制Reporting Services报表导出功能中格式选项

    Reporting Services报表导出功能中缺省会提供多种导出格式选项,但很多情况下不需要全部的格式选项,因此需要对这些选项进行限制.下面我们以SQL Server 2008 R2为例来说明对这 ...

  9. Atitit.excel导出 功能解决方案 php java C#.net版总集合.doc

    Atitit.excel导出 功能解决方案 php java C#.net版总集合.docx 1.1. Excel的保存格式office2003 office2007/2010格式1 1.2. 类库选 ...

随机推荐

  1. Javaweb项目不需要端口号及项目名的访问配置(已备注)

    1.不需要端口号的配置: 在server.xml中找到节点: <Connector URIEncoding="UTF-8" connectionTimeout="2 ...

  2. 【玩转SpringBoot】SpringBoot应用的启动过程一览表

    SpringBoot应用的启动方式很简单,就一行代码,如下图01: 其实这行代码背后主要执行两个方法,一个是构造方法,一个是run方法. 构造方法主要内容就是收集一些数据,和确认一些信息.如下图02: ...

  3. java 数字转换格式化

    1.小数格式化后转字符串百分数(带%)输出 NumberFormat nt = NumberFormat.getPercentInstance(); //设置百分数精确度2即保留两位小数 nt.set ...

  4. mybatis(六):设计模式 - 装饰器模式

  5. mysql-sql逻辑查询顺序

    1.sql逻辑执行顺序(物理执行顺序可能会因索引而不同) SELECT  7  DISTINCT 8 FROM  1 JOIN     2 ON      3 WHERE  4 GROUP BY 5 ...

  6. Java-POJ1007-DNA Sorting

    题目大意: 你的任务是分类DNA字符串(只有ACGT四个字符,所有字符串长度相同). 根据逆序数,排序程度从好到差. 第一次用到了“类”,和c++里的结构体有类似之处 一次AC,简单暴力的冒泡排序,要 ...

  7. [Linux] Ubuntu18.04 安装之后配置过程

    前言  最终还是安装了Ubuntu 虽然一开始想用Deepin 但是死活安装不上,我也很绝望(引导一直找不到 恢复BIOS默认设置也还是不行 所以 有些绝望啊....)  所以 为了让以后的我不再那么 ...

  8. HTML5学习(2)语义化

    什么是语义化? 1.每一个HTML元素都有具体的含义,例: a元素:超链接,p元素:段落 2.所有的元素与展示效果无关 元素内容展示到页面中的效果,应该由CSS决定. 因为浏览器带有默认的CSS样式, ...

  9. 找不到方法:'System.Net.Http.HttpRequestMessage System.Web.Http.Controllers.HttpActionContext.get_Request()'

    出现这种情况一般是引用了.net standard 库解决方案1:在web.config 文件中增加如下节点 <dependentAssembly> <assemblyIdentit ...

  10. TCP的状态转换

    TCP的状态转换图 手写的状态转换图 一.服务端状态变迁:​ 服务端创建套接字之后调用listen函数将套接字有一个未连接的主动套接字转换为被动套接字,指示内核应接受指向该套接字的连接请求,套接字状态 ...