層與層之間的關(guān)系表示層依賴(lài)于業(yè)務(wù)邏輯層,業(yè)務(wù)邏輯層依賴(lài)于數(shù)據(jù)訪(fǎng)問(wèn)層
三層開(kāi)發(fā)遵循的原則(1)上層依賴(lài)其下層,依賴(lài)關(guān)系不跨層1、表示層不能直接訪(fǎng)問(wèn)數(shù)據(jù)訪(fǎng)問(wèn)層2、上層調(diào)用下層的結(jié)果,取決于下層的實(shí)現(xiàn)(2)下一層不能調(diào)用上一層(3)下一層不依賴(lài)上一層1、上層的改變不會(huì)影響下一層2、下層的改變會(huì)影響上一層得到的結(jié)果(4)在上一層中不能出現(xiàn)下一層的概念1、分工明確,各司其職分層開(kāi)發(fā)的特點(diǎn)(1)下層不知道上層的存在1、僅完成自身的功能2、不關(guān)心結(jié)果如何使用(2)每一層僅知道其下層的存在,忽略其他層的存在1、只關(guān)心結(jié)果的取得2、不關(guān)心結(jié)果的實(shí)現(xiàn)過(guò)程3、JSTL通常會(huì)與EL表達(dá)式合作實(shí)現(xiàn)JSP頁(yè)面的編碼分層開(kāi)發(fā)的優(yōu)勢(shì)1、職責(zé)劃分清晰2、無(wú)損替換3、復(fù)用代碼4、降低了系統(tǒng)內(nèi)部的依賴(lài)程度分層開(kāi)發(fā)的案例:package DB;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;/** * 數(shù)據(jù)庫(kù)連接幫助類(lèi) * @author Administrator * */public class BaseDao { //=======數(shù)據(jù)庫(kù)連接信息======== static String url = "jdbc:Oracle:thin:@localhost:1521:orcl"; static String username = "news"; static String passWord = "news"; static String driverClassName = "oracle.jdbc.driver.OracleDriver"; //獲取數(shù)據(jù)庫(kù)連接 public static Connection getConnection(){ Connection connection = null; try { //加載驅(qū)動(dòng) Class.forName(driverClassName); //獲取連接 connection = DriverManager.getConnection(url,username,password); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.PRintStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return connection; } /* * 釋放連接 * */ public void close_con(Connection connection,Statement st,ResultSet rs){ try { if(connection!= null){ connection.close(); } if(st!= null){ st.close(); } if(rs!= null){ rs.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}package entity;/** * 用戶(hù)實(shí)體 * @author Administrator * */public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }package Dao;import java.util.List;import entity.User;/** * 用戶(hù)數(shù)據(jù)訪(fǎng)問(wèn)層接口 * @author Administrator * */public interface UserDao { /** * 查詢(xún)用戶(hù)是否存在 * @param user * @return */ public boolean findUser(User user); /** * 查詢(xún)用戶(hù)列表 */ public List<User> findUserList();}package Dao.Impl;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import DB.BaseDao;import Dao.UserDao;import entity.User;/** * 用戶(hù)數(shù)據(jù)訪(fǎng)問(wèn)層實(shí)現(xiàn)類(lèi) * @author Administrator * 繼承數(shù)據(jù)庫(kù)連接類(lèi) */public class UserDaoImpl extends BaseDao implements UserDao { //數(shù)據(jù)庫(kù)連接對(duì)象 Connection conn = null; PreparedStatement stm = null; ResultSet rs = null; /** * 實(shí)現(xiàn)查詢(xún)用戶(hù)功能 */ public boolean findUser(User user) { //獲得連接 conn = this.getConnection(); //編寫(xiě)sql String sql = "select * from news_users where uname = ? and upwd = ?"; try { //得到預(yù)編譯對(duì)象 stm = conn.prepareStatement(sql); stm.setString(1, user.getUsername()); stm.setString(2, user.getPassword()); //執(zhí)行sql得到結(jié)果集 rs = stm.executeQuery(); return rs.next(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ //釋放連接 this.close_con(conn, stm, rs); } return false; } /** * 查詢(xún)用戶(hù)列表 */ public List<User> findUserList() { //獲得連接 conn = this.getConnection(); //編寫(xiě)sql String sql = "select * from news_users"; //創(chuàng)建泛型集合 List<User> userlist = new ArrayList<User>(); try { //得到預(yù)編譯對(duì)象 stm = conn.prepareStatement(sql); //執(zhí)行sql得到結(jié)果集 rs = stm.executeQuery(); while(rs.next()){ User user = new User(); user.setUsername(rs.getString(2)); user.setPassword(rs.getString(3)); userlist.add(user); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; }finally{ //釋放連接 this.close_con(conn, stm, rs); } return userlist; }}package Biz;import java.util.List;import entity.User;/** * 用戶(hù)業(yè)務(wù)邏輯層接口 * @author Administrator * */public interface UserBiz { /** * 用戶(hù)登錄 * @param user * @return */ public boolean login(User user); /** * 查詢(xún)用戶(hù)列表 */ public List<User> finduserList();}import Dao.UserDao;import Dao.Impl.UserDaoImpl;import entity.User;/** * 用戶(hù)業(yè)務(wù)邏輯實(shí)現(xiàn)類(lèi) * @author Administrator * */public class UserBizImpl implements UserBiz { //創(chuàng)建數(shù)據(jù)訪(fǎng)問(wèn)層對(duì)象 UserDao userdao = new UserDaoImpl(); /** * 實(shí)現(xiàn)登錄 */ public boolean login(User user) { //調(diào)用數(shù)據(jù)訪(fǎng)問(wèn)層查詢(xún)用戶(hù) return userdao.findUser(user); } /** * 查詢(xún)用戶(hù)列表 */ public List<User> finduserList() { // TODO Auto-generated method stub return userdao.findUserList(); }}JSP頁(yè)面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="descrCSS" href="styles.css"> --> </head> <body> <form action="dologin.jsp" method="post"> <p>用戶(hù)名:<input type="text" name="username" /></p> <p>密 碼:<input type="password" name="password" /></p> <input type="submit" /> </form> </body></html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@page import="Biz.UserBiz"%><%@page import="Biz.Impl.UserBizImpl"%><%@page import="entity.User"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; //調(diào)用業(yè)務(wù)邏輯層 UserBiz userbiz = new UserBizImpl(); //獲取表單項(xiàng) request.setCharacterEncoding("utf-8"); String username = request.getParameter("username"); String password = request.getParameter("password"); //創(chuàng)建用戶(hù)實(shí)體 User user = new User(); user.setUsername(username); user.setPassword(password); //實(shí)現(xiàn)登錄 boolean f = userbiz.login(user); if(f){ request.getRequestDispatcher("success.jsp").forward(request,response); }else{ %> <script type="text/Javascript"> alert("用戶(hù)名或密碼錯(cuò)誤!"); location.href="index.jsp"; </script> <% } %>新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注