springboot入门
1.访问https://start.spring.io
Group一般为域名反写,Artifact填的是项目名
指定War包,这里用的是MySQL数据库和MyBatis持久层框架,所以添加依赖,最后点击Generate the project生成项目
然后,通过Eclipse的Import功能导入MavenExisting Maven Projects,如果项目没有开始自动更新,则对项目点右键,选择Maven > Update Project,在弹出的对话框中,勾选**Force update …**然后开始更新,并等待更新结束即可.
注意:推荐使用Eclipse Oxygen 2以上版本,如果使用的版本较低,例如使用Mars系列的版本,在pom.xml中可能会提示错误,原因是maven的相关配置的版本较低,该错误可以无视,不影响正常运行。
如图,展开项目,找到application.properties文件,配置数据库的连接
spring.datasource.url=jdbc:mysql://localhost:3306/tedu_store?useUnicode=true&characeterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
然后找到springbootApplication类,里面有main方法,直接运行即可
没报错表示运行成功,浏览器中直接访问http://localhost:8080/,默认显示的是index.html的内容
导出Excel功能的类
package cn.zkun.exportExcell;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.ibatis.javassist.expr.NewArray;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class Excel {
@RequestMapping("downExcel")
@ResponseBody
public String down(HttpServletRequest request, HttpServletResponse response) {
System.err.println("//");
List<Map<String, Object>> listresult = new ArrayList<Map<String, Object>>();
for (int j = 0; j < 10; j++) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 1; i < 5; i++) {
map.put("rowKey" + i, "第" + j + "行" + i + "列");
}
map.put("j", j);
listresult.add(map);
}
String name = downUserList(listresult, request, response);
return name;
}
/**
* 创建excel
*
* @param listresult 是需要写入excel中的数据,通过map中的k-v来将数据写入excel
* @return
*/
private XSSFWorkbook createUserListExcel(List<Map<String, Object>> listresult) {
// 1.创建HSSFWorkbook,一个HSSFWorkbook对应一个Excel文件
XSSFWorkbook wb = new XSSFWorkbook();
// 2.在workbook中添加一个sheet,对应Excel文件中的sheet
int count=0;
for (int k = 0; k <= listresult.size() / 3; k++) {
XSSFSheet sheet = wb.createSheet("测试表"+k);
// 创建一个居中的样式
XSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setBorderBottom((short) 1);
style.setBorderLeft((short) 1);
style.setBorderRight((short) 1);
style.setBorderTop((short) 1);
// 合并
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 4));
// 设置列宽
sheet.setColumnWidth(0, 2000);
sheet.setColumnWidth(1, 3000);
sheet.setColumnWidth(2, 4000);
sheet.setColumnWidth(3, 4000);
sheet.setColumnWidth(4, 8000);
// 3.设置表头,即每个列的列名
String[] titel = {"序号", "学院", "任课教师数", "调课次数", "调课原因" };
// 3.1创建第一行
XSSFRow row = sheet.createRow(0);
XSSFRow row0 = sheet.createRow(1);
XSSFCell cell = row.createCell(0);
cell.setCellValue("武汉理工大学调课情况统计表");
cell.setCellStyle(style);
// 将列名写入
for (int i = 0; i < titel.length; i++) {
// 给列写入数据,创建单元格,写入数据
cell = row0.createCell(i);
cell.setCellStyle(style);
cell.setCellValue(titel[i]);
}
// 写入正式数据
int num=0;
for (int i = 3*count; i < listresult.size(); i++) {
// 创建行
row0 = sheet.createRow(num%3+2);
num++;
// 序号
cell = row0.createCell(0);
cell.setCellStyle(style);
cell.setCellValue(i + 1);
// 学院列
cell = row0.createCell(1);
cell.setCellStyle(style);
cell.setCellValue(listresult.get(i).get("rowKey1").toString());
// 任课教师数列
cell = row0.createCell(2);
cell.setCellStyle(style);
cell.setCellValue(listresult.get(i).get("rowKey2").toString());
// 调课次数列
cell = row0.createCell(3);
cell.setCellStyle(style);
cell.setCellValue(listresult.get(i).get("rowKey3").toString());
// 调课原因列
cell=row0.createCell(4);
cell.setCellStyle(style);
cell.setCellValue(listresult.get(i).get("rowKey4").toString());
if(i==3*(count+1)-1) {
break;
}
}
count++;
}
return wb;
}
/**
* 用户列表导出
*
* @param userForm
*/
private String downUserList(List<Map<String, Object>> listresult, HttpServletRequest request,
HttpServletResponse response) {
String csvFile = "G:\\Excel";
// getTime()是一个返回当前时间的字符串,用于做文件名称
String name = new SimpleDateFormat().format(new Date()) + "2";
// csvFile是我的一个路径,自行设置就行
String ys = csvFile + "//" + name + ".xlsx";
String[] titel = {"序号", "学院", "任课教师数", "调课次数", "调课原因" };
String[] columnName= {"j","rowKey1","rowKey2","rowKey3","rowKey4"};
int[] columnWidth= {10*100,10*200,10*300,10*400,10*500};
// 1.生成Excel
HSSFWorkbook userListExcel =Export(listresult, 60000, "武汉理工大学调课情况统计表", titel, columnName, columnWidth);
OutputStream os = null;
try {
// 输出成文件
File file = new File(csvFile);
if (file.exists() || !file.isDirectory()) {
file.mkdirs();
}
// 生成的wb对象传输
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(ys, "UTF-8"));
response.setContentType("application/msexcel");
os = response.getOutputStream();
userListExcel.write(os);
} catch (Exception e) {
e.printStackTrace();
}finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return name;
}
/**
*
* @param list
* @param sum sheet数据条数
* @param header 表头
* @param titel 列名
* @param columnName list集合中map的key
* @param columnWidth 列宽
* @return
*/
public HSSFWorkbook Export(List list,int sum,String header,String[] titel,String[] columnName,int[] columnWidth) {
// 1.创建HSSFWorkbook,一个HSSFWorkbook对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 2.在workbook中添加一个sheet,对应Excel文件中的sheet
int count=0;//统计生成sheet数量
Object obj=null;
for (int k = 0; k <= list.size() / sum; k++) {
HSSFSheet sheet = wb.createSheet("等级考试成绩表"+(k+1));
// 创建一个居中的样式
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setBorderBottom((short) 1);
style.setBorderLeft((short) 1);
style.setBorderRight((short) 1);
style.setBorderTop((short) 1);
// 合并
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, titel.length-1));
// 设置列宽
for (int i = 0; i < columnWidth.length; i++) {
sheet.setColumnWidth(i, columnWidth[i]);
}
// 3.1创建第一行
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
cell.setCellValue(header);
HSSFRow row0 = sheet.createRow(1);
cell.setCellStyle(style);
// 将列名写入
for (int i = 0; i < titel.length; i++) {
// 给列写入数据,创建单元格,写入数据
cell = row0.createCell(i);
cell.setCellStyle(style);
cell.setCellValue(titel[i]);
}
// 写入正式数据
int num=0;
for (int i = sum*count; i < list.size(); i++) {
Map rw = (Map) list.get(i);
HSSFRow row2 = sheet.createRow(num%sum+2);
num++;
for (int j = 0; j < columnName.length; j++) {
cell = row2.createCell(j);
obj=rw.get(columnName[j]);
obj=obj==null?"":obj;
cell.setCellValue(obj.toString());
cell.setCellStyle(style);
}
if(i==sum*(count+1)-1) {
break;
}
}
count++;
}
return wb;
}
}
该类是一个控制器类,实际开发中框架的流程应该是:
控制器类调用业务类的方法,业务类调用持久接口的方法,持久接口对应访问数据库的xml文件.