三层结构详解
1、三层结构:
与MVC设计模式的目标一致:都是为了 解耦合、提高代码的复用;
区别:理解的角度不同。
三层结构对应的MVC:
2、三层结构的组成
表示层(USL,User Show Layer :视图层)
-前台:对应于MVC中的View:用于用户交互、界面的显示
代码:jsp js html css jquery等web前端技术
代码位置:WebContent
-后台:对应于MVC中的Controller:用于控制器跳转、调用业务逻辑层
Servlet(SpringMVC Struts2)
代码位置:。。。。。.servlet包
业务逻辑层(BBL, Business Logic Layer : Service层)
-调用表示层的请求,调用
-用于组装数据访问层,逻辑性的操作(增删改查,删:查+删)
一般位于xxx.service包
数据访问层(DAL,Data Access Layer :Dao层)
-直接范问数据库的操作,原子性操作(增删改查-不带逻辑)
一般位于 xxx.dao包
在进行删时,先访问数据库,参看数据是否存在再进行下一步
3、案例
代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="../AddStudentServlet" method="post" > 学号:<input type='text' name= "sno" /><br/> 姓名:<input type='text' name= "sname" /><br/> 年龄:<input type='text' name= "sage" /><br/> 地址:<input type='text' name= "saddress" /><br/> <input type="submit" value="新增" /><br/> </form> </body> </html>
package org.student.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.student.entity.Student; import org.student.service.StudentService; @WebServlet("/AddStudentServlet") public class AddStudentServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); int no = Integer.parseInt( request.getParameter("sno")); String name = request.getParameter("sname"); int age = Integer.parseInt(request.getParameter("sage")); String address = request.getParameter("saddress"); Student student = new Student(no,name,age,address); StudentService studentService = new StudentService(); boolean result = studentService.addStudent(student); PrintWriter out = response.getWriter(); if(result) { out.println("添加成功"); }else { out.println("添加失败"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package org.student.service; import org.student.dao.StudentDao; import org.student.entity.Student; //业务逻辑层:逻辑性的增删改查(增:查+增),对dao层进行的组装 public class StudentService { StudentDao studentDao = new StudentDao(); public boolean addStudent(Student student) { if(studentDao.isExist(student.getSno())) { System.out.println("此人已存在"); return false; }else { studentDao.addStudent(student); } return false; } }
package org.student.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.student.entity.Student; public class StudentDao { private final String url="jdbc:sqlserver://localhost:1433;DatabaseName=test"; private final String user ="sa"; private final String password="123456"; public boolean isExist(int sno) {//true此人存在;false此人不存在 // return quertStudentBysno(sno)==null?false:true; // 三目写法 if(quertStudentBysno(sno)==null) { return false; }else { return true; } } // 增加学生 public boolean addStudent(Student student) { Connection con =null; PreparedStatement pstmt =null; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); con = DriverManager.getConnection(url, user, password); // 导入数据 String sql ="insert into student values(?,?,?,?)"; pstmt = con.prepareStatement(sql); pstmt.setInt(1, student.getSno()); pstmt.setString(2, student.getSname()); pstmt.setInt(3, student.getSage()); pstmt.setString(4, student.getSaddress()); int count = pstmt.executeUpdate(); // true成功导入,false导入失败 if(count >0) { return true; }else { return false; } } catch (ClassNotFoundException e) { e.printStackTrace(); }catch(SQLException e) { e.printStackTrace(); }catch(Exception e) { e.printStackTrace(); }finally { try { if(pstmt!=null)pstmt.close(); if(con!=null)con.close(); } catch (SQLException e) { e.printStackTrace(); } } return false; } // 根据学号查询学生 public Student quertStudentBysno(int sno) { Student student = null; Connection con =null; PreparedStatement pstmt =null; ResultSet rs = null; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); con = DriverManager.getConnection(url, user, password); String sql ="select * from student where sno =?"; pstmt = con.prepareStatement(sql); pstmt.setInt(1, sno); rs = pstmt.executeQuery(); if(rs.next()) { int no = rs.getInt("sno"); String name = rs.getString("sname"); int age = rs.getInt("sage"); String address = rs.getString("saddress"); student = new Student(no,name,age,address); } return student; } catch (ClassNotFoundException e) { e.printStackTrace(); return null; }catch(SQLException e) { e.printStackTrace(); return null; }catch(Exception e) { e.printStackTrace(); return null; }finally { try { if(rs!=null)rs.close(); if(pstmt!=null)pstmt.close(); if(con!=null)con.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
package org.student.entity; public class Student { private int sno; private String sname; private int sage; private String saddress; public Student(int sno, String sname, int sage, String saddress) { this.sno = sno; this.sname = sname; this.sage = sage; this.saddress = saddress; } public Student(String sname, int sage, String saddress) { this.sname = sname; this.sage = sage; this.saddress = saddress; } public int getSno() { return sno; } public void setSno(int sno) { this.sno = sno; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public int getSage() { return sage; } public void setSage(int sage) { this.sage = sage; } public String getSaddress() { return saddress; } public void setSaddress(String saddress) { this.saddress = saddress; } }