年级表效果图样例:
可以看到主要分为以下这些功能:
功能一:展示年级列表
功能二:每个年级都具备修改和删除
功能三:添加一个年级
功能四:对多个年级选中删除,也可以全选删除,或者反选删除
功能五:根据提供信息查询年级名称得到整个年级信息
功能六:对列表进行分页展示【年级表的信息一般不会超过10个以上...,可选】
1、展示年级列表:
- 1、我们需要一个页面模版,然后根据我们的字段信息进行调整
- 2、这个页面是通过Servlet转发访问的,所以必须通过对于的Servlet的方法访问(例如这是年级表,就用GradeServlet的index方法访问)
- 3、然后在Servlet的访问方法中携带我们查询的年级列表结果
- 4、在页面中使用JSTL与EL将请求携带的年级列表数据进行渲染处理
- 5、对其他东西,例如修改和删除,还有全选的这个复选框也相应的做模版调整,就基本完成
2、添加一个年级:
- 1、添加年级是前往一个新的页面,和这个年纪列表不存在任何关联的信息【第一个Servlet方法负责跳转到添加的页面】
- 2、提供对应的年级信息获取,输入框,然后把信息使用表单或者Ajax发送到服务中处理【第二个Servlet方法负责处理接收的请求数据,然后反馈Ajax或者跳转年级列表页】
- 3、返回Json结果,对于如何跳转到年级列表的处理方案很多,不赘述了
- 4、是否需要对添加部分进信息检查? 发现可以使用正则表达式处理
3、修改一个年级:
- 1、和上面添加年级差不多,但是问题是修改的年级需要把数据带回到修改页面中,
- 2、然后再是对应的修改
- 3、由Servlet1跳转到修改页面并渲染对应的ID记录数据,然后提交给Servlet2处理,然后返回
4、删除一个年级:
- 1、通过ID发送到Servlet调用业务层对象处理,然后页面刷新即可
- 2、主要的问题是在于软硬删除的考量,或者是二次删除,以保证数据安全
- 3、基于二次删除的设计,我们还需要一个标记为删除的记录列表,在这里查看,然后添加真正删除的操作
- 4、或者设置多级标记状态,不允许出现真正的记录删除
5、模糊查询年级:
和展示列表的功能是一样的,都是为了展示出来,所以就需要加入模糊查询的条件来执行
根据请求对象中的关键字参数是否提供,来实现动态的功能执行:
DAO层设计:
接口
package cn.dzz.dao; import cn.dzz.entity.menu.Grade; import java.util.List; /** * @author DaiZhiZhou * @file OA-Project-Phase1 * @create 2020-07-02 13:53 */ public interface GradeDao { /** * 年级表单表简单数据稳定不需要考虑分页的操作 * 主要的问题是需要按照正常的列表显示和标记删除的列表显示,其次是两者都带有模糊查询 * @param is_del * @param keyword * @return */ List<Grade> queryGradeList(Integer is_del,String keyword); /** * 对列表展示的记录的ID来查询单个的记录实在是操作频繁, * 这一个dao方法是绝对要用的 * @param grade_id * @return */ Grade queryGradeById(Integer grade_id); /** * 插入记录只需要关键的字段提供即可,主键维护设置自增即可 * @param grade * @return */ int insertGrade(Grade grade); /** * 根据ID修改记录,用于is_del删除和正常字段修改,搭配按ID查询使用 * @param grade * @return */ int updateGradeById(Grade grade); /** * 真正的对数据移除操作,一般来说不会开发这个功能出来 * @param grade_id * @return */ int deleteGradeById(Integer grade_id); }
实现:
package cn.dzz.dao; import cn.dzz.entity.menu.Grade; import cn.dzz.util.CommonUtil; import cn.dzz.util.DruidUtil; import lombok.SneakyThrows; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import java.util.List; /** * @author DaiZhiZhou * @file OA-Project-Phase1 * @create 2020-07-02 13:53 */ public class GradeDaoImpl implements GradeDao{ private QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource()); private final String QUERY_ALL_GRADE = "SELECT * FROM `t_grade` WHERE 1 = 1 "; private final String INSERT = "INSERT INTO `t_grade` VALUES(DEFAULT, ?, ?, DEFAULT)"; private final String UPDATE = "UPDATE `t_grade` SET `grade_name` = ?, `grade_order_id` = ?, `grade_is_del` = ? WHERE 1 = 1 "; private final String DELETE = "DELETE FROM `t_grade` WHERE 1 = 1 "; private final String CONDITION_GRADE_ID = "AND `grade_id` = ? "; private final String CONDITION_IS_DEL = "AND `grade_is_del` = ? "; private final String CONDITION_GRADE_NAME_LIKE = "AND `grade_name` LIKE CONCAT('%',?,'%') "; @SneakyThrows @Override public List<Grade> queryGradeList(Integer is_del,String keyword) { final String SQL = QUERY_ALL_GRADE + CONDITION_IS_DEL; if (CommonUtil.notNullAndEmpty(keyword)) return queryRunner.query(SQL + CONDITION_GRADE_NAME_LIKE, new BeanListHandler<Grade>(Grade.class), is_del, keyword); return queryRunner.query(SQL, new BeanListHandler<Grade>(Grade.class), is_del); } @SneakyThrows @Override public Grade queryGradeById(Integer grade_id) { return queryRunner.query(QUERY_ALL_GRADE + CONDITION_GRADE_ID, new BeanHandler<Grade>(Grade.class), grade_id); } @SneakyThrows @Override public int insertGrade(Grade grade) { return queryRunner.update(INSERT, grade.getGrade_name(), grade.getGrade_order_id()); } @SneakyThrows @Override public int updateGradeById(Grade grade) { return queryRunner.update( UPDATE + CONDITION_GRADE_ID, grade.getGrade_name(), grade.getGrade_order_id(), grade.getGrade_is_del(), grade.getGrade_id() ); } @SneakyThrows @Override public int deleteGradeById(Integer grade_id) { return queryRunner.update(DELETE + CONDITION_GRADE_ID, grade_id); } }
业务层设计:
接口:
package cn.dzz.service.menu; import cn.dzz.entity.menu.Grade; import cn.dzz.util.common.JsonResult; import javax.servlet.http.HttpServletRequest; import java.util.List; /** * @author DaiZhiZhou * @file OA-Project-Phase1 * @create 2020-07-02 14:45 */ public interface GradeService { /** * 根据提供的参数条件进行查询年级记录 * 主要是因为2个页面的2搜索 * @param keyword * @param is_del * @return */ List<Grade> getGradeList(String keyword,Integer is_del); /** * 跟dao层一样,无业务逻辑,仅按照架构规范传递数据用 * @param request * @return */ Grade getGradeById(HttpServletRequest request); /** * 根据提供的ID修改年级记录 * 结果返回的是JSON给Ajax前端消息 * @param request * @return */ JsonResult altGradeById(HttpServletRequest request); /** * 是使用is_del来进行的“删除”,调用的dao就是修改而已 * @param request * @return */ JsonResult delGradeById(HttpServletRequest request); /** * 需要批量操作“删除”,遍历调用即可 * @param request * @return */ JsonResult delGradeByIdArray(HttpServletRequest request); /** * 添加年级记录,只需要提供的关键字段数据,执行即可 * @param request * @return */ JsonResult addGrade(HttpServletRequest request); /** * 还原is_del记录,dao层调用的修改 * @param request * @return */ JsonResult undoGradeById(HttpServletRequest request); /** * 批量还原is_del记录,遍历dao层调用的修改 * @param request * @return */ JsonResult undoGradeByIdArray(HttpServletRequest request); /** * 真DELETE删除,原理不赘述了 * @param request * @return */ JsonResult removeGradeById(HttpServletRequest request); /** * 同理,不赘述 * @param request * @return */ JsonResult removeGradeByIdArray(HttpServletRequest request); }
实现:
package cn.dzz.service.menu; import cn.dzz.dao.GradeDao; import cn.dzz.dao.GradeDaoImpl; import cn.dzz.entity.menu.Grade; import cn.dzz.util.common.JsonResult; import javax.servlet.http.HttpServletRequest; import java.util.List; /** * @author DaiZhiZhou * @file OA-Project-Phase1 * @create 2020-07-02 14:46 */ public class GradeServiceImpl implements GradeService { GradeDao gradeDao = new GradeDaoImpl(); @Override public List<Grade> getGradeList(String keyword,Integer is_del) { return gradeDao.queryGradeList(is_del,keyword); } @Override public Grade getGradeById(HttpServletRequest request) { return gradeDao.queryGradeById(Integer.valueOf(request.getParameter("grade_id"))); } @Override public JsonResult altGradeById(HttpServletRequest request) { int i = gradeDao.updateGradeById( new Grade( Integer.valueOf(request.getParameter("grade_id")), request.getParameter("grade_name"), Integer.valueOf(request.getParameter("grade_order_id")), 0 ) ); if (i == 0) return new JsonResult(200,"修改失败"); return new JsonResult(100,"修改成功"); } @Override public JsonResult delGradeById(HttpServletRequest request) { Integer grade_id = Integer.valueOf(request.getParameter("grade_id")); Grade grade = gradeDao.queryGradeById(grade_id); grade.setGrade_is_del(1); int i = gradeDao.updateGradeById(grade); if (i == 0) return new JsonResult(200,"标记删除失败"); return new JsonResult(100,"标记删除成功"); } @Override public JsonResult delGradeByIdArray(HttpServletRequest request) { String grade_ids = request.getParameter("grade_ids"); String[] split = grade_ids.split(","); // Grade grade = new Grade(null,null,null,1); int affectedCount = 0; for (String s : split) { Integer grade_id = Integer.valueOf(s); Grade grade = gradeDao.queryGradeById(grade_id); grade.setGrade_is_del(1); // grade.setGrade_id(Integer.valueOf(s)); int affected = gradeDao.updateGradeById(grade); affectedCount += affected; } if (affectedCount != split.length) return new JsonResult(200,"多记录标记删除失败"); return new JsonResult(100,"多记录标记删除成功"); } @Override public JsonResult addGrade(HttpServletRequest request) { int i = gradeDao.insertGrade( new Grade( null, request.getParameter("grade_name"), Integer.valueOf(request.getParameter("grade_order_id")), null ) ); if (i == 0) return new JsonResult(200,"添加失败"); return new JsonResult(100,"添加成功"); } @Override public JsonResult removeGradeById(HttpServletRequest request) { int i = gradeDao.deleteGradeById(Integer.valueOf(request.getParameter("grade_id"))); if (i == 0) return new JsonResult(200,"彻底删除失败"); return new JsonResult(100,"彻底删除成功"); } @Override public JsonResult removeGradeByIdArray(HttpServletRequest request) { String remove_grade_ids = request.getParameter("remove_grade_ids"); String[] split = remove_grade_ids.split(","); int affectedCount = 0; for (String s : split) { int affected = gradeDao.deleteGradeById(Integer.valueOf(s)); affectedCount += affected; } if (affectedCount != split.length) return new JsonResult(200,"多记录彻底删除失败"); return new JsonResult(100,"多记录彻底删除成功"); } @Override public JsonResult undoGradeById(HttpServletRequest request) { Grade gradeById = gradeDao.queryGradeById(Integer.valueOf(request.getParameter("grade_id"))); gradeById.setGrade_is_del(0); int i = gradeDao.updateGradeById(gradeById); if (i == 0) return new JsonResult(200,"还原失败"); return new JsonResult(100,"还原成功"); } @Override public JsonResult undoGradeByIdArray(HttpServletRequest request) { String recover_grade_ids = request.getParameter("recover_grade_ids"); String[] split = recover_grade_ids.split(","); int affectedCount = 0; for (String s : split) { Integer grade_id = Integer.valueOf(s); Grade grade = gradeDao.queryGradeById(grade_id); grade.setGrade_is_del(0); int affected = gradeDao.updateGradeById(grade); affectedCount += affected; } if (affectedCount != split.length) return new JsonResult(200,"多记录还原失败"); return new JsonResult(100,"多记录还原成功"); } }
Servlet分发处理:
package cn.dzz.servlet.menu; import cn.dzz.service.menu.GradeService; import cn.dzz.service.menu.GradeServiceImpl; import cn.dzz.util.common.BaseServlet; import cn.dzz.util.common.JsonResult; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @author DaiZhiZhou * @file OA-Project-Phase1 * @create 2020-07-02 13:33 */ @WebServlet("/home/grade") public class GradeServlet extends BaseServlet { GradeService gradeService = new GradeServiceImpl(); /** * 年级列表首页 /home/grade * @param request * @param response * @return */ public String index(HttpServletRequest request, HttpServletResponse response) { request.setAttribute("gradeList",gradeService.getGradeList(request.getParameter("search-in-grade-list"),0)); return "/WEB-INF/jsp/home/grade/grade-list.jsp"; } /** * 标记删除的年级列表页 * /home/grade?act=deletedGradeListPage * @param request * @param response * @return */ public String deletedGradeListPage(HttpServletRequest request, HttpServletResponse response) { request.setAttribute("deletedGradeList",gradeService.getGradeList(request.getParameter("search-in-del-grade-list"),1)); return "/WEB-INF/jsp/home/grade/grade-del.jsp"; } /** * 年级添加的页面 * /home/grade?act=addGradePage * @param request * @param response * @return */ public String addGradePage(HttpServletRequest request, HttpServletResponse response) { return "/WEB-INF/jsp/home/grade/grade-add.jsp"; } /** * 年级添加的处理 * /home/grade?act=toAddGrade * @param request * @param response * @return */ public JsonResult toAddGrade(HttpServletRequest request, HttpServletResponse response) { return gradeService.addGrade(request); } /** * 年级标记删除的处理 * /home/grade?act=toDelGrade * @param request * @param response * @return */ public JsonResult toDelGrade(HttpServletRequest request, HttpServletResponse response) { return gradeService.delGradeById(request); } /** * 年级修改的页面 * /home/grade?act=altGradePage * @param request * @param response * @return */ public String altGradePage(HttpServletRequest request, HttpServletResponse response) { request.setAttribute("grade",gradeService.getGradeById(request)); return "/WEB-INF/jsp/home/grade/grade-alt.jsp"; } /** * 年级修改的处理 * /home/grade?act=toAltGrade * @param request * @param response * @return */ public JsonResult toAltGrade(HttpServletRequest request, HttpServletResponse response) { return gradeService.altGradeById(request); } /** * 多记录年级删除的处理 * /home/grade?act=toDelGrades * @param request * @param response * @return */ public JsonResult toDelGrades(HttpServletRequest request, HttpServletResponse response) { return gradeService.delGradeByIdArray(request); } /** * 单个年级记录还原 * /home/grade?act=toRecoverGrade * @param request * @param response * @return */ public JsonResult toRecoverGrade(HttpServletRequest request, HttpServletResponse response) { return gradeService.undoGradeById(request); } /** * 单个记录彻底删除 * /home/grade?act=toRemoveGrade * @param request * @param response * @return */ public JsonResult toRemoveGrade(HttpServletRequest request, HttpServletResponse response) { return gradeService.removeGradeById(request); } /** * 批量还原记录 * /home/grade?act=toRecoverGrades * @param request * @param response * @return */ public JsonResult toRecoverGrades(HttpServletRequest request, HttpServletResponse response) { return gradeService.undoGradeByIdArray(request); } /** * 批量彻底移除记录 * /home/grade?act=toRemoveGrades * @param request * @param response * @return */ public JsonResult toRemoveGrades(HttpServletRequest request, HttpServletResponse response) { return gradeService.removeGradeByIdArray(request); } }
JSP视图渲染的部分就不再展示了,后端的东西到这里就算搞定了