功能要求
1.分页
- 1.1 当前页 – currentPage – 前端获取
- 1.2 每页展示的数据 – pageSize – 后端制定
- 1.3 总条数 – totalCount – 数据库查询获取
- 1.4 总页数 – pageCount
– 总条数%每页展示的数据 == 0?总条数/每页展示的数据:总条数/每页展示的数据+1- 1.5 每页展示的数据 – list – 数据库查询获取 需要通过偏移量和每页
展示的数据来完成查询2.删除
- 2.1 点击对用用户的删除按钮是,删除对应的用户。
1、在数据库建立person数据表,添加数据
/*
Navicat MySQL Data Transfer
Source Server : java
Source Server Version : 50554
Source Host : localhost:3306
Source Database : wzy
Target Server Type : MYSQL
Target Server Version : 50554
File Encoding : 65001
Date: 2021-01-08 20:32:15
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`sex` varchar(255) DEFAULT NULL,
`state` varchar(255) DEFAULT NULL,
`hobbies` varchar(255) DEFAULT NULL,
`addrs` varchar(255) DEFAULT NULL,
`is_delete` int(1) DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', '圣采儿', '123', '女', '九阶五级侠者', '刺客', '圣殿联盟刺客圣殿', '1');
INSERT INTO `user` VALUES ('3', '萧炎', '123456', '男', '斗帝', '斗者、炼药师', '斗气大陆加玛帝国', '1');
INSERT INTO `user` VALUES ('4', '萧薰儿', '123', '女', '斗帝', '斗者', '斗气大陆中州', '1');
INSERT INTO `user` VALUES ('5', '罗峰', '123456', '男', '虚空真神', '战士、精神念师', '银河圣地宇宙', '1');
INSERT INTO `user` VALUES ('6', '徐欣', '123', '女', '封侯不朽', '战士', '银河圣地宇宙', '1');
INSERT INTO `user` VALUES ('7', '坐山客', '123', '男', '神王', '战士、炼器师', '起源大陆晋之神国', '1');
INSERT INTO `user` VALUES ('8', '唐三', '123456', '男', '神王境', '魂师、炼药师', '斗罗大陆天斗帝国', '1');
INSERT INTO `user` VALUES ('9', '小舞', '123456', '女', '一级神祗', '魂师', '斗罗大陆星斗大森林', '1');
INSERT INTO `user` VALUES ('10', '龙皓晨', '123456', '男', '九阶九级神印骑士', '守护骑士、惩戒骑士', '圣殿联盟骑士圣殿', '1');
INSERT INTO `user` VALUES ('12', '药尘', '123', '男', '一星斗圣', '斗者、炼药师', '斗气大陆星陨阁', '1');
INSERT INTO `user` VALUES ('13', '林动', '123456', '男', '祖境', '武者、符师', '天玄大陆大炎王朝', '1');
INSERT INTO `user` VALUES ('14', '应欢欢', '123456', '女', '半步祖境', '武者', '天玄大陆道宗', '1');
INSERT INTO `user` VALUES ('15', '绫清竹', '123456', '女', '半步祖境', '武者', '天玄大陆九天太清宫', '1');
INSERT INTO `user` VALUES ('16', '彩鳞', '123', '女', '斗帝', '斗者', '斗气大陆塔戈尔大沙漠', '1');
INSERT INTO `user` VALUES ('17', '霍雨浩 情绪之神', '123456', '男', '神王境', '魂师、魂导师', '斗罗大陆星斗帝国', '1');
INSERT INTO `user` VALUES ('18', '融念冰', '123456', '男', '神王境', '魔法师', '仰光大陆华融帝国', '1');
INSERT INTO `user` VALUES ('19', '姬动 邪恶之神', '123456', '男', '神王境', '阴阳魔师', '五行大陆', '1');
INSERT INTO `user` VALUES ('20', '烈焰 善良之神', '123', '女', '神王境', '阴阳魔师', '五行大陆地心世界', '1');
INSERT INTO `user` VALUES ('21', '周维清 破坏神', '123456', '男', '神师境', '天珠师、凝形师', '浩渺大陆天弓帝国', '1');
INSERT INTO `user` VALUES ('22', '长弓·威 光明神王', '123', '男', '大魔导师、光之子', '魔法师', '天舞大陆艾夏王国', '1');
INSERT INTO `user` VALUES ('23', '海龙 仙帝', '123456', '男', '九阶高级', '修真者', '神州连云仙宗', '1');
INSERT INTO `user` VALUES ('24', '阿呆 死神', '123', '男', '神王', '魔法师、战士', '天元大陆天金帝国', '1');
INSERT INTO `user` VALUES ('25', '雷翔 狂神', '123', '男', '神王', '魔法师、武士', '晋元异大陆', '1');
INSERT INTO `user` VALUES ('26', '天痕 天帝', '123456', '男', '魔神', '异能者', '地球', '1');
INSERT INTO `user` VALUES ('27', '叶音竹 琴帝', '123', '男', '神王', '魔法师。武者', '阿卡迪亚王国', '1');
INSERT INTO `user` VALUES ('28', '唐舞麟 龙王斗罗', '123', '男', '120级超神级', '魂师、斗铠师、机甲师、锻造师', '斗罗大陆', '1');
- 新建web项目,配置tomcat服务器
- 在项目的WEB-INF目录下新建 lib 文件。
- 将 mysql 驱动复制到项目的 lib 文件夹中。
- 选中 lib 文件夹右键 Add as Libraay,点击 OK。
表示层:
- 命名:XXXView
- 职责:收集用户的数据和需求、展示数据。
业务逻辑层:
- 命名:XXXServiceImpl
- 职责:数据加工处理、调用DAO完成业务实现、控制事务。
数据访问层:
- 命名:XXXDaoImpl
- 职责:向业务层提供数据,将业务层加工后的数据同步到数据库。
三层架构核心流程 |
---|
- utils 存放工具类(DBUtils)
- entity 存放实体类(Person)
- dao 存放 DAO 接口(PersonDao)
- impl 存放 DAO 接口实现类(PersonDaoImpl)
- service 存放 service 接口(PersonService)
- impl 存放 service 接口实现类(PersonServiceImpl)
- view 存放程序启动类(main)
index.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 0008 2021-01-08
Time: 11:32
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户中心</title>
</head>
<style>
a{
color: blue;
}
a:hover{
color: red;
}
</style>
<body>
<a href="UserServlet?action=getList¤tPage=1">展示用户信息</a>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 0008 2021-01-08
Time: 14:20
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>展示用户数据</title>
</head>
<style>
a{
color: blue;
}
a:hover{
color: red;
}
</style>
<body>
<p><a href="index.jsp">用户首页</a></p>
<table border="1" cellspacing="0">
<tr style="text-align: center">
<th> <input type="checkbox">全选 </th>
<th>ID</th>
<th>姓名</th>
<th>性别</th>
<th>境界</th>
<th>职业</th>
<th>所属势力</th>
<th>操作</th>
</tr>
<c:forEach var="user" items="${list}">
<tr style="text-align: center">
<td> <input type="checkbox"> </td>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.sex}</td>
<td>${user.hobbies}</td>
<td>${user.addrs}</td>
<td>${user.state}</td>
<td>
<%--
href属性值指向中不能有/,不然无法访问
--%>
<a href="UserServlet?action=update&id=${user.id}¤tPage=${currentPage}">编辑</a>
<a href="UserServlet?action=delete&id=${user.id}¤tPage=${currentPage}">删除</a>
</td>
</tr>
</c:forEach>
<tr style="text-align: center">
<td colspan="7">
<a href="#">删除选中</a>
<a href="#">添加</a>
</td>
</tr>
</table>
<a href="UserServlet?action=getList¤tPage=1">首页</a>
<c:if test="${currentPage > 1}">
<a href="UserServlet?action=getList¤tPage=${currentPage-1}">上一页</a>
</c:if>
<c:if test="${currentPage < pageCount}">
<a href="UserServlet?action=getList¤tPage=${currentPage+1}">下一页</a>
</c:if>
<a href="UserServlet?action=getList¤tPage=${pageCount}">尾页</a>
</body>
</html>
IUser.java
package com.qf.wzy.dao;
import com.qf.wzy.entity.User;
import java.util.List;
/**
* @Author wzy
* @Date 0008 2021-01-08 14:46
* @Version 1.0
*/
public interface IUserDao {
//查询数据总数
Integer getTotalCount();
//获取每一个展示的数据
List<User> getList(int offset, int pageSize);
//删除数据
Integer delete_Data(int id);
}
UserDaoImpl
package com.qf.wzy.dao.impl;
import com.qf.wzy.dao.IUserDao;
import com.qf.wzy.entity.User;
import com.qf.wzy.utils.DButils;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* @Author wzy
* @Date 0008 2021-01-08 14:45
* @Version 1.0
*/
public class UserDaoImpl implements IUserDao {
@Override
public Integer delete_Data(int id){
Connection connection = null;
PreparedStatement ps = null;
int i=0;
try {
connection = DButils.getConnection();
ps = connection.prepareStatement("delete from user where id=?");
ps.setInt(1,id);
i = ps.executeUpdate();
if (i!= 0){
System.out.println("删除成功");
}
} catch (SQLException e) {
e.printStackTrace();
}
DButils.closeAll(ps,connection);
return i;
}
@Override
public Integer getTotalCount() {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
int totalCount = 0;
try {
connection = DButils.getConnection();
statement = connection.createStatement();
ResultSet rset = statement.executeQuery("select count(*) from user where is_delete=1");
if (rset.next()){
totalCount = rset.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
finally {
DButils.closeAll(resultSet,statement,connection);
}
return totalCount;
}
@Override
public List<User> getList(int offset, int pageSize) {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
ResultSetMetaData data=null;
PreparedStatement ps=null;
User user=null;
List<User> list = new ArrayList<>();
try {
connection = DButils.getConnection();
ps = connection.prepareStatement("select * from user where is_delete=1 order by id limit ?,?");
ps.setInt(1,offset);
ps.setInt(2,pageSize);
resultSet = ps.executeQuery();
while (resultSet.next()){
int id = resultSet.getInt("id");
String username = resultSet.getString("username");
String password = resultSet.getString("password");
String sex = resultSet.getString("sex");
String state = resultSet.getString("state");
String hobbies = resultSet.getString("hobbies");
String addrs = resultSet.getString("addrs");
user = new User(id,username,password,sex,state,hobbies,addrs);
list.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
}
finally {
DButils.closeAll(connection,resultSet,ps);
}
return list;
}
}
UserServlet.java
package com.qf.wzy.servlet;
import com.qf.wzy.dao.IUserDao;
import com.qf.wzy.dao.impl.UserDaoImpl;
import com.qf.wzy.entity.User;
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.util.List;
/**
* @Author wzy
* @Date 0008 2021-01-08 14:04
* @Version 1.0
*/
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
public IUserDao userDao = new UserDaoImpl();
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String action = request.getParameter("action");
switch (action){
case "getList":
toGetList(request,response);
break;
case "update":
toUpdate(request,response);
break;
case "delete":
toDelete(request,response);
break;
default:
System.out.println("action有误");
break;
}
}
private void toDelete(HttpServletRequest request, HttpServletResponse response) {
System.out.println("toDelete");
int id = Integer.parseInt(request.getParameter("id"));
System.out.println("id--------------------------------------"+id);
int i = userDao.delete_Data(id);
if (i!=0){
try {
//调用获取数据方法,展示删除后的数据
toGetList(request,response);
// request.getRequestDispatcher("userList.jsp").forward(request,response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void toUpdate(HttpServletRequest request, HttpServletResponse response) {
}
private void toGetList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取前端发送的数据 -- 当前页
String currentPage = request.getParameter("currentPage");
//2.指定每页展示5条数据
int pageSize=9;
//3.查询数据库获取总行数
int totalCount = userDao.getTotalCount();
//4.获取总页数
int pageCount = totalCount%pageSize==0?totalCount/pageSize:(totalCount/pageSize+1);
//5.获取展示的数据
List<User> list = userDao.getList(pageSize*(Integer.parseInt(currentPage)-1),pageSize);
request.setAttribute("list",list);
request.setAttribute("currentPage",currentPage);
request.setAttribute("pageCount",pageCount);
request.getRequestDispatcher("userList.jsp").forward(request,response);
}
}
DButils.java
package com.qf.wzy.utils;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
/**
* @Author wzy
* @Date 0007 2021-01-07 17:18
* @Version 1.0
*/
public class DButils {
private static String className=null;
private static String url=null;
private static String username=null;
private static String password=null;
static {
Properties properties = new Properties();
try {
properties.load(DButils.class.getClassLoader().getResourceAsStream("jdbc.properties"));
className = properties.getProperty("className");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
} catch (IOException e) {
e.printStackTrace();
}
}
//封装连接对象的方法
public static Connection getConnection(){
//定义连接对象
Connection connection=null;
//通过DriverManger获取连接对象
try {
//加载驱动
Class.forName(className);
//调用DriverManager的getConnection获取连接对象
connection = DriverManager.getConnection(url,username,password);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//返回连接对象
return connection;
}
public static void closeAll(AutoCloseable ... closeables){
for (AutoCloseable closeable:closeables) {
if (closeable!=null){
try {
closeable.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
jdbc.properties
className=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/wzy?characterEncoding=utf8
username=root
password=root