文章目录
- 1. 为什么需要分层?
- 2.软件设计中的分层模式
- 3.分层
- 4.三层架构:显示层、业务逻辑层、数据访问层
- 3. 案例:利用三层架构原理实现编写web程序的流程
摘自:https://blog.csdn.net/qq_64001795/article/details/124112824
1. 为什么需要分层?
业务处理的代码与JSP代码混在一起,不易于阅读,不易于代码维护,采用三层软件设计架构后,软件系统在可扩展性和可复用性方面得到极大提高,
2.软件设计中的分层模式
分层模式是最常见的一种架构模式;甚至可以说分层模式是很多架构模式的基础。
3.分层
①分层模式是将解决方案的组件分隔不同的层中(分工合作,细化)
②每一层的组件应保持内聚性(部门之间常联系)
③每一层都应与其下面的各层保持松耦合
4.三层架构:显示层、业务逻辑层、数据访问层
3. 案例:利用三层架构原理实现编写web程序的流程
login.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action ="doLogin.jsp" method = "post">
账户: <input type = "text" name = "username"/>
<br><br>
密码: <input type = "password" name = "password"/>
<br><br>
<input type = "submit" value = "登录"/>
</form>
</body>
</html>
dologin.jsp页面
<%@page import="com.zking.news.biz.impl.UsersBizImpl"%>
<%@page import="com.zking.news.biz.IUsersBiz"%>
<%@page import="com.zking.news.entity.Users"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//设置request编码
request.setCharacterEncoding("utf-8");
//获取表单值
String username = request.getParameter("username");
String password = request.getParameter("password");
//实体封装
Users users = new Users(username,password);
//调用业务逻辑层
IUsersBiz iub = new UsersBizImpl();
Users u = iub.usersLogin(users);
if(u!=null){
session.setAttribute("users", u);
out.println("ok");
}else{
out.println("no");
}
%>
biz包---->impl包---->UsersBizImpl.java
package com.zking.news.biz.impl;
import java.sql.Connection;
import com.zking.news.biz.IUsersBiz;
import com.zking.news.dao.IUserDao;
import com.zking.news.dao.impl.UsersDaoImpl;
import com.zking.news.entity.Users;
import com.zking.news.utils.DBHelper;
//目前阶段 业务逻辑层的实现类 直接拿数据访问层的数据操作即可
public class UsersBizImpl implements IUsersBiz {
//直接实例化数据访问层的dao即可
private IUserDao iud = new UsersDaoImpl();
@Override
public Users usersLogin(Users users) {
Connection con = DBHelper.getConn();
//复杂处理 sql = select * from tb_users where username = ?
//怎么数据
return iud.usersLogin(users);
}
}
biz包---->IUsersBiz.java
package com.zking.news.biz;
import com.zking.news.entity.Users;
public interface IUsersBiz {
/**
* 方法功能:用户登录
* @param Users users 用户实体(封装账户和密码)
* @return Users users 数据库中返回的实体(方便session保存)
*/
Users usersLogin(Users users);
}
Dao包---->Impl包---->UsersDaoImpl.java
package com.zking.news.dao.impl;
import java.sql.ResultSet;
import com.zking.news.dao.IUserDao;
import com.zking.news.entity.Users;
import com.zking.news.utils.BaseDao;
import com.zking.news.utils.DBHelper;
public class UsersDaoImpl extends BaseDao implements IUserDao {
@Override
public Users usersLogin(Users users) {
Users u = null;//存储数据库中返出来的用户实体
//用户登录的sql语句
String sql = "select * from tb_t281_news_users where username = ? and password = ?";
//调用Basedao中的通用查询方法 返回一个结果集对象
ResultSet rs = this.executeQuery(sql, new Object[] {
users.getUsername(),
users.getPassword()
});
try {
//判断结果集是否存在记录
if(rs.next()) {//存在记录
u = new Users(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getInt(7), rs.getString(8));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(conn, ps, rs);
}
return u;
}
public static void main(String[] args) {
Users usersLogin = new UsersDaoImpl().usersLogin(new Users("admin", "123"));
System.out.println(usersLogin);
}
}
Dao包---->IUserDao.java
package com.zking.news.dao;
import com.zking.news.entity.Users;
/**
* 数据访问层接口
* @author Administrator
*
*/
public interface IUserDao {
/**
* 方法功能:用户登录
* @param Users users 用户实体(封装账户和密码)
* @return Users users 数据库中返回的实体(方便session保存)
*/
Users usersLogin(Users users);
}
entity包
package com.zking.news.entity;
/**
* 用户实体
*
* @author Administrator
*
* 数据表中的字段及类型以及字段的名称 与 实体类一一对应
*
*
*/
public class Users {
private int userid;
private String username;
private String password;
private String usex;
private String uaddress;
private String utelphone;
private int urole;
private String udate;
public Users() {
// TODO Auto-generated constructor stub
}
public Users(String username, String password) {
super();
this.username = username;
this.password = password;
}
public Users(String username, String password, String usex, String uaddress, String utelphone, int urole,
String udate) {
super();
this.username = username;
this.password = password;
this.usex = usex;
this.uaddress = uaddress;
this.utelphone = utelphone;
this.urole = urole;
this.udate = udate;
}
public Users(int userid, String username, String password, String usex, String uaddress, String utelphone,
int urole, String udate) {
super();
this.userid = userid;
this.username = username;
this.password = password;
this.usex = usex;
this.uaddress = uaddress;
this.utelphone = utelphone;
this.urole = urole;
this.udate = udate;
}
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
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;
}
public String getUsex() {
return usex;
}
public void setUsex(String usex) {
this.usex = usex;
}
public String getUaddress() {
return uaddress;
}
public void setUaddress(String uaddress) {
this.uaddress = uaddress;
}
public String getUtelphone() {
return utelphone;
}
public void setUtelphone(String utelphone) {
this.utelphone = utelphone;
}
public int getUrole() {
return urole;
}
public void setUrole(int urole) {
this.urole = urole;
}
public String getUdate() {
return udate;
}
public void setUdate(String udate) {
this.udate = udate;
}
@Override
public String toString() {
return "Users [userid=" + userid + ", username=" + username + ", password=" + password + ", usex=" + usex
+ ", uaddress=" + uaddress + ", utelphone=" + utelphone + ", urole=" + urole + ", udate=" + udate + "]";
}
}
utils包(BaseDao和DBHelper
package com.zking.news.utils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class BaseDao {
//protected 访问控制符 受保护的
protected Connection conn =null;
protected PreparedStatement ps = null;
protected ResultSet rs = null;
/**
* 方法功能:通用增删改方法
*/
public int executeUpdate(Connection conn,String sql,Object...x) {
int n = 0;
try {
// a.获取数据库连接
conn = DBHelper.getConn();
// b.sql传入方法返回执行对象
ps = conn.prepareStatement(sql);
//新增的sql语句 有占位符
if(null!=x) {
//拿到可变参数中的2个值
for(int i = 0;i<x.length;i++) {
//System.out.println(x[i]);
ps.setObject(i+1, x[i]);
}
}
n = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(conn, ps, null);
}
return n;
}
/**
* 方法功能:通用查询 查询所有 查询一个 模糊查询
*/
public ResultSet executeQuery(String sql,Object...x) {
try {
// a.获取数据库连接
conn = DBHelper.getConn();
// b.sql传入方法返回执行对象
ps = conn.prepareStatement(sql);
//新增的sql语句 有占位符
if(null!=x) {
//拿到可变参数中的2个值
for(int i = 0;i<x.length;i++) {
//System.out.println(x[i]);
ps.setObject(i+1, x[i]);
}
}
rs = ps.executeQuery();
} catch (Exception e) {
e.printStackTrace();
}finally {
//不能关闭 通用的查询base方法 不能实现数据库关闭
//DBHelper.myClose(conn, ps, null);
}
return rs;
}
}
package com.zking.news.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
* 数据库帮助类
*
* @author Administrator
*
*/
public class DBHelper {
private static final String URL = "jdbc:oracle:thin:@localhost:1521:orcl";
// 1.加载驱动 静态代码块进行封装 优先级别最高(静态代码块>普通代码块>构造函数)
static {
try {
// OracleDriver
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (Exception e) {
e.printStackTrace();
}
}
// 2.建立数据库连接
public static Connection getConn() {
Connection conn = null;
try {
conn = DriverManager.getConnection(URL, "scott", "123");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void main(String[] args) {
// System.out.println(DBHelper.getConn());
}
// 3.关闭服务
/**
* 方法功能:数据库服务关闭
*
* @param conn
* 连接对象
* @param ps
* 执行对象
* @param rs
* 结果集对象
*/
public static void myClose(Connection conn, PreparedStatement ps, ResultSet rs) {
try {
if (null != conn && !conn.isClosed()) {
conn.close();
}
if (null != ps) {
ps.close();
}
if (null != rs) {
rs.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}