赞
踩
宿舍管理系统实现的功能主要分三大类:系统用户的注册登录验证、对宿舍信息的增删改查、对学生信息的增删改查。
登录时,通过用户名和密码判断是否有此用户;注册时,不允许注册相同用户名的用户。为防止多次注册,设置邮箱验证码功能。基础抽象类如下:
public boolean login(String username, String password) throws Exception;//登录验证 public boolean register(UserBean userBean) throws SQLException ;//注册用户 public boolean addUser(UserBean userBean);//增加用户 |
宿舍功能模块的增删改查有许多限制条件,如增加宿舍时,宿舍楼号只能在01-10之间,当宿舍入住学生时不可删除,宿舍楼号作为唯一标识不可新修改,查询宿舍时可以多条件联合模糊查询。基础抽象类如下:
public boolean addDorm(Dormbean dormbean) throws SQLException ;//增加宿舍信息 public boolean delDorm(Dormbean dormbean) throws SQLException;//删除宿舍信息 public boolean updateDorm(Dormbean dormbean) throws SQLException ;//更新宿舍信息 public ArrayList<Dormbean> selectDorm(Dormbean dormbean) throws SQLException ;//查询宿舍信息 public ArrayList<Dormbean> showAllDorm() throws SQLException;//展示所有宿舍信息 public int dormTotalCount(Dormbean dormbean);//获取宿舍个数 public Dormbean selectDormById(int id) throws SQLException;//根据id查询宿舍 public boolean updateDormQuantity(Dormbean dormbean); //更新宿舍当前人数 |
学生信息的增删改查需要做到多表联动,增加学生信息并选择宿舍的时候需要和宿舍表进行联合操作,根据学生的性别及宿舍已入住人数判断学生信息是否可以添加成功;删除学生信息时,为防止误删,提供了事务回滚操作;因学生信息较多,提供了批量删除和分页功能操作;查询时可以多条件模糊查询;学生学号作为唯一标识,不可修改。抽象基础类如下:
public ArrayList<StudentBean> showStudentByPage(int status,int nowPage,int step) throws SQLException;//分页展示所有学生信息 public int StudentTotalCount(int status,StudentBean studentBean);//根据学生状态获得学生个数 public ArrayList<StudentBean> selectStudents(int status,int nowPage,int step,StudentBean studentBean) throws SQLException ;//根据条件查询学生信息并分页 public boolean addStudent(StudentBean studentBean) throws SQLException ;//增加学生信息 public boolean updateStudent(StudentBean studentBean);//更新学生信息 public boolean updateStatusStudent(int id,int status);//更新学生状态 public boolean delStudent(int id); //根据id删除学生 public boolean delAllStudent(String[] id); //根据id数组批量删除学生 public boolean updateAllStudent(String[] id);//根据id数组批量改变学生信息状态 |
项目开发工具为Spring Tool Suite ,版本为3.9.7.RELEASE;数据库为mysql数据库,版本为5.5.53;服务器为Tomcat服务器,版本为8.5.37。
2.1 数据库dormitory
数据库中共有三张表管理员信息表user,宿舍信息表dorm,学生信息表student。
用户信息表包含5个字段信息,对应着系统用户的登录注册验证模块,表结构如下所示。
用户信息表
字段名 | 字段类型 | 字段长度 | 意义 | 备注 |
id | int | 11 | 数据编号 | 主键 |
username | varchar | 10 | 用户名 | |
password | varchar | 15 | 密码 | |
| varchar | 20 | 邮箱 | |
realname | varchar | 10 | 真实姓名 |
2.3宿舍信息表
宿舍信息表包含七个字段,对应着宿舍信息增删改查的功能模块,表结构如下:
宿舍信息表
字段名 | 字段类型 | 字段长度 | 意义 | 备注 |
id | int | 5 | 数据编号 | 主键 |
house | varchar | 3 | 哪一栋 | |
build | varchar | 5 | 哪一座 | |
number | varchar | 10 | 门牌号 | |
attribute | varchar | 5 | 男女宿舍 | |
quantity | int | 2 | 当前人数 | |
status | int | 2 | 是否可用 | 可用为1;不可用为0 |
学生信息表包含12个字段,对应着学生信息增删改查的功能模块,表结构如下:
学生信息表
字段名 | 字段类型 | 字段长度 | 意义 | 备注 |
id | int | 11 | 数据编号 | 主键 |
sid | int | 11 | 学号 | |
dormID | int | 5 | 对应宿舍ID | |
sname | varchar | 10 | 学生姓名 | |
ssex | varchar | 5 | 学生性别 | |
sbirthday | date | 出生日期 | ||
smajor | varchar | 30 | 专业 | |
scollege | varchar | 20 | 学院 | |
sentranceT | int | 5 | 入学时间 | |
stelephone | varchar | 15 | 电话 | |
img | varchar | 20 | ||
status | int | 2 | 学生状态 | 默认为1,代表住宿中;2代表已离开 |
用户模块主要涉及用户登录和用户注册功能,为实现这两个功能,涉及了五个方法,从而对数据库进行操作,所有方法如下:
public boolean isUserExist(String username, String password) throws SQLException;//登录时根据用户名和密码判断用户是否存在 public boolean addUser(UserBean userBean);//增加用户 public boolean updateUser(UserBean userBean);//更新用户信息 public boolean selectByUserName(String username) throws SQLException;//注册用户时,先根据用户名查找此用户名是否已被注册 public UserBean selectUser(String username, String password);//根据用户名和密码,返回用户的详细信息 |
用户登录模块需要根据用户输入的密码和用户名,对数据库中存储的数据进行查询,如果查询到对应数据,则允许登录,反之,则不允许登录。
用户注册时因为用户名为唯一标识,所以不允许注册有相同用户名的账号,为了防止重复注册,用户在注册时,需要填写自己的邮箱地址,系统会向用户邮箱发送验证码,验证码正确时才允许注册。
宿舍模块包括对用户的增删改查操作,为实现这些操作,涉及了以下的方法:
public boolean addDorm(Dormbean dormbean) ;//增加宿舍 public boolean delDorm(Dormbean dormbean) throws SQLException;//删除宿舍 public boolean updateDorm(Dormbean dormbean) ;//更新宿舍 public ArrayList<Dormbean> selectDorm(Dormbean dormbean) throws SQLException ;//查询复合条件的宿舍 public ArrayList<Dormbean> showAllDorm() throws SQLException;//展示所有宿舍 public boolean isExitDorm(Dormbean dormbean) throws SQLException;//判断宿舍是否存在 public Dormbean selectDormById(int id) throws SQLException ;//根据宿舍编号查询宿舍并返回宿舍信息 public int dormTotalCount(Dormbean dormbean) ;//返回宿舍总数 public boolean updateDormQuantity(int id,int quantity) ;//根据宿舍id更新宿舍学生数,插入学生时使用 public Dormbean selectDormByDormbean(Dormbean dormbean) throws SQLException; //根据条件返回宿舍 |
增加宿舍信息时,首先在前台已经对宿舍信息进行了验证,防止不合法的输入。在确保验证和法的情况下,查询将要创建的宿舍是否已经存在,如已经存在,则提示用户此宿舍已存在,创建失败;如宿舍不存在,则创建成功。
删除宿舍信息时,会先判断当前宿舍的人数,若当前宿舍人数为0,则可以删除宿舍,若当前宿舍仍有学生入住,则不可删除。
宿舍信息的楼号,栋号,门牌号作为宿舍的唯一标识不可修改,可修改宿舍的使用状态和可入住学生的性别。因此,在修改宿舍信息之前会判断宿舍当前人数,若宿舍人数为0,则可以修改;若不为0,则不允许修改。
宿舍信息查询支持对宿舍座号、栋号、门牌号、入住学生性别、当前入住人数的多条件联合查询。
学生模块包括对学生信息的增删改查,批量操作,信息分页的功能,涉及了以下的方法:
public ArrayList<StudentBean> showStudentByPage(int status,int nowPage,int step) throws SQLException ;//学生信息分页 public int studentTotalCount(int status,StudentBean studentBean) ;//返回学生总数 public ArrayList<StudentBean> selectStudents(int status,int nowPage, int step,StudentBean studentBean) throws SQLException;//根据查询条件实现学生信息分页 public boolean addStudent(StudentBean studentBean);//增加学生信息 public boolean isExistStudent(int sid) throws SQLException ;//根据学生学号判断学生是否存在 public StudentBean selectStudentById(int id) throws SQLException;//根据学生主键查询相对应学生信息 public boolean updateStudent(StudentBean studentBean);//更新学生信息 public boolean updateStatusStudent(int id,int status);//根据学生主键更新学生状态 public boolean delStudent(int id); //根据主键删除单个学生 public boolean delAllStudent(String[] id); //根据主键数组批量删除学生 public boolean updateAllStudent(String[] id); //根据主键恢复被删除的学生 |
增加学生信息时首先在前端进行表单验证,防止不合法的输入。在增加学生信息时,需要为其入住相应的宿舍,入住宿舍时需要进行判断,当宿舍存在时,判断学生性别以及当前宿舍人数,来判断学生是否能够入住;当宿舍不存在时,则为学生创建相对应的宿舍进行入住,入住成功后,对应的宿舍信息表中的宿舍人数数据进行加一。
为防止用户误删学生信息,系统提供了事务回滚机制,切因学生信息较多,提供了批量删除机制。当学生信息真正从数据库中删除的时候,对应的学生宿舍的宿舍人数会减一。
学生学号作为学生信息的唯一标识不可修改,可以对学生的姓名、学院、专业、出生日期、入学年份、联系电话进行修改。
因为学生信息较多,所以对学生数据进行了分页,支持对学生学号、姓名、性别、专业、学院入学年份的多条件模糊查询。
request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String code = request.getParameter("code"); HttpSession session = request.getSession(); if (session.getAttribute("code")!=null) { if ((code.equals(session.getAttribute("code")))) { session.removeAttribute("code"); UserBean userBean = new UserBean(); userBean.setUsername(request.getParameter("username")); userBean.setPassword(request.getParameter("password")); userBean.setEmail(request.getParameter("email")); UserServiceImpl userServiceImpl = new UserServiceImpl(); try { if (userServiceImpl.register(userBean)) { response.getWriter().print("注册成功"); } else { response.getWriter().print("此用户名已存在,请更换用户名"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { session.removeAttribute("code"); response.getWriter().print("验证码错误"); } } else { response.getWriter().print("还未获取验证码"); } |
package com.gzf.util; import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.Properties; import javax.mail.Address; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage.RecipientType; public class EmailUtil { public static void createMimeMessage(String email,String content) throws UnsupportedEncodingException, MessagingException { Properties properties = new Properties(); // 使用的协议 properties.setProperty("mail.transport.protocol", "smtp"); //QQ邮箱协议地址 properties.setProperty("mail.smtp.host", "smtp.qq.com"); //端口号 properties.setProperty("mail.smtp.port", "465"); //是否需要授权 properties.setProperty("mail.smtp.auth", "true"); //qq:ssl安全认证 //引用需要的类 properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); //是否处理未经SSL认证的信息 properties.setProperty("mail.smtp.socketFactory.fallback", "true"); //SSL认证的端口号 properties.setProperty("mail.smtp.socketFactory.port", "465"); Session session = Session.getInstance(properties); // MimeMessage定义了大多数消息公共属性的抽象获取和设置方法 MimeMessage mimeMessage = new MimeMessage(session); // 设置发件人地址信息 Address address = new InternetAddress("1226164732@qq.com","gzf","utf-8"); mimeMessage.setFrom(address); mimeMessage.setSubject("来自谷占方的邮件","utf-8"); content = "您正在注册宿舍管理系统管理员,验证码为"+content; mimeMessage.setContent(content, "text/html;charset=utf-8"); //设置传送日期 mimeMessage.setSentDate(new Date()); //设置接收人, .to为正常发送 .cc为抄送 .bcc为密送 mimeMessage.setRecipient(RecipientType.TO,new InternetAddress(email, "小可爱", "utf-8")); //保存邮件 mimeMessage.saveChanges(); //实例化一个对象,单例模式 session.setDebug(true); //创建邮箱 //建立连接对象 Transport transport = session.getTransport(); //此处QQ必须填密钥而不是密码 transport.connect("1226164732@qq.com", "xaetrwzpaknkgeeb"); //进行发送 transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients()); //关闭连接 transport.close(); } } |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); int id = Integer.parseInt(request.getParameter("id")); int status = Integer.parseInt(request.getParameter("status")); String house=null; String attribute = null; if (request.getParameter("house")!=null) {//说明是通过表单提交更改性别 house = request.getParameter("house"); attribute = request.getParameter("attribute"); } Dormbean dormbean = new Dormbean(); dormbean.setId(id); dormbean.setStatus(status); dormbean.setHouse(house); dormbean.setAttribute(attribute); IDormService iDormService = new DormServiceImpl(); try { response.getWriter().println(iDormService.updateDorm(dormbean)); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } |
学生列表实现了分页操作,方便用户浏览信息,并能够在页面中提示当前学生总数,以及页数,在进行搜索之后仍能对学生信息进行分页。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); HttpSession session = request.getSession(); StudentBean studentBean = new StudentBean(); int nowPage; int step = 5; if (request.getParameter("nowPage")==null) {//为null说明是通过按钮提交 nowPage = 1; int sid = 0; if (request.getParameter("sid")!="") { sid = Integer.parseInt(request.getParameter("sid")); } String sname = request.getParameter("sname"); String ssex = request.getParameter("ssex"); String smajor = request.getParameter("smajor"); String scollege = request.getParameter("scollege"); int sentranceT = 0; if (request.getParameter("sentranceT")!="") { sentranceT = Integer.parseInt(request.getParameter("sentranceT")); } studentBean.setSid(sid); studentBean.setSname(sname); studentBean.setSsex(ssex); studentBean.setSmajor(smajor); studentBean.setScollege(scollege); studentBean.setSentranceT(sentranceT); session.setAttribute("studentBean", studentBean); } else { //不为空说明为通过页码提交搜索 nowPage = Integer.parseInt(request.getParameter("nowPage")); studentBean = (StudentBean) session.getAttribute("studentBean"); } int status = 1 ; System.out.println("servlet中的studentbean为"+studentBean); IStudentService iStudentService = new StudentServiceImpl(); int totalCount = iStudentService.StudentTotalCount(status,studentBean); ArrayList<StudentBean> studentBeans = new ArrayList<>(); try { studentBeans = iStudentService.selectStudents(status,nowPage, step, studentBean); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } Iterator<StudentBean> iterator = studentBeans.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } PageUtil<StudentBean> pageUtil = new PageUtil<>(); pageUtil.setList(studentBeans); pageUtil.setTotalCount(totalCount); pageUtil.setStep(step); pageUtil.setNowPage(nowPage); request.setAttribute("pageUtil", pageUtil); request.getRequestDispatcher("student-select.jsp").forward(request, response); } |
package com.gzf.util; import java.util.ArrayList; public class PageUtil<T> { private int totalCount; private int step; private int totalPage; private int nowPage; private ArrayList<T> list; public PageUtil(int totalCount, int step, int totalPage, int nowPage, ArrayList<T> list) { super(); this.totalCount = totalCount; this.step = step; this.totalPage = totalPage; this.nowPage = nowPage; this.list = list; } public PageUtil() { super(); // TODO Auto-generated constructor stub } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public int getStep() { return step; } // 在这里定义了总页数,因此需要先定义数据总数,再定义每页显示数量 public void setStep(int step) { this.step = step; if(this.totalCount % this.step ==0) { this.totalPage = this.totalCount / this.step; } else { this.totalPage = this.totalCount / this.step + 1; } } public int getTotalPage() { return totalPage; } public int getNowPage() { return nowPage; } public void setNowPage(int nowPage) { this.nowPage = nowPage; } public ArrayList<T> getList() { return list; } public void setList(ArrayList<T> list) { this.list = list; } } |
支持对学生信息到的多条件模糊查询。
public ArrayList<StudentBean> selectStudents(int status,int nowPage, int step,StudentBean studentBean) throws SQLException { // TODO Auto-generated method stub String sql = "SELECT * FROM student where status = ?"; if (studentBean.getSid()==0) { sql = sql + " and sid != ?"; } else { sql = sql + " and sid like ?"; } if (studentBean.getSname()=="") { sql = sql + " and sname != ?"; } else { sql = sql + " and sname like ?"; } if (studentBean.getSsex()=="") { sql = sql + " and ssex != ?"; } else { sql = sql + " and ssex = ?"; } if (studentBean.getSmajor()=="") { sql = sql + " and smajor != ?"; } else { sql = sql + " and smajor like ?"; } if (studentBean.getScollege()=="") { sql = sql + " and scollege != ?"; } else { sql = sql + " and scollege like ?"; } if (studentBean.getSentranceT()==0) { sql = sql + " and sentranceT != ?"; } else { sql = sql + " and sentranceT like ?"; } sql = sql + " LIMIT ?,?"; System.out.println("*******"+sql+"------"+status); Object [] params = {status,"%"+studentBean.getSid()+"%","%"+studentBean.getSname()+"%",studentBean.getSsex(), "%"+studentBean.getSmajor()+"%","%"+studentBean.getScollege()+"%","%"+studentBean.getSentranceT()+"%", nowPage*step-step,step}; ResultSet resultSet = DButil.select(sql, params); ArrayList<StudentBean> studentBeans = new ArrayList<>(); while(resultSet.next()) { StudentBean studentBean2 = new StudentBean(); studentBean2.setId(resultSet.getInt("id")); studentBean2.setSid(resultSet.getInt("sid")); studentBean2.setDormID(resultSet.getInt("dormID")); studentBean2.setSname(resultSet.getString("sname")); studentBean2.setSsex(resultSet.getString("ssex")); studentBean2.setSbirthday(resultSet.getString("sbirthday")); studentBean2.setSmajor(resultSet.getString("smajor")); studentBean2.setScollege(resultSet.getString("scollege")); studentBean2.setSentranceT(resultSet.getInt("sentranceT")); studentBean2.setStelephone(resultSet.getString("stelephone")); studentBean2.setImg(resultSet.getString("img")); studentBean2.setStatus(resultSet.getInt("status")); studentBeans.add(studentBean2); } return studentBeans; } |
在增加学生信息时,需要为其入住相应的宿舍,入住宿舍时需要进行判断,当宿舍存在时,判断学生性别以及当前宿舍人数,来判断学生是否能够入住;当宿舍不存在时,则为学生创建相对应的宿舍进行入住,入住成功后,对应的宿舍信息表中的宿舍人数数据进行加一。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); //获取学生信息,attribute为性别,学号表和宿舍表共用 int sid = Integer.parseInt(request.getParameter("sid")); int sentranceT = Integer.parseInt(request.getParameter("sentranceT")); String sname = request.getParameter("sname"); String sbirthday = request.getParameter("sbirthday"); String smajor = request.getParameter("smajor"); String scollege = request.getParameter("scollege"); String stelephone = request.getParameter("stelephone");
String house = request.getParameter("house"); String build = request.getParameter("build"); String number = request.getParameter("number"); String attribute = request.getParameter("attribute"); int status = Integer.parseInt(request.getParameter("status")); // 查询验证输入的宿舍是否可以入住,验证包括宿舍是否存在,性别,以及入住状态 Dormbean dormbean = new Dormbean(); dormbean.setHouse(house); dormbean.setBuild(build); dormbean.setNumber(number); dormbean.setAttribute(attribute); dormbean.setStatus(status); IDormDao iDormDao = new DormDAoImpl(); //检测学生是否存在 IStudentDao iStudentDao = new StudentDaoImpl(); try { if (iStudentDao.isExistStudent(sid)) { response.getWriter().print("此学生学号已存在,无法添加"); } else { try { if (iDormDao.isExitDorm(dormbean)) {//当宿舍存在时 System.out.println("此宿舍存在"); Dormbean dormbean2 = iDormDao.selectDormByDormbean(dormbean); System.out.println(dormbean2); if (dormbean2.getQuantity()==6) { System.out.println("当前宿舍人数已满,请为此学生更换宿舍"); response.getWriter().print("当前宿舍人数已满,请为此学生更换宿舍"); } else { if (!(dormbean2.getAttribute().equals(attribute))) { System.out.println("****----"+dormbean2.getAttribute()); System.out.println(attribute); System.out.println("当前宿舍不能入住此性别学生"); response.getWriter().print("当前宿舍不能入住此性别学生"); } else { if (dormbean2.getStatus()==0) { System.out.println("当前宿舍未启用,请更换或先启用此宿舍"); response.getWriter().print("当前宿舍未启用,请更换或先启用此宿舍"); } else { StudentBean studentBean = new StudentBean(); studentBean.setSid(sid); studentBean.setDormID(dormbean2.getId()); studentBean.setSname(sname); studentBean.setSsex(attribute); studentBean.setSbirthday(sbirthday); studentBean.setSmajor(smajor); studentBean.setScollege(scollege); studentBean.setSentranceT(sentranceT); studentBean.setStelephone(stelephone); IStudentService iStudentService = new StudentServiceImpl(); iStudentService.addStudent(studentBean); System.out.print("已为您创建此学生信息,并入住宿舍"); response.getWriter().print("已为您创建此学生信息,并入住宿舍"); } } } } else {//当宿舍不存在的时候,先创建宿舍,再入住学生 iDormDao.addDorm(dormbean); System.out.println(dormbean); Dormbean dormbean2 = iDormDao.selectDormByDormbean(dormbean); System.out.println(dormbean2); StudentBean studentBean = new StudentBean(); studentBean.setSid(sid); studentBean.setDormID(dormbean2.getId()); studentBean.setSname(sname); studentBean.setSsex(attribute); studentBean.setSbirthday(sbirthday); studentBean.setSmajor(smajor); studentBean.setScollege(scollege); studentBean.setSentranceT(sentranceT); studentBean.setStelephone(stelephone); IStudentService iStudentService = new StudentServiceImpl(); iStudentService.addStudent(studentBean); System.out.println("已为您创建此宿舍,并入住此学生"); response.getWriter().print("已为您创建此宿舍,并入住此学生"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } |
因为学生信息比较多,当大四同学离校时逐条删除信息比较麻烦,所以提供了批量信息删除功能。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); System.out.println(request.getParameter("data")); String data = request.getParameter("data"); String[] id = data.split(","); IStudentService iStudentService = new StudentServiceImpl(); response.getWriter().println(iStudentService.delAllStudent(id)); } |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。