记录通过浏览器操作数据库 , 前端页面有点不熟悉 , 见谅啊~~!
三层架构
按照不同功能分为不同层,通常分为三层:表现层(web层),业务层,持久(数据库)层。
其中
- WEB层(Servlet)
- 获得请求参数
- 创建业务对象,调用业务处理参数结果
- 响应
- 业务层(Service)
- 处理业务(请求参数处理)
- 创建Dao对象,调用Dao
- 持久层(Dao)
- 操作数据库
该程序设计的宗旨:
- 高内聚低耦合
- 可拓展性强
- 可维护性强
- 可重用性强
打码环节
效果图
管理员登录页面
超级管理员注册登录页面
超级管理员注册页面
数据库查看页面
用户信息新增页面
用户信息删除页面
用户信息修改页面
打码
- 项目结构
Web层
RootServlet(登录页面的数据处理)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117package com.lqh.Demo.web.Servlet;
import com.lqh.Demo.Domain.Root;
import com.lqh.Demo.Service.RootService;
import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Map;
/**
* author:小刘
* 日期: 2019/12/31 11:58
*/
"/RootServlet") (value =
public class RootServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = request.getParameter("method");
RootService rootService = new RootService();
String rootid = request.getParameter("id");
String rootname = request.getParameter("root");
String password = request.getParameter("password");
String Rootname = request.getParameter("Root");
//查询超级管理员
if ("findRoot".equals(method)) {
try {
boolean result = rootService.findRoot(rootid, Rootname, password);
if (result) {
request.setAttribute("MSG", "登录成功");
request.getSession().setAttribute("rootboss", rootid);
request.getRequestDispatcher("Register.jsp").forward(request, response);
} else {
request.setAttribute("MSG", "请输入正确的账户信息或者联系管理员");
request.getRequestDispatcher("RootLogin.jsp").forward(request, response);
}
} catch (SQLException e) {
request.setAttribute("MSG", "异常,请重试");
request.getRequestDispatcher("RootLogin.jsp").forward(request, response);
}
}
//添加管理员
if ("addroot".equals(method)) {
Map<String, String[]> map = request.getParameterMap();
Root root = new Root();
try {
BeanUtils.populate(root, map);
boolean result = rootService.addroot(root);
if (result) {
request.setAttribute("MSG", "添加root成功");
request.getRequestDispatcher("Register.jsp").forward(request, response);
}
} catch (IllegalAccessException e) {
request.setAttribute("MSG", "添加root失败");
request.getRequestDispatcher("Register.jsp").forward(request, response);
} catch (InvocationTargetException e) {
request.setAttribute("MSG", "添加root失败");
request.getRequestDispatcher("Register.jsp").forward(request, response);
} catch (SQLException e) {
request.setAttribute("MSG", "添加root失败");
request.getRequestDispatcher("Register.jsp").forward(request, response);
}
}
//查询管理员
if ("findroot".equals(method)) {
try {
boolean result = rootService.findroot(rootid, rootname, password);
if (result) {
//找到管理员信息之后要匹配验证码是否正确
String jspcode = request.getParameter("code");
String Servletcode = (String) request.getSession().getAttribute("code");
if (Servletcode.equalsIgnoreCase(jspcode)) {
//获取是否记住用户名和密码
String remember = request.getParameter("remember");
Cookie rotid = new Cookie("cookieid", rootid);
Cookie rotname = new Cookie("cookierootname", rootname);
Cookie rotpassword = new Cookie("cookiepassword", password);
if (remember != null) {
rotid.setMaxAge(24 * 60 * 60);
rotname.setMaxAge(24 * 60 * 60);
rotpassword.setMaxAge(24 * 60 * 60);
} else {
rotid.setMaxAge(0);
rotname.setMaxAge(0);
rotpassword.setMaxAge(0);
}
response.addCookie(rotid);
response.addCookie(rotname);
response.addCookie(rotpassword);
request.getSession().setAttribute("remember", remember);
//-------------------------------------
request.setAttribute("MSG", "登录成功");
request.getSession().setAttribute("root", rootid);
//默认每页展示5条数据,当前页为1
String curSize = "5";
String curPage = "1";
response.sendRedirect("UserServlet?method=findPage&curSize=" + curSize + "&curPage=" + curPage);
} else { request.setAttribute("msg", "请输入正确的验证码"); request.getRequestDispatcher("index.jsp").forward(request, response);
}
} else {
request.setAttribute("MSG", "请输入正确的账户信息或者联系管理员");
request.getRequestDispatcher("index.jsp").forward(request, response);
}
} catch (SQLException e) {
request.setAttribute("MSG", "请输入正确的账户信息或者联系管理员");
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
}
}UserServlet(展示页面的数据处理)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143package com.lqh.Demo.web.Servlet;
import com.lqh.Demo.Domain.Page;
import com.lqh.Demo.Domain.User;
import com.lqh.Demo.Service.UserService;
import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Map;
import java.util.Random;
/**
* author:小刘
* 日期: 2019/12/30 10:16
*/
"/UserServlet") (value =
public class UserServlet extends HttpServlet {
private UserService userService = new UserService();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map<String, String[]> map = request.getParameterMap();
User user = new User();
String method = request.getParameter("method");
//查
if ("findPage".equals(method)) {
//调用业务层findPage
String regex = "^[0-9]*$";
String curPage1 = request.getParameter("curPage");
String curSize1 = request.getParameter("curSize");
if (curSize1.trim().length() == 0 || "0".equals(curSize1)) {
curSize1 = "5";
}
if (curPage1.trim().length() == 0 || "0".equals(curPage1)) {
curPage1 = "1";
}
if (!curPage1.matches(regex) || !curSize1.matches(regex)) {
request.setAttribute("MSG1", "兄嘚异常辣,到底了或请输入正整数");
request.getRequestDispatcher("UserManage.jsp").forward(request, response);
}
int curPage = Integer.parseInt(curPage1);
int curSize = Integer.parseInt(curSize1);
try {
Page pageBean = userService.PageBean(curPage, curSize);
if (curPage > pageBean.getSumPage()) {
int sumPage = pageBean.getSumPage().intValue();
pageBean = userService.PageBean(sumPage, curSize);
}
request.getSession().setAttribute("PageBean", pageBean);
response.sendRedirect("UserManage.jsp");
} catch (SQLException e) {
request.setAttribute("MSG", "数据库异常,请重试");
}
}
//改
if ("updateUser".equals(method)) {
//调用业务层update
String id = request.getParameter("updateid");
try {
BeanUtils.populate(user, map);
} catch (IllegalAccessException e) {
request.setAttribute("MSG", "修改失败 ,系统异常,请重新修改 !");
request.getRequestDispatcher("UserManage.jsp").forward(request, response);
} catch (InvocationTargetException e) {
request.setAttribute("MSG", "修改失败 ,系统异常,请重新修改 !");
request.getRequestDispatcher("UserManage.jsp").forward(request, response);
}
try {
updateUser(id, user, request, response);
} catch (SQLException e) {
request.setAttribute("MSG", "修改失败 ,数据库异常,请重新添加 !");
request.getRequestDispatcher("UserManage.jsp").forward(request, response);
}
}
//增
if ("addUser".equals(method)) {
//调用业务层addUser
try {
BeanUtils.populate(user, map);
} catch (IllegalAccessException e) {
request.setAttribute("MSG", "添加失败 ,系统异常,请重新添加 !");
request.getRequestDispatcher("addUser.jsp").forward(request, response);
} catch (InvocationTargetException e) {
request.setAttribute("MSG", "添加失败 ,系统异常,请重新添加 !");
request.getRequestDispatcher("addUser.jsp").forward(request, response);
}
try {
addUser(user, request, response);
} catch (SQLException e) {
request.setAttribute("MSG", "添加失败 ,数据库异常,请重新添加 !");
request.getRequestDispatcher("addUser.jsp").forward(request, response);
}
}
//删
if ("deleteUser".equals(method)) {
//调用业务层deleteUser
String id = request.getParameter("id");
try {
deleteUser(id, request, response);
} catch (SQLException e) {
request.setAttribute("MSG", "删除失败 ,数据库异常,请重新添加 !");
request.getRequestDispatcher("UserManage.jsp").forward(request, response);
}
}
//测试加100个人
if ("test".equals(method)) {
int i = new Random().nextInt(6);
int age = new Random().nextInt(92) + 18;
int i1 = new Random().nextInt(2);
for (int j = 0; j < 50; j++) {
User user1 = new User();
String[] address = {"北京", "上海", "广东", "福建", "南京", "汕头"};
String[] name = {"家政3", "家政1", "家政2", "家政5", "家政56", "家政58"};
String[] sex = {"男", "女"};
user1.setAddress(address[i]);
user1.setAge(age);
user1.setName(name[i]);
user1.setEmail("123456789@qq.com");
user1.setQq("123456789");
user1.setSex(sex[i1]);
try {
userService.addUser(user1);
} catch (SQLException e) {
e.printStackTrace();
}
}
request.setAttribute("MSG", "添加成功 !");
request.getRequestDispatcher("UserManage.jsp").forward(request, response);
}
}
//-------------------------------------这是方法区--------------------------------------
public void addUser(User user, HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {
boolean User = userService.addUser(user);
//判断有没有这个人
if (User) {
request.setAttribute("MSG", "添加成功 !");
request.getRequestDispatcher("addUser.jsp").forward(request, response);
} else {
request.setAttribute("MSG", "添加失败 ,请重新添加 !");
request.getRequestDispatcher("addUser.jsp").forward(request, response);
}
}
private void updateUser(String id, User user, HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {
boolean result = userService.updateUser(id, user);
if (result) {
request.setAttribute("MSG", "修改成功 !");
request.getRequestDispatcher("UserManage.jsp").forward(request, response);
} else {
request.setAttribute("MSG", "修改失败 ,请重新修改 !");
request.getRequestDispatcher("UserManage.jsp").forward(request, response);
}
}
private void deleteUser(String id, HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {
boolean result = userService.deleteUser(id);
if (result) {
request.setAttribute("MSG", "删除成功 !");
request.getRequestDispatcher("UserManage.jsp").forward(request, response);
} else {
request.setAttribute("MSG", "删除失败 ,请重新删除 !");
request.getRequestDispatcher("UserManage.jsp").forward(request, response);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
- CodeServlet(用于验证码生产)
```java
package com.lqh.Demo.web.Servlet;
import cn.dsna.util.images.ValidateCode;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* author:小刘
* 日期: 2019/12/26 13:00
*/
@WebServlet(value = "/Code")
public class CodeServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//生成验证码
ValidateCode code = new ValidateCode(100, 30, 4, 8);
String code1 = code.getCode();
//将验证码信息共享RootServlet
request.getSession().setAttribute("code", code1);
ServletOutputStream ous = response.getOutputStream();
code.write(ous);
}
}
Service层
- RootService
1 | package com.lqh.Demo.Service; |
- UserService
1 | package com.lqh.Demo.Service; |
Dao层
RootDao
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35package com.lqh.Demo.Dao;
import com.lqh.Demo.Domain.Root;
import com.lqh.Demo.Utils.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import java.sql.SQLException;
/**
* author:小刘
* 日期: 2019/12/31 11:51
*/
public class RootDao {
private QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
//查
//查找超级管理员的所有信息
public Root findRoot(String rootid) throws SQLException {
String sql = "select * from rootboss where id=?";
return queryRunner.query(sql, new BeanHandler<Root>(Root.class), rootid);
}
//增加管理员
public int addroot(Root root) throws SQLException {
String sql = "insert into root values(?,?,?)";
return queryRunner.update(sql, root.getId(), root.getRoot(), root.getPassword());
}
//查询管理员信息
public Root findroot(String rootid) throws SQLException {
String sql = "select * from root where id=?";
return queryRunner.query(sql, new BeanHandler<Root>(Root.class), rootid);
}
}UserDao
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53package com.lqh.Demo.Dao;
import com.lqh.Demo.Domain.User;
import com.lqh.Demo.Utils.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
/**
* author:小刘
* 日期: 2019/12/30 9:54
*/
public class UserDao {
private QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
//查
//查找当前页面的所有信息
public List<User> findPage(int a, int b) throws SQLException {
String sql = "select * from User limit ?, ?";
return queryRunner.query(sql, new BeanListHandler<User>(User.class), a, b);
}
//查找数据库所有信息的数量
public Long findCount() throws SQLException {
String sql = "select count(*) from User ";
return (Long) queryRunner.query(sql, new ScalarHandler());
}
//---------------------------------------------------------------------------
//增
public int addUser(User user) throws SQLException {
String sql = "insert into User values(null,?,?,?,?,?,?)";
return queryRunner.update(sql, user.getName(), user.getSex(), user.getAge(), user.getAddress(), user.getQq(), user.getEmail());
}
//改
public int updateUser(String id, User user) throws SQLException {
String sql = "update User set name=? ,sex=?,age=?,address=?,qq=?,email=? where id=? ";
return queryRunner.update(sql, user.getName(), user.getSex(), user.getAge(), user.getAddress(), user.getQq(), user.getEmail(), id);
}
//删
public int deleteUser(String id) throws SQLException {
String sql = "delete from User where id =?";
return queryRunner.update(sql, id);
}
}
管理员登录jsp
1 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> |
超级管理员注册登录jsp
1 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> |
超级管理员注册管理员jsp
1 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> |
展示数据库信息(其中有分页效果和删除页面)jsp
- 分页是将用户点击想要跳转的页码和每页想要展示多少数据,传给后端 , 通过与后端查询的总页码(总数量/每页展示的数量)结合得出 , 按钮的数量和当前页的高亮 , 对于页数的导航的图片则是用了表格来进行排布
1 | <%@ page import="com.lqh.Demo.Domain.Page" %> |
修改用户信息(默认将修改用户信息带到该页面)
为了方便,下面代码直接走jsp,不走Servlet
1 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> |
新增用户jsp
1 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> |
总结
通过本次打码 加深了mysql , 三层架构 , jsp 的理解 , 同时掌握了
1 数据库增删改查操作和分页操作
2 三层架构的基本逻辑
3 浏览器会话技术数据和请求时,后台数据的处理
期间对于展示页面的分页效果 , 逻辑借鉴了百度页面 ,然后按照自己逻辑进行实现 , 好玩又有趣;