Java Web应用优化 认识软件设计分层架构
2023-12-14 02:06:16
分层资料的整理
先准备值类和连接数据库的工具类
package week17.entity;
public class User {
private int id;
private String uname;
private String pwd;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public User(int id, String uname, String pwd) {
super();
this.id = id;
this.uname = uname;
this.pwd = pwd;
}
public User() {
id=0;
uname="";
pwd="";
}
@Override
public String toString() {
return "User [id=" + id + ", uname=" + uname + ", pwd=" + pwd + ", toString()=" + super.toString() + "]";
}
}
package week17.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class BaseDao {
private final static String driver = "com.mysql.cj.jdbc.Driver"; // 数据库驱动
private final static String url = "jdbc:mysql://mysql.sqlpub.com:3306/huangjin"; // url
private final static String dbName = "laocooon"; // 数据库用户名
private final static String dbPass = "fc12f7a5215e8e0a"; // 数据库密码
private static Connection conn = null;
private static PreparedStatement pstmt = null;
private static ResultSet rs = null;
public static Connection getConn() throws Exception{
Class.forName(driver); //注册驱动
return DriverManager .getConnection(url,dbName,dbPass); //获得数据库连接并返回
}
public static void closeAll( Connection conn, PreparedStatement pstmt, ResultSet rs ) throws Exception {
if(rs != null) rs.close();
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
}
public static void closeAll( Connection conn, PreparedStatement pstmt) throws Exception {
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
}
//直接关静态成员的资源
public static void closeAll() throws Exception {
closeAll(conn,pstmt,rs);// 关 17 18 19行的资源的
}
//增删改 返回多少条发生了变化,如果0表示没变化
public static int executeUpdataSQL(String sql,String[] param)throws Exception {
Connection conn = null;
PreparedStatement pstmt = null;
conn = getConn();
pstmt = conn.prepareStatement(sql);
for( int i = 0; i < param.length; i++ )
pstmt.setString(i+1, param[i]);
int num = pstmt.executeUpdate();
closeAll(conn,pstmt);
return num;
}
//调用此代码,使用完 记录集的内容之后,通过 closeAll();
public static ResultSet executeQuerySQL(String sql,String[] param)throws Exception {
closeAll();
conn = getConn();
pstmt = conn.prepareStatement(sql);
for( int i = 0; i < param.length; i++ )
pstmt.setString(i+1, param[i]);
rs = pstmt.executeQuery();
return rs;
}
}
数据访问层的类
package week17.dao;
import java.util.List;
import week17.entity.User;
//打算有哪些业务
public interface UserDao {
public User findUser(String uname) throws Exception;
public List<User> allUser() throws Exception;
public int addUser(User user) throws Exception;
}
package week17.dao.impl;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import week17.dao.UserDao;
import week17.entity.User;
import week17.util.BaseDao;
public class UserDaoImpl implements UserDao {
@Override
public User findUser(String uname) throws Exception {
// TODO Auto-generated method stub
String sql="SELECT * FROM adminuser WHERE uname= ?";
String[] param = {uname};
ResultSet rs=BaseDao.executeQuerySQL(sql, param);
User user= new User();
while(rs.next()) {
user.setId(rs.getInt(1));
user.setUname(rs.getString(2));
user.setPwd(rs.getString(3));
}
BaseDao.closeAll();
rs=null;
return user;
}
@Override
public List<User> allUser() throws Exception {
String sql="SELECT * FROM adminuser";
String[] param = {};
ResultSet rs=BaseDao.executeQuerySQL(sql, param);
List<User> list=new ArrayList<User>();
while(rs.next()) {
User user= new User();
user.setId(rs.getInt(1));
user.setUname(rs.getString(2));
user.setPwd(rs.getString(3));
list.add(user);
}
BaseDao.closeAll();
return list;
}
@Override
public int addUser(User user) throws Exception {
String sql="INSERT INTO adminuser (uname,pwd) value(?,?)";
String[] param = {user.getUname(),user.getPwd()};
int row = BaseDao.executeUpdataSQL(sql, param);
BaseDao.closeAll();
return row;
}
}
业务逻辑层的类
package week17.biz;
import java.util.List;
import week17.entity.User;
public interface UserBiz {
public boolean login(User user) throws Exception;
public List<User> getAll() throws Exception;
public int addUser(User user) throws Exception;
}
package week17.biz.impl;
import java.util.List;
import week17.biz.UserBiz;
import week17.dao.UserDao;
import week17.dao.impl.UserDaoImpl;
import week17.entity.User;
public class UserBizImpl implements UserBiz {
@Override
public boolean login(User user) throws Exception {
UserDao userDao=new UserDaoImpl();
System.out.println("login");
User u=userDao.findUser(user.getUname());
return u.getPwd().equals(user.getPwd());
}
@Override
public List<User> getAll() throws Exception {
// TODO Auto-generated method stub
UserDao ud=new UserDaoImpl();
return ud.allUser();
}
@Override
public int addUser(User user) throws Exception {
UserDao ud=new UserDaoImpl();
String uname=user.getUname();
User u=ud.findUser(uname);
return u.getUname().equals(uname)?0:ud.addUser(user);
}
}
表示层
<%-- dologin.jsp --%>
<%@page import="week17.biz.impl.UserBizImpl"%>
<%@page import="week17.biz.UserBiz"%>
<%@page import="week17.entity.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
String uname=request.getParameter("username");
String pwd=request.getParameter("password");
if(uname==null||pwd==null)
return ;
User u=new User();
u.setUname(uname);
u.setPwd(pwd);
UserBiz userBiz=new UserBizImpl();
if(userBiz.login(u)){
session.setAttribute("user", u);
//去管理页
out.println("登录成功~!");
response.sendRedirect("./listall.jsp");
}
else{
//去登录页
response.sendRedirect("./login.jsp");
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>验证检测</title>
</head>
<body>
</body>
</html>
<%-- listall.jsp --%>
<%@page import="week17.biz.impl.UserBizImpl"%>
<%@page import="week17.biz.UserBiz"%>
<%@page import="week17.entity.User"%>
<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
UserBiz ub=new UserBizImpl();
List<User> list=ub.getAll();
for(User user:list){
out.println(user+"<br>");
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
<%-- doadd.jsp --%>
<%@page import="week17.biz.impl.UserBizImpl"%>
<%@page import="week17.biz.UserBiz"%>
<%@page import="week17.entity.User"%>
<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
User user=new User(0,"sss1","pwds");
UserBiz ub=new UserBizImpl();
if(ub.addUser(user)>0){
out.println("添加成功");
}else{
out.println("已经注册过了");
return ;
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
文章来源:https://blog.csdn.net/laocooon/article/details/134912657
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!