@Service
public class StudentServiceImpl implements IStudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public List selectAllStudent() {
// TODO Auto-generated method stub
List allStudent = studentMapper.selectAllStudent();
return allStudent;
}
@Override
public void deleteStudent(Integer studentId) {
studentMapper.deleteStudent(studentId);
}
@Override
public List selectByList() {
return studentMapper.selectByList();
}
@Override
public void insertSelective(Student student) {
studentMapper.insertSelective(student);
}
@Override
public Student selectByPrimaryKey(Integer studentId) {
return studentMapper.selectByPrimaryKey(studentId);
}
@Override
public void updateByPrimaryKey(Student student) {
studentMapper.updateByPrimaryKey(student);
}
//统计
@Override
public List selectAllgrade() {
return studentMapper.selectAllgrade();
}
//修改排序
@Override
public PageInfo studentList(int currentPage, int pageSize, Map params) {
// TODO Auto-generated method stub
PageHelper.startPage(currentPage, pageSize);
List userList = (List) studentMapper.studentList(params);
PageInfo userPage = new PageInfo(userList);
return userPage;
}
//excel导入
@Override
public String readExcelFile(MultipartFile file) {
String result ="";
//创建处理EXCEL的类
ReadExcel readExcel=new ReadExcel();
//解析excel,获取上传的事件单
List useList = readExcel.getExcelInfo(file);
//至此已经将excel中的数据转换到list里面了,接下来就可以操作list,可以进行保存到数据库,或者其他操作,
//和你具体业务有关,这里不做具体的示范
studentMapper.batchInsert(useList);
if(useList != null && !useList.isEmpty()){
result = "上传成功";
}else{
result = "上传失败";
}
return result;
}
@Override
public PageInfo selectByAsc(int currentPage, int pageSize, Map params) {
// TODO Auto-generated method stub
PageHelper.startPage(currentPage, pageSize);
List userList = (List) studentMapper.selectByAsc(params);
PageInfo userPage = new PageInfo(userList);
return userPage;
}
下面是dao
public interface StudentMapper {
//查找所有学生
public List selectAllStudent();
//通过id刪除学生信息
public void deleteStudent(Integer studentId);
//分页查找
public List selectByList();
//新增学生
public void insertSelective(Student student);
//通过Id查找学生信息
public Student selectByPrimaryKey(Integer studentId);
//修改学生信息
public void updateByPrimaryKey(Student student);
//统计年纪人数
public List selectAllgrade();
//本框架的自带分页查询
List studentList(Map params);
//批量插入
public void batchInsert(List list);
//正序排序
public List selectByAsc(Map param);
}
下面是mapper
studentId, name, sex, classId,gradeId
delete from student
where studentId = #{studentId,jdbcType=INTEGER}
insert into student
(studentId, name, sex,
classId,gradeId)
values
(#{studentid,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR},
#{sex,jdbcType=VARCHAR},
#{classid,jdbcType=INTEGER},
#{gradeid,jdbcType=INTEGER})
insert into student
studentId,
name,
sex,
classId,
gradeId,
#{studentId,jdbcType=INTEGER},
#{name,jdbcType=VARCHAR},
#{sex,jdbcType=VARCHAR},
#{classId,jdbcType=INTEGER},
#{gradeId,jdbcType=INTEGER},
update student
name = #{name,jdbcType=VARCHAR},
sex = #{sex,jdbcType=VARCHAR},
classId = #{classId,jdbcType=INTEGER},
gradeId = #{gradeId,jdbcType=INTEGER},
where studentId = #{studentId,jdbcType=INTEGER}
update student
set
name = #{name,jdbcType=VARCHAR},
sex = #{sex,jdbcType=VARCHAR},
classId
= #{classId,jdbcType=INTEGER},
gradeId = #{gradeId,jdbcType=INTEGER}
where studentId = #{studentId,jdbcType=INTEGER}
insert into student
(name,sex,classId,gradeId) values
(
#{item.name},
#{item.sex},
#{item.classId},
#{item.gradeId}
)
还有两个工具类,excel的导入和导出
public class WriteExcelUtils {
//导出表的列名
private String[] rowName;
//每行作为一个Object对象
private List dataList = new ArrayList();
//构造方法,传入要导出的数据
public WriteExcelUtils(String[] rowName,List dataList){
this.dataList = dataList;
this.rowName = rowName;
}
/*
* 导出数据
* */
public InputStream export() throws Exception{
HSSFWorkbook workbook = new HSSFWorkbook(); // 创建工作簿对象
HSSFSheet sheet = workbook.createSheet("sheet1"); // 创建工作表
//sheet样式定义【getColumnTopStyle()/getStyle()均为自定义方法 - 在下面 - 可扩展】
HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);//获取列头样式对象
HSSFCellStyle style = this.getStyle(workbook); //单元格样式对象
// 定义所需列数
int columnNum = rowName.length;
HSSFRow rowRowName = sheet.createRow(0); // 在索引2的位置创建行(最顶端的行开始的第二行)
// 将列头设置到sheet的单元格中
for(int n=0;n
public class ReadExcel {
// 总行数
private int totalRows = 0;
// 总条数
private int totalCells = 0;
// 错误信息接收器
private String errorMsg;
// 构造方法
public ReadExcel() {
}
// 获取总行数
public int getTotalRows() {
return totalRows;
}
// 获取总列数
public int getTotalCells() {
return totalCells;
}
// 获取错误信息
public String getErrorInfo() {
return errorMsg;
}
/**
* 读EXCEL文件,获取信息集合
*
* @param fielName
* @return
*/
public List getExcelInfo(MultipartFile mFile) {
String fileName = mFile.getOriginalFilename();// 获取文件名
List userList=null;
try {
if (!validateExcel(fileName)) {// 验证文件名是否合格
return null;
}
boolean isExcel2003 = true;// 根据文件名判断文件是2003版本还是2007版本
if (isExcel2007(fileName)) {
isExcel2003 = false;
}
userList = createExcel(mFile.getInputStream(), isExcel2003);
} catch (Exception e) {
e.printStackTrace();
}
return userList;
}
/**
* 根据excel里面的内容读取客户信息
*
* @param is
* 输入流
* @param isExcel2003
* excel是2003还是2007版本
* @return
* @throws IOException
*/
public List createExcel(InputStream is, boolean isExcel2003) {
List userList=null;
try {
Workbook wb = null;
if (isExcel2003) {// 当excel是2003时,创建excel2003
wb = new HSSFWorkbook(is);
} else {// 当excel是2007时,创建excel2007
wb = new XSSFWorkbook(is);
}
userList = readExcelValue(wb);// 读取Excel里面客户的信息
} catch (IOException e) {
e.printStackTrace();
}
return userList;
}
/**
* 读取Excel里面客户的信息
*
* @param wb
* @return
*/
private List readExcelValue(Workbook wb) {
// 得到第一个shell
Sheet sheet = wb.getSheetAt(0);
// 得到Excel的行数
this.totalRows = sheet.getPhysicalNumberOfRows();
// 得到Excel的列数(前提是有行数)
if (totalRows > 1 && sheet.getRow(0) != null) {
this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
}
List userList = new ArrayList();
// 循环Excel行数
for (int r = 1; r < totalRows; r++) {
Row row = sheet.getRow(r);
if (row == null) {
continue;
}
Student user = new Student();
// 循环Excel的列
for (int c = 0; c < this.totalCells; c++) {
Cell cell = row.getCell(c);
if (null != cell) {
if (c == 0) {
// 如果是纯数字,比如你写的是25,cell.getNumericCellValue()获得是25.0,通过截取字符串去掉.0获得25
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String name = String.valueOf(cell.getNumericCellValue());
user.setName(name.substring(0, name.length() - 2 > 0 ? name.length() - 2 : 1));// 名字
} else {
user.setName(cell.getStringCellValue());// 名字
}
} else if (c == 1) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String sex = String.valueOf(cell.getNumericCellValue());
user.setSex(sex.substring(0, sex.length() - 2 > 0 ? sex.length() - 2 : 1));// 性别
} else {
user.setSex(cell.getStringCellValue());// 性别
}
} else if (c == 2) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String gradeId = String.valueOf(cell.getNumericCellValue());
user.setGradeId(Integer.parseInt((gradeId.substring(0, gradeId.length() - 2 > 0 ? gradeId.length() - 2 : 1))));// 年级
} else {
user.setGradeId(Integer.parseInt((cell.getStringCellValue())));// 年级
}
}else if (c == 3) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String classId = String.valueOf(cell.getNumericCellValue());
user.setClassId(Integer.parseInt((classId.substring(0, classId.length() - 2 > 0 ? classId.length() - 2 : 1))));// 班级
} else {
user.setClassId(Integer.parseInt(cell.getStringCellValue()));// 班级
}
}
}
}
// 添加到list
userList.add(user);
}
return userList;
}
/**
* 验证EXCEL文件
*
* @param filePath
* @return
*/
public boolean validateExcel(String filePath) {
if (filePath == null || !(isExcel2003(filePath) || isExcel2007(filePath))) {
errorMsg = "文件名不是excel格式";
return false;
}
return true;
}
// @描述:是否是2003的excel,返回true是2003
public static boolean isExcel2003(String filePath) {
return filePath.matches("^.+\\.(?i)(xls)$");
}
// @描述:是否是2007的excel,返回true是2007
public static boolean isExcel2007(String filePath) {
return filePath.matches("^.+\\.(?i)(xlsx)$");
}
}