作为刚毕业的java初级开发师入职第一个新人考核项目

入职第一天,公司把框架给了我,让我做一个学生管理信息,功能都有学生信息的增删改查,分页,柱状图统计,excel的导入导出。

历经一个星期,项目终于搞完。也许对其他中高级开发来说,别说一个星期,半天就能弄完。。。刚入行,慢慢来。

下面对自己最近一个星期的代码做一个总结,以便以后不断改善。

@RequestMapping("/student")
@Controller
public class StudentController {
	@Autowired
	private IStudentService s1;
	
	// 刪除功能
	@RequestMapping("/deleteStudent")
	public String deleteStudent(Integer studentId) {
		s1.deleteStudent(studentId);
		return "redirect:/page/datatables_demo/datatables.html";
	}


	// 新增功能
	@RequestMapping("/addStudent")
	public String addStudent(Student student) {
		s1.insertSelective(student);
		return "redirect:/page/datatables_demo/datatables.html";
	}


	// 修改功能
	@RequestMapping("/updateStudent")
	public String updateStudent(Student student) {
		s1.updateByPrimaryKey(student);
		return "redirect:/page/datatables_demo/datatables.html";
	}
	
	//框架自带的分页方法+自写排序
	@RequestMapping(value = "/userList", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel getUserList(
			@RequestParam("currentPage") int currentPage,
			@RequestParam("pageSize") int pageSize, @RequestParam(value="column",required=false) Integer column,
			@RequestParam(value="dir",required=false) String dir)
			throws Exception {
		Map map = new HashMap();
		PageInfo page = null;
		//判断是否排序
		if (column!= null) {
			if (column == 0) {
				if (dir.equals("asc")) {
					map.put("columns", "studentId");
					map.put("dir", "asc");
					page = s1.studentList(currentPage, pageSize,
							map);
				}else{
					map.put("columns", "studentId");
					map.put("dir", "desc");
					page = s1.studentList(currentPage, pageSize,
							map);
				}
			}
			if (column == 1) {
				if (dir.equals("asc")) {
					map.put("columns", "name");
					map.put("dir", "asc");
					page = s1.studentList(currentPage, pageSize,
							map);
				}else{
					map.put("columns", "name");
					map.put("dir", "desc");
					page = s1.studentList(currentPage, pageSize,
							map);
				}
			}
			if (column == 2) {
				if (dir.equals("asc")) {
					map.put("columns", "sex");
					map.put("dir", "asc");
					page = s1.studentList(currentPage, pageSize,
							map);
				}else{
					map.put("columns", "sex");
					map.put("dir", "desc");
					page = s1.studentList(currentPage, pageSize,
							map);
				}
			}
			if (column == 3) {
				if (dir.equals("asc")) {
					String cloumns="className";
					map.put("columns", cloumns);
					map.put("dir",dir);
					page = s1.studentList(currentPage, pageSize,
							map);
				}else{
					map.put("columns", "className");
					map.put("dir", dir);
					page = s1.studentList(currentPage, pageSize,
							map);
				}
				
			}
			if (column == 4) {
				if (dir.equals("asc")) {
					map.put("columns", "gradeName");
					map.put("dir", "asc");
					page = s1.studentList(currentPage, pageSize,
							map);
				}else{
					map.put("columns", "gradeName");
					map.put("dir", "desc");
					page = s1.studentList(currentPage, pageSize,
							map);
				}
			}	
		}else {
			page = s1.studentList(currentPage, pageSize,
					map);
		}
		return new ResultModel(true, "", "", page);
	}


	//统计
	@RequestMapping(value = "/statistic", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel statistic()
			throws Exception {
		List gradeNum=s1.selectAllgrade();
		return new ResultModel(true, "", "", gradeNum);
	}
	
	
	//excel 的导入数据库
	@RequestMapping(value="/upload",method = RequestMethod.POST)  
    @ResponseBody  
    public String  upload(@RequestParam(value="file",required = false)MultipartFile file,HttpServletRequest request, HttpServletResponse response){  
        String result = s1.readExcelFile(file);  
        return result;  
    }
	
	//导出
	@RequestMapping("/export")
	public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		System.out.println("进入导出方法...");
		String[] title = {"StudentId","name","sex","className","gradeName"};
        List list = s1.selectAllStudent();
        List  dataList = new ArrayList();     
        for(int i=0;i@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)$");
	}
}

前台页面


    学生信息统计
    
    
    
    
    


学生信息统计

学生学号 姓名 性别 班级 年级 操作
总体代码就是这样,晚上回去做总结



你可能感兴趣的:(作为刚毕业的java初级开发师入职第一个新人考核项目)