一個簡單的案例學會三層架構

三層架構與 MVC 設計模式很類似。

三層步驟

1、前臺代碼獲取用戶數據
2、編寫與數據庫表機構對應的JavaBean
3、servlet 獲取用戶數據並組裝成 Javabean
4、編寫dao 的原子操作
5、service 創建dao對象,組裝dao原子,形成邏輯
6、servlet 創建service 對象, 把組裝好的Javabean傳入service

在這裏插入圖片描述

一、視圖層

前臺

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; 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>

servlet

package org.student.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.jasper.tagplugins.jstl.core.Out;
import org.student.entity.Student;
import org.student.service.StudentService;

//視圖層後臺servlet
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);
		//servlet依賴於service層,需要調用service的函數
		StudentService studentService = new StudentService();
		boolean result = studentService.addStudent(student);
		
		
		//設置響應編碼
		response.setContentType("text/html; charset=UTF-8");
		response.setCharacterEncoding("utf-8");
		
		//jsp:out內置對象
		PrintWriter out = response.getWriter();
		if(result) {
			out.println("增加成功!");
		}else {
			out.println("增加失敗!!");
		}
		
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

javabean

package org.student.entity;

public class Student {
	private int sno;
	private String sname;
	private int sage;
	private String 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;
	}
	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 Student(){
	}
	
	
}

二、數據訪問層

dao層存在大量代碼相同,因此可以優化,此處不展示。

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:mysql://120.76.156.19:3306/STD2017?useUnicode=true&characterEncoding=utf-8";
	private final String USERNAME="STD2017";
	private final String PASSWORD="STD20171QAZ";
	
	
	//增加學生
	public boolean addStudent(Student student) {
		Connection connection = null;
		PreparedStatement pstmt = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
			String sql = "insert into student values(?,?,?,?)";
			pstmt = connection.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();
			
			if(count > 0) {
				return true;
			}else {
				return false;
			}
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return false;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally {
			
				try {
					if(pstmt !=null)pstmt.close();
					if(connection!=null)connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
	}
	
	//是否存在
	public boolean isExist(int sno) {
		return queryStudentBysno(sno)==null?false:true;
	}
	
	
	//根據學號查人
	public Student queryStudentBysno(int sno) {
		Student student = null;
		Connection connection = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
			String sql = "select * from student where sno = ?";
			pstmt = connection.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(connection!=null)connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
		
	}
	
}

三、業務邏輯層

package org.student.service;

import org.student.dao.StudentDao;
import org.student.entity.Student;

//業務邏輯層(簡單對dao層組裝)
public class StudentService	 {
	//業務邏輯層依賴於dao層,需要dao層對象
	StudentDao studentDao=new StudentDao();
	
	//增加:查+ 增
	public boolean addStudent(Student student) {
		if(!studentDao.isExist(student.getSno() ) ) {
			studentDao.addStudent(student);
			return true;
		}else {
			System.out.println("此人已經存在!");
			return false;
		}
	}
	
}

此案例只是簡單的案例,用於學習和理解。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章