<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                [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; } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看