多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] # Web 分层思想 在现代的 Web 开发中,分层的思想非常的重要,体现了软件复用的重要理念。 从 MVC 层 → 服务层(Service) → 数据访问层(DAO) → 持久化数据层(DB) ,通过数据与业务的层层剥离,减少了代码之间的耦合。 ![](https://box.kancloud.cn/eec2fc401295913d8440ce1393d533e3_500x384.png) - MVC 层 完成页面请求和数据传递的过程,着重在于请求逻辑的实现。 - Service 层 完成核心业务点的数据处理,用于被 Controller 调用,具体的数据持久化分散的交给 DAO 去完成。 - DAO 层 衔接数据库,完成数据实体对象的单个数据库操作,称为数据访问对象。 ## 按照分层思想构建的代码 **包结构示例** ~~~ └─cn ├─controller ├─dao ├─filters ├─model ├─service └─utils ~~~ > 为了避免 Service 和 DAO 层对象实例的多度浪费创建,使用了单例的设计模式构建 Service 和 DAO 的对象。 Controller 代码示例 ~~~ private StudentService studentService = StudentService.getInstance(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { User user = (User) request.getSession().getAttribute("user"); // 1 获取前台用户输入参数 String name = request.getParameter("name"); String code = request.getParameter("code"); Student stu = new Student(); stu.setCode(code); stu.setName(name); // 2 进行业务逻辑处理 int i = studentService.regStudent(stu); // 3 传递数据给显示层使用 request.setAttribute("stu", stu); request.setAttribute("i", i); // 4 跳转到显示层 request.getRequestDispatcher("/WEB-INF/pages/regResult.jsp").forward(request, response); ; } ~~~ Service 代码示例 ~~~ package cn.service; import cn.dao.LogDao; import cn.dao.StudentDao; import cn.model.Log; import cn.model.Student; public class StudentService { private static StudentService studentService = new StudentService(); private StudentService() { } public static StudentService getInstance() { return studentService; } private StudentDao studentDao = StudentDao.getInstance(); private LogDao logDao = LogDao.getInstance(); /** * 学生注册 * @param stu * @return */ public int regStudent(Student stu) { // 插入学生表,同时插入日志 int i = studentDao.insert(stu); if (i == 1) { Log log = new Log(); log.setType("ADD"); log.setContent("插入学生记录:" + stu.getName()); logDao.insert(log); return 1; } return 0; } /** * 编辑学生记录 * @param stu * @return */ public int editStudent(Student stu) { // 根据ID查询出数据中的老对象 Student oldStu = studentDao.findById(stu.getId()); oldStu.setName(stu.getName()); int i = studentDao.update(oldStu); if (i == 1) { Log log = new Log(); log.setType("EDIT"); log.setContent("编辑学生记录:" + stu.getCode()); logDao.insert(log); return 1; } return 0; } } ~~~ DAO 代码示例 ~~~ package cn.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import cn.model.Student; import cn.utils.DbHelp; /** * 学生表的数据访问对象 * * @author lzq31 * */ public class StudentDao { private static StudentDao studentDao = new StudentDao(); private StudentDao() {} public static StudentDao getInstance() { return studentDao; } /** * 插入一个学生记录 * * @param stu * @return */ public int insert(Student stu) { int i = 0; try { Class.forName(DbHelp.JDBC_DRIVER); Connection conn = DriverManager.getConnection(DbHelp.JDBC_URL, DbHelp.JDBC_USERNAME, DbHelp.JDBC_PASSWORD); PreparedStatement ps = conn.prepareStatement("INSERT INTO student(name,code) VALUES(?,?)"); ps.setString(1, stu.getName()); ps.setString(2, stu.getCode()); i = ps.executeUpdate(); ps.close(); conn.close(); } catch (Exception ex) { ex.printStackTrace(); } return i; } /** * 根据 ID 获取学生对象 * * @param id * @return */ public Student findById(int id) { Student stu = null; // 业务逻辑操作 try { Class.forName(DbHelp.JDBC_DRIVER); Connection conn = DriverManager.getConnection(DbHelp.JDBC_URL, DbHelp.JDBC_USERNAME, DbHelp.JDBC_PASSWORD); PreparedStatement ps = conn.prepareStatement("SELECT id,name,code FROM student WHERE id=?"); ps.setInt(1, id); ResultSet rs = ps.executeQuery(); if (rs.next()) { stu = new Student(); stu.setId(rs.getInt("id")); stu.setName(rs.getString("name")); stu.setCode(rs.getString("code")); } ps.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } return stu; } /** * 删除学生 * * @param id * @return */ public int delete(int id) { int i = 0; // 业务逻辑操作 try { Class.forName(DbHelp.JDBC_DRIVER); Connection conn = DriverManager.getConnection(DbHelp.JDBC_URL, DbHelp.JDBC_USERNAME, DbHelp.JDBC_PASSWORD); PreparedStatement ps = conn.prepareStatement("DELETE FROM student WHERE id=?"); ps.setInt(1, id); i = ps.executeUpdate(); ps.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } return i; } /** * 更新学生 * * @param stu * @return */ public int update(Student stu) { int i = 0; // 业务逻辑操作 try { Class.forName(DbHelp.JDBC_DRIVER); Connection conn = DriverManager.getConnection(DbHelp.JDBC_URL, DbHelp.JDBC_USERNAME, DbHelp.JDBC_PASSWORD); PreparedStatement ps = conn.prepareStatement("UPDATE student SET name=?, code=? WHERE id=?"); ps.setString(1, stu.getName()); ps.setString(2, stu.getCode()); ps.setInt(3, stu.getId()); i = ps.executeUpdate(); ps.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } return i; } /** * 查询所有学生 * @return */ public List<Student> findAll() { List<Student> stus = new ArrayList<Student>(); // 获取用户数据列表 try { Class.forName(DbHelp.JDBC_DRIVER); Connection conn = DriverManager.getConnection(DbHelp.JDBC_URL, DbHelp.JDBC_USERNAME, DbHelp.JDBC_PASSWORD); PreparedStatement ps = conn.prepareStatement("SELECT * FROM student"); ResultSet rs = ps.executeQuery(); while (rs.next()) { Student stu = new Student(); int id = rs.getInt("id"); String name = rs.getString("name"); String code = rs.getString("code"); stu.setId(id); stu.setName(name); stu.setCode(code); stus.add(stu); } ps.close(); conn.close(); } catch (Exception ex) { ex.printStackTrace(); } return stus; } } ~~~