java web MVC architecture

One, MVC architecture

  1. Bean layer: a model that encapsulates data. The specific embodiment in Java is the definition of a data class
  2. Action layer: also called the presentation layer, the specific embodiment in Java is the design and interaction of the front desk
  3. Dao layer: also known as the persistence layer, mainly to provide data for the front desk
  4. Service layer: also called the service layer, adding business logic on the basis of the dao layer
  5. Util layer: also called tool layer

Insert picture description here

Query all student information

One, first import the jar package
Insert picture description here

  1. The bean layer (unchanged) is used to associate database tables
    Insert picture description here
  2. The util layer to open and close the database is
    basically the same, as long as the following database name is changed.
package util;

import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//打开关闭数据库
public class DBUtil {
    
    
	static Connection conn;
	public static Connection getConnection(){
    
    
		try {
    
    
			Class.forName("com.mysql.jdbc.Driver");
			//那个edu就是数据库,哪个数据库连接哪个。
			String url="jdbc:mysql://localhost:3306/edu?useUnicode=true&characterEncoding=UTF-8";
			String userName="root";//自己数据库的账号
			String password="";//自己数据库的密码
			conn=DriverManager.getConnection(url, userName, password);
		} catch (Exception e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	
	public static void close(Connection conn,Statement stmt,ResultSet rs){
    
    
			try {
    
    
				if(rs!=null){
    
    
					rs.close();
				}
				if(stmt!=null){
    
    
					stmt.close();
				}
				if(conn!=null){
    
    
					conn.close();
				}
			} catch (SQLException e) {
    
    
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	public static void main(String[] args){
    
    
		Connection conn=DBUtil.getConnection();
		if(conn!=null){
    
    
			System.out.println("数据库连接成功");
		}else{
    
    
			System.out.println("数据库连接失败");
		}
	}
}

  1. The dao layer provides data for the front desk
package dao;

import java.sql.Connection;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


import bean.Student;
import util.DBUtil;

public class StudentDao {
    
    
	Connection conn=null;
	Statement stmt=null;
	ResultSet rs=null;
	List<Student> stu=null;
	int n=0;
	//查询所有的学生信息;
	public List<Student> selectAllStudent(){
    
    
		try {
    
    
			//用静态类就可以这样调用
			conn=DBUtil.getConnection();
			stmt=conn.createStatement();
			String sql="select * from student";
			rs= stmt.executeQuery(sql);
			
			//这里定义一个列表,把结果集中的Student的对象存放列表中
			
			stu=new ArrayList<Student>();
			while(rs.next()){
    
    
				//把结果集当前的数据转成一个Student对象
				Student s=new Student();
				s.setS_id(rs.getString("s_id"));
				s.setS_name(rs.getString("s_name"));
				s.setS_sex(rs.getBoolean("s_sex"));
				s.setS_birthday(rs.getDate("s_birthday"));
				s.setS_major(rs.getString("s_major"));
				s.setS_scholarship(rs.getInt("s_scholarship"));
				//把对象存到列表
				stu.add(s);
//			String xh= rs.getString("s_id");
//			String xm=rs.getString("s_name");
//			String xb=rs.getString("s_sex");
//			Date csrq =rs.getDate("s_birthday");
//			System.out.println("xh="+xh+",xm="+xm+",xb="+xb+",csrq="+csrq);
			}
		} catch (SQLException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return stu;
	}
	//根据姓名模糊查询相关学生信息
	public List<Student> selectStudentLikeName(String xm) {
    
    
		
		try {
    
    
			//用静态类就可以这样调用
			conn=DBUtil.getConnection();
			String sql="select * from student where s_name like ?";
			PreparedStatement pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, "%".concat(xm).concat("%"));
			rs= pstmt.executeQuery();
			
			//这里定义一个列表,把结果集中的Student的对象存放列表中
			
			stu=new ArrayList<Student>();
			while(rs.next()){
    
    
				//把结果集当前的数据转成一个Student对象
				Student s=new Student();
				s.setS_id(rs.getString("s_id"));
				s.setS_name(rs.getString("s_name"));
				s.setS_sex(rs.getBoolean("s_sex"));
				s.setS_birthday(rs.getDate("s_birthday"));
				s.setS_major(rs.getString("s_major"));
				s.setS_scholarship(rs.getInt("s_scholarship"));
				//把对象存到列表
				stu.add(s);
//			String xh= rs.getString("s_id");
//			String xm=rs.getString("s_name");
//			String xb=rs.getString("s_sex");
//			Date csrq =rs.getDate("s_birthday");
//			System.out.println("xh="+xh+",xm="+xm+",xb="+xb+",csrq="+csrq);
			}
		} catch (SQLException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return stu;
	}
	//添加学生信息
	//修改学生信息;
	//删除学生信息;
	public int insertStudent(Student s) {
    
    
		// TODO Auto-generated method stub
		try {
    
    
			//用静态类就可以这样调用
			conn=DBUtil.getConnection();
			String sql="insert into student values(?,?,?,?,?,?)";
			PreparedStatement pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, s.getS_id());
			pstmt.setString(2, s.getS_name());
			pstmt.setBoolean(3, s.isS_sex());
			//把util.Date数据类型转成sql.Date的数据类型
			pstmt.setDate(4, new Date(s.getS_birthday().getTime()));
			pstmt.setString(5, s.getS_major());
			pstmt.setInt(6, s.getS_scholarship());
			n=pstmt.executeUpdate();
		} catch (SQLException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("n="+n);
		return n;
	}
}
  1. service layer, add business logic on the basis of dao layer
    , there are 3 project codes in it
package service;

import java.util.List;


import bean.Student;
import dao.StudentDao;

public class StudentService {
    
    
	StudentDao studentDao;
	public StudentService(){
    
    
		studentDao=new StudentDao();
	}
	//查询整张表格的数据
	public List<Student> selectAllStudent(){
    
    
		List<Student> stu=studentDao.selectAllStudent();
		return stu;
	}
	//模糊查询的学生的数据
	public List<Student> selectStudentLikeName(String xm) {
    
    
		return studentDao.selectStudentLikeName(xm);
	}
	
	//插入学生数据代码
	public int insertStudent(Student s) {
    
    
		// TODO Auto-generated method stub
		return studentDao.insertStudent(s);
	}
	
}


  1. The action layer, also called the presentation layer, is embodied in the design and interaction of the front desk in Java
  • Query the code of the entire table
try {
    
    
//			StudentDao studentDao=new StudentDao();
//			List<Student> stu=studentDao.selectAllStudent();
	StudentService studentService=new StudentService();
	List<Student> stu=studentService.selectAllStudent();
	//把列表存到域对象
	request.setAttribute("stus", stu);
	RequestDispatcher dispatcher=request.getRequestDispatcher("selectResult.jsp");
dispatcher.forward(request, response);
} catch (Exception e) {
    
    
	// TODO Auto-generated catch block
	e.printStackTrace();
}finally{
    
    
	DBUtil.close(conn, stmt, rs);
}
  • Fuzzy query of student's code
request.setCharacterEncoding("utf-8");
String xm=request.getParameter("name");
//调用StudentSeivlet层的方法来完成模糊查询
StudentService studentService=new StudentService();
List<Student> stu=studentService.selectStudentLikeName(xm);
request.setAttribute("stus", stu);
RequestDispatcher dispatcher=request.getRequestDispatcher("selectResult.jsp");
dispatcher.forward(request, response);
  • Code to insert table data
request.setCharacterEncoding("utf-8");
String xh=request.getParameter("xh");
String xm=request.getParameter("xm");
String xb=request.getParameter("xb");
String csrq=request.getParameter("csrq");
String zy=request.getParameter("zy");
String jxj=request.getParameter("jxj");
Student s=new Student();
s.setS_id(xh);
s.setS_name(xm);
if(xb.equals("男")){
    
    
	s.setS_sex(true);
}
if(xb.equals("女")){
    
    
	s.setS_sex(false);
}
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
Date date;
try {
    
    
	date = format.parse(csrq);
	s.setS_birthday(date);
} catch (ParseException e) {
    
    
// TODO Auto-generated catch block
	e.printStackTrace();
}
s.setS_major(zy);
s.setS_scholarship(Integer.parseInt(jxj));
StudentService studentService=new StudentService();
int n=studentService.insertStudent(s);
System.out.println("servlet里面的n="+n);
  1. web interface
    Insert picture description here
    Insert picture description here
    Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44931166/article/details/106304677