JavaWeb三层架构学习笔记

JavaWeb三层架构
在这里插入图片描述

在这里插入图片描述

三层架构:

在这里插入图片描述

MVC与三层架构:

在这里插入图片描述
在这里插入图片描述

三层代码流程解析:
在这里插入图片描述

实例流程:
在这里插入图片描述

代码示例
Dao层的 StudentDao.java

public class StudentDao {

    private Connection con;

    public StudentDao(Connection con) {
        this.con = con;
    }

    public Connection getCon() {
        return con;
    }

    public void setCon(Connection con) {
        this.con = con;
    }

    //向数据库中插入学生数据
    public int addStudent(Student stu) {
        int result = 0;
        PreparedStatement pst = null;

        try {
            String sql = "insert into student values (?,?,?,?)";
            pst = con.prepareStatement(sql);
            pst.setString(1,stu.getNumber());
            pst.setString(2,stu.getName());
            pst.setString(3,stu.getSex());
            pst.setDate(4, new java.sql.Date(stu.getBirthday().getTime()));

            result = pst.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DruidUtils.close(null,pst);
        }
        return result;
    }

    //查找该学生信息
    public Student selectStudent(String number) {

        Student stu = null;
        System.out.println("new student = " + stu);
        PreparedStatement pst = null;
        ResultSet resultSet = null;

        try {

            String sql = "select * from student where number = ?";
            pst = con.prepareStatement(sql);
            pst.setString(1,number);
            resultSet = pst.executeQuery();
            while (resultSet.next()) {
                stu = new Student();
                String number1 = resultSet.getString("number");
                String name = resultSet.getString("name");
                String sex = resultSet.getString("sex");
                java.util.Date birthday = resultSet.getDate("birthday");


                stu.setNumber(number1);
                stu.setName(name);
                stu.setSex(sex);
                stu.setBirthday(birthday);


            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close(null,pst,resultSet);
        }

        return stu;
    }

}

entinty层的Student.java

public class Student {

    private String number;
    private String name;
    private String sex;
    private Date birthday;

    public Student(String number, String name, String sex, Date birthday) {
        this.number = number;
        this.name = name;
        this.sex = sex;
        this.birthday = birthday;
    }

    public Student() {
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}

service层的 StudentService.java

public class StudentService {

    //执行查询学生信息的操作
    public Student selectStudent(String number) {
        Student student = null;
        Connection con = DruidUtils.getConnection();
        StudentDao studentDao = new StudentDao(con);
        System.out.println("student in service" + student);
        student = studentDao.selectStudent(number);
        System.out.println("student in service" + student);
        return student;

    }

    //执行增加学生的操作
    public int addStudent(String number, String name, String sex, String birthday) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
        Student student = new Student(number,name,sex, sdf.parse(birthday));
        Connection con = DruidUtils.getConnection();
        StudentDao studentDao = new StudentDao(con);
        int i = studentDao.addStudent(student);
        return i;

    }
}

servlet层的AddServlet.java

public class AddServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取用户输入的学生信息
        String number = req.getParameter("number");
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String birthday = req.getParameter("birthday");
        // 创建Session对象, 用于数据共享
        HttpSession session = req.getSession();
        session.setAttribute("number",number);
        session.setAttribute("name",name);
        session.setAttribute("sex",sex);
        session.setAttribute("birthday",birthday);

        StudentService studentService = new StudentService();
        Student student = null;
        System.out.println("student = " + student);
        student = studentService.selectStudent(number);
        //如果返回的student信息不为空,则表述数据库中已存在该学生的信息
        System.out.println("student in a = " + student);
        if (null != student) {
            System.out.printf("该学生信息已存在,请重新输入");
            System.out.printf("Add student failed");
            resp.sendRedirect("/homework/addfail.html");
        } else {
            int i = 0;
            System.out.printf("i = " + i);
            try {
                i = studentService.addStudent(number, name, sex, birthday);
                System.out.printf("ii = " + i);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (0 != i){
                System.out.printf("学生信息添加成功!");
                System.out.printf("Add student success");
                resp.sendRedirect("/homework/addsuccess.html");
            }
        }



    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}

web层的index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
</head>
<body>
<form action="/homework/loginServlet" method="get">
    用户名&nbsp<input type="text" name="username"><br/><br>
    密码&nbsp&nbsp&nbsp&nbsp&nbsp<input type="password" name="password"><br/><br>
    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input type="submit" value="登录">
</form>
</body>
</html>
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页