使用HSSFWorkbook导出数据库中的数据
导入Apache POI Maven jar包
<!-- Apache POI -->
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.6</version>
</dependency>
在控制层编写表单导出函数
/*** 将数据库里面的报名成员的数据以excel表格的形式打印出来* @param response* @throws IOException*/@RequestMapping(value = "/ExcelDownloads")public void downloadAllMember(HttpServletResponse response) throws IOException {HSSFWorkbook workbook = new HSSFWorkbook();HSSFSheet sheet = workbook.createSheet("信息表");List<Member> memberList = memberService.findAll();//设置要导出的文件的名字String fileName = "member" + ".xls";//新增数据行,并且设置单元格数据int rowNum = 1;//headers表示excel表中第一行的表头String[] headers = { "序号", "姓名", "性别", "学号","电话号码","专业班级","分组意向"};//在excel表中添加表头HSSFRow row = sheet.createRow(0);for(int i=0;i<headers.length;i++){HSSFCell cell = row.createCell(i);HSSFRichTextString text = new HSSFRichTextString(headers[i]);cell.setCellValue(text);}//在表中存放查询到的数据放入对应的列for (Member member : memberList) {HSSFRow row1 = sheet.createRow(rowNum);row1.createCell(0).setCellValue(member.getId());row1.createCell(1).setCellValue(member.getName());row1.createCell(2).setCellValue(member.getSex());row1.createCell(3).setCellValue(member.getNumber());row1.createCell(4).setCellValue(member.getPhone());row1.createCell(5).setCellValue(member.getMajor());row1.createCell(6).setCellValue(member.getIntention());rowNum++;}response.setContentType("application/octet-stream");response.setHeader("Content-disposition", "attachment;filename=" + fileName);response.flushBuffer();workbook.write(response.getOutputStream());}
注意:导出表单的名字不能为中文,否则会出现导出的表单文件无名字的情况
springboot自定义http反馈状态码
这个是前端朋友要求设置的…一开始设置还设置错了,导致并没有起作用…
ResponseEntity :标识整个http相应:状态码、头部信息、响应体内容(spring)
/*** 配置类,判断注册结果的准确与否*/
public class ConfigResult {public static ResponseEntity<Map<String,Object>> success(){Map<String,Object> map = new HashMap<>();map.put("data", "success");map.put("code","200");return new ResponseEntity<>(map, HttpStatus.OK);}public static ResponseEntity<Map<String,Object>> error(){Map<String,Object> map = new HashMap<>();map.put("data", "error");map.put("code","404");return new ResponseEntity<>(map, HttpStatus.NOT_FOUND);}
}
常用HttpStatus状态:
- HttpStatus.OK = 200;
- HttpStatus.BAD_REQUEST = 400;
- HttpStatus.FORBIDDEN = 403;
- HttpStatus.NOT_FOUND = 404;
- HttpStatus.REQUEST_TIMEOUT = 408;
- HttpStatus.SERVICE_UNAVAILABLE =500;
跨域问题
出于安全原因,浏览器禁止Ajax调用驻留在当前原点之外的资源。跨域,指的是浏览器不能执行其他网站的脚本。它是由浏览器的同源策略造成的,是浏览器对JavaScript施加的安全限制。
所谓同源是指,域名,协议,端口均相同,不明白没关系,举个栗子:
http://www.123.com/index.html 调用 http://www.123.com/service.PHP (非跨域)
http://www.123.com/index.html 调用 http://www.456.com/service.php (主域名不同:123/456,跨域)
http://abc.123.com/index.html 调用 http://def.123.com/service.php(子域名不同:abc/def,跨域)
http://www.123.com:8080/index.html调用 http://www.123.com:8081/sevice.php(端口不同:8080/8088,跨域)
http://www.123.com/index.html 调用 https://www.123.com/service.php(协议不同:http/https,跨域)
请注意:localhost和127.0.0.1虽然都指向本机,但也属于跨域。
浏览器执行javascript脚本时,会检查这个脚本属于哪个页面,如果不是同源页面,就不会被执行。
@CrossOrigin注解最简单的使用方法,直接加在@RestController前面,如下图:
注意:springMVC的版本要在4.2或以上版本才支持@CrossOrigin
关于主键自增(@GeneratedValue注解)
使用了@GeneratedValue 注解使得主键由数据库自己增长,如果不设置这个的话,即使数据库已经设置了主键递增,但还是会报错(实际上数据库并没有执行主键递增操作)。
springboot中@GeneratedValue作用:
(1)@GeneratedValue注解存在的意义主要就是为一个实体生成一个唯一标识的主键,@GeneratedValue提供了主键的生成策略;
(2)@GeneratedValue注解有两个属性,分别是strategy和generator。
strategy属性:提供四种值:
-AUTO主键由程序控制, 是默认选项 ,不设置就是这个-IDENTITY 主键由数据库生成, 采用数据库自增长, Oracle不支持这种方式-SEQUENCE 通过数据库的序列产生主键, MYSQL 不支持-Table 提供特定的数据库产生主键, 该方式更有利于数据库的移植
使用方法如下图: