在IDEA环境下,创建⼀个三层架构项⽬,以学⽣管理系统增
加学⽣信息为例
三层架构分别是
表⽰层:MVC中的视图层和控制器层,前端通常为html css js jsp⽂件,后端例如Servlet⽂件,调⽤业务逻辑层
业务逻辑层(Service):接受表⽰层的请求,先进⾏逻辑处理,后调⽤数据逻辑层,(增:查+增)
数据访问层(Dao):直接访问数据库的操作
通过三层架构,将表⽰层请求传递到数据访问层,数据访问层将结果返回⾄表⽰层
具体流程
下⾯为代码(环境:IDEA Mysql  8.0.19        mysql driver:8.0.15  servlet:3.0以上版本)
代码结构(正规来说的话service 和 dao 需要接⼝,为了代码看着⽅便省略了)
add.jsp
<%--
Created by IntelliJ IDEA.
User: Laura Li
Date: 2020/3/2
Time: 12:56
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html>
<head>
<title>add</title>
</head>
<body>
<form action="addStudentServlet" >
学号:<input type="number" name="sno"><br/>
name: <input type="text" name="sname"/><br/>
age: <input type="text" name="sage"/><br/>
address: <input type="text" name="saddress"/><br/>
<input type="submit" value="add"/><br/>
</form>
</body>
mvc和三层架构的理解</html>
写后端代码前,先将数据封装Java bean(entity包中)
package ity;
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;
}
@Override
public String toString() {
return "Student{" +
"sno=" + sno +
", sname='" + sname + '\'' +
", sage=" + sage +
", saddress='" + saddress + '\'' +
'}';
}
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = 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;
}
}
addStudentServlet.java(com.student.servlet包中)
import com.Student.service.StudentService;
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 java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/addStudentServlet")
public class addStudentServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// response.setCharacterEncoding("utf-8"); // 指定输出到客户端的是编码格式
response.setContentType("text/html;charset=UTF-8"); // 指定浏览器解析数据的时候,使⽤的编码格式
int no = Integer.Parameter("sno"));
String name = Parameter("sname");
int age = Integer.Parameter("sage"));
String address = Parameter("saddress");
Student student = new Student(no, name, age, address);
StudentService studentService = new StudentService();
boolean result = studentService.addStudent(student);
PrintWriter out = Writer();
if(result){
out.println("add successfully");
}else{
out.println("fail to add");
}
/*if (!result) {  //如果增加失败,给request放⼊⼀条数据error
request.setAttribute("error", "error");
}
}
}
addStudentService.java(⾃⾏匹配放包中)
import ity.Student;
import java.util.List;
//业务逻辑层:逻辑性的增删改查(增:查+增),对Dao层进⾏组装public class StudentService {
StudentDao studentDao = new StudentDao();
public boolean addStudent(Student student){
if(!studentDao.Sno())){      //此⼈不存在
studentDao.addStudent(student);
return true;
}else{
System.out.println("此⼈已存在");
return false;
}
}
public boolean deleteStudent(int sno){
if(studentDao.isExist(sno)){
return studentDao.deleteStudentBySno(sno);
}else{
return false;
}
}
public boolean updateStudentBySno(int sno,Student student){            if(studentDao.isExist(sno)){
return studentDao.updateStudentBySno(sno, student);            }else{
return false;
}
}
//根据学号查询⼀个学⽣
public Student queryStudentBySno(int sno){
return studentDao.queryStudentBySno(sno);
}
public List<Student> queryAllStudent(){
return studentDao.queryAllStudent();
}
}
StudentDao.java

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。