用户管理实现
对于用户管理这部分,要养成思路,显示的这些数据可能并不只是在 MySQL 的一张表里面查,可能是多张表,也可能是多个数据库里面查,去 oracle 里面查,也可能是多台电脑分布式去查
发起请求, Sevlet 处理请求,并调用业务,去查询数据库的角色列表,用户列表,同时处理分页,需要设定每页大小 pagesize,以及所有数据的总数。pagesize 是固定的,数据总数是数据库里面查的,不固定。
用户列表先到 service 层查询,service 层调 Dao 层,其他同,Dao 层操作数据库。 本项目只操作一个数据库,但是大项目很多需要同时操作多个数据库
1. 导入分页工具类
对于分页的实现,先将 PageSupport 导入到 utils 中
package com.uestc.utils;
public class PageSupport {
//当前页码-来自于用户输入
private int currentPageNo = 1;
//总数量(表)
private int totalCount = 0;
//页面容量
private int pageSize = 0;
//总页数-totalCount/pageSize(+1)
private int totalPageCount = 1;
public int getCurrentPageNo() {
return currentPageNo;
}
public void setCurrentPageNo(int currentPageNo) {
if(currentPageNo > 0){ // 页面分页设置不能小于等于 0
this.currentPageNo = currentPageNo;
}
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
if(totalCount > 0){ // 同样总数也要 > 0
this.totalCount = totalCount;
//设置总页数
this.setTotalPageCountByRs();
}
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
if(pageSize > 0){
this.pageSize = pageSize;
}
}
public int getTotalPageCount() {
return totalPageCount;
}
public void setTotalPageCount(int totalPageCount) {
this.totalPageCount = totalPageCount;
}
public void setTotalPageCountByRs(){
if(this.totalCount % this.pageSize == 0){
this.totalPageCount = this.totalCount / this.pageSize;
}else if(this.totalCount % this.pageSize > 0){
this.totalPageCount = this.totalCount / this.pageSize + 1;
}else{
this.totalPageCount = 0;
}
}
}
接下来看前端用户管理的实现 userlist.jsp
1. 获取用户数量
撰写代码顺序同样:1.UserDao 2. UserDaolmpl 3.UserService 4.UserServicelmpl
UserDao
// 查询用户数量
int getUserCount(Connection connection, String userName, int userRole) throws SQLException;
可以在 IDEA 中直接执行 SQL 语句
UserDaoImpl
@Override
public int getUserCount(Connection connection, String userName, int userRole) throws SQLException {
PreparedStatement pstm = null; // 预编译
ResultSet resultSet = null; // 结果集
int count = 0;
if (connection != null) {
StringBuffer sql = new StringBuffer(); //
sql.append("select count(1) as count from smbms_user u,smbms_role r where u.userRole=r.id");
//定义一个列表,用于存放我们的参数(万能解决方法)
ArrayList<Object> list = new ArrayList<Object>();
if (!StringUtils.isNullOrEmpty(userName)) { // 如果当前搜索的用户名不为空
sql.append(" and u.userName like ?"); // 拼接一个 and 语句
list.add("%" + userName + "%"); // % 是用户模糊查询的,即用户只需要输入查询中的一部分就可以出结果,比如:%李%
}
if (userRole > 0) {
sql.append(" and u.userRole = ?");
list.add(userRole);
}
//把 list 转换成数组
Object[] params = list.toArray();
// 输出sql语句,方便调试
//System.out.println("UserDaoImpl--->getUserCount():" + sql.toString());
try {
resultSet = BaseDao.execute(connection, pstm, resultSet, sql.toString(), params);
} catch (Exception e) {
throw new RuntimeException(e);
}
if (resultSet.next()) {
count = resultSet.getInt("count"); // 从结果集中获取最终的数量
}
BaseDao.closeResource(null, pstm, resultSet); // connection 在这个地方没有实例话,在业务层关闭
}
return count;
}
UserService
// 查询用户记录数
int getUserCount(String userName, int userRole);
UserServiceImpl,其中 count 要返回给前端,servelt 调用下面方法即可拿到数据
//查询用户记录数
@Override
public int getUserCount(String userName, int userRole) {
Connection connection = null;
int count = 0;
try {
connection = BaseDao.getConnection(); // 创建连接
count = userDao.getUserCount(connection, userName, userRole); // 执行操作
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
BaseDao.closeResource(connection, null, null);
}
return count;
}
先执行 Jnit 测试单元,测试一下上面的过程
2. 获取用户列表(过程同上)
(1)编写UserDao
// 获取用户列表的接口
List<User> getUserList(Connection connection, String userName, int userRole, int currentPageNo, int pageSize) throws Exception;
(2)UserDao接口实现类UserDaoImpl
// 获取用户列表的接口
@Override
public List<User> getUserList(Connection connection, String userName, int userRole, int currentPageNo, int pageSize) throws Exception {
PreparedStatement pstm = null;
ResultSet rs = null;
List<User> userList = new ArrayList<User>();
if (connection != null) {
StringBuffer sql = new StringBuffer();
sql.append("select u.*,r.roleName as userRoleName from smbms_user u,smbms_role r where u.userRole = r.id ");
List<Object> list = new ArrayList<Object>();
if (!StringUtils.isNullOrEmpty(userName)) {
sql.append(" and u.userName like ?");
list.add("%" + userName + "%");
}
if (userRole > 0) {
sql.append(" and u.userRole =?");
list.add(userRole);
}
//在数据库中,分页使用 limit startIndex,pagesize;总数
//当前页(当前页-1)*页面大小
//0, 5 1 0 01234
//5, 5 2 5 26789
//10, 5 3 10
sql.append(" order by creationDate DESC limit ?,?");
currentPageNo = (currentPageNo - 1) * pageSize;
list.add(currentPageNo);
list.add(pageSize);
Object[] params = list.toArray();
//System.out.println("sql---->" + sql.toString());
rs = BaseDao.execute(connection, pstm, rs, sql.toString(), params);
while (rs.next()) {
User _user = new User();
_user.setId(rs.getInt("id"));
_user.setUserCode(rs.getString("userCode"));
_user.setUserName(rs.getString("userName"));
_user.setGender(rs.getInt("gender"));
_user.setBirthday(rs.getDate("birthday"));
_user.setPhone(rs.getString("phone"));
_user.setUserRole(rs.getInt("userRole"));
_user.setUserRoleName(rs.getString("userRoleName"));
userList.add(_user);
}
BaseDao.closeResource(null, pstm, rs);
}
return userList;
}
(3)编写业务层接口
在service的user包中的UserService接口中添加如下代码
// 根据条件查询用户列表
List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize);
(4)编写业务层接口实现类
在service的user包中的UserServiceImpl类中添加以下代码
// 据条件查询用户列表
@Override
public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
Connection connection = null;
List<User> userList = null;
try {
connection = BaseDao.getConnection();
userList = userDao.getUserList(connection, queryUserName, queryUserRole, currentPageNo, pageSize);
} catch (Exception throwables) {
throwables.printStackTrace();
} finally {
BaseDao.closeResource(connection, null, null);
}
return userList;
}
3. 获取角色操作
为了职责统一,可以把角色的操作单独放在一个包中,和POJO类对应
(1)编写接口RoleDao
public interface RoleDao {
// 获取角色列表
public List<Role> getRoleList(Connection connection)throws Exception;
}
(2)RoleDao接口实现类RoleDaoImpl
向实现类中添加以下代码
package com.uestc.dao.role;
import com.uestc.dao.BaseDao;
import com.uestc.pojo.Role;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class RoleDaoImpl implements RoleDao{
// 获取角色列表
@Override
public List<Role> getRoleList(Connection connection) throws Exception {
PreparedStatement pstm = null;
ResultSet rs = null;
List<Role> roleList = new ArrayList<Role>();
if(connection != null){
String sql = "select * from smbms_role";
Object[] params = {};
rs = BaseDao.execute(connection, pstm, rs, sql, params);
while(rs.next()){
Role _role = new Role();
// id, roleCode, roleName 都来源与表中的属性
_role.setId(rs.getInt("id"));
_role.setRoleCode(rs.getString("roleCode"));
_role.setRoleName(rs.getString("roleName"));
roleList.add(_role);
}
BaseDao.closeResource(null, pstm, rs);
}
return roleList;
}
}
(3)编写业务层接口
在service建立role包,然后建立RoleService接口,添加如下代码
package com.uestc.servlet.role;
import com.uestc.pojo.Role;
import java.util.List;
// 用户角色接口
public interface RoleService {
// 获取角色列表的抽象方法
List<Role> getRoleList();
}
(4)编写业务层接口实现类
在service的role包中的RoleServiceImpl类中添加以下代码
package com.uestc.servlet.role;
import com.uestc.dao.BaseDao;
import com.uestc.dao.role.RoleDao;
import com.uestc.dao.role.RoleDaoImpl;
import com.uestc.pojo.Role;
import org.junit.Test;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
public class RoleServiceImpl implements RoleService {
//获得一个RoleDao对象
private RoleDao roleDao;
public RoleServiceImpl() {
this.roleDao = new RoleDaoImpl();
}
// 获取角色列表的抽象方法
@Override
public List<Role> getRoleList() {
List<Role> roleList = null;
Connection connection = null;
try {
connection = BaseDao.getConnection();
roleList = roleDao.getRoleList(connection);
} catch (SQLException throwables) {
throwables.printStackTrace();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
BaseDao.closeResource(connection, null, null);
}
return roleList;
}
@Test // 对上面代码进行测试
public void test() {
RoleServiceImpl roleService = new RoleServiceImpl();
List<Role> roleList = roleService.getRoleList();
for (Role role : roleList) {
System.out.println(role.getRoleName());
}
}
}
以上操作就写完了下图中的三条线,现需要写请求 Servlet 了
4. 编写Servlet类
对Servlet包中的user包中UserServlet类进行如下添加
head.jsp 中
/jsp/user.do?method=query"
这句话表示,走的 /jsp/user.do 的请求,并且 method=query,/jsp/user.do 的请求在上面我们已经通过 UserServlet 处理请求,因此直接在 UserServlet 类中添加对于方法
UserServlet
注意在 userlist.jsp 中,同样是 /jsp/user.do 的请求 并且 method=query,因此在进行用户管理操作时,同样会执行 userlist.jsp 的内容
对Servlet包中的user包中UserServlet类进行如下添加
/**
* @description: 对用户进行查询【重点和难点】
* 1.获取用户前端的数据(查询)
* 2.判断请求是否需要执行,看参数的值判断
* 3.为了实现分页,需要计算出当前页面和总页面,页面大小…
* 4.用户列表展示
* 5.返回前端
*/
private void userQuery(HttpServletRequest req, HttpServletResponse resp) {
//查询用户列表
String queryUserName = req.getParameter("queryname");
String temp = req.getParameter("queryUserRole");
String pageIndex = req.getParameter("pageIndex");
int queryUserRole = 0;
// ------------ 获取用户列表 -----------------------------
UserServiceImpl userService = new UserServiceImpl();
//第一次走这个请求,一定是第一页,页面大小固定的
//可用把pageSize写道配置文件,方便后期修改
int pageSize = 5;
int currentPageNo = 1;
if (queryUserName == null) {
queryUserName = "";
}
if (temp != null && !temp.equals("")) {
//给查询赋值 0,1,2,3,
queryUserRole = Integer.parseInt(temp);
}
if (pageIndex != null) {
currentPageNo = Integer.parseInt(pageIndex);
}
// ---------------------- 获取用户数量 ------------------------------
int totalCount = userService.getUserCount(queryUserName, queryUserRole);
List<User> userList = null;
//总页数支持
PageSupport pageSupport = new PageSupport();
pageSupport.setCurrentPageNo(currentPageNo);
pageSupport.setPageSize(pageSize);
pageSupport.setTotalCount(totalCount);
//最大页数,总共有几页【如果totalCount刚好是pageSize的整数倍,那么是不是多算了一页】
int totalPageCount = ((int) (totalCount / pageSize)) + 1;
//控制首页和尾页
if (currentPageNo < 1) {
//如果页面小于第一页,就显示第一页
currentPageNo = 1;
} else if (currentPageNo > totalPageCount) {
//如果页面大于最后一页,就显示最后一页
currentPageNo = totalPageCount;
}
// --------------------- 获取用户列表展示 --------------------------
userList = userService.getUserList(queryUserName, queryUserRole, currentPageNo, pageSize);
req.setAttribute("userList", userList);
// ---------------------- 获取角色列表 ----------------------------
RoleServiceImpl roleService = new RoleServiceImpl();
List<Role> roleList = roleService.getRoleList();
req.setAttribute("roleList", roleList);
req.setAttribute("totalCount", totalCount);
req.setAttribute("currentPageNo", currentPageNo);
req.setAttribute("totalPageCount", totalPageCount);
req.setAttribute("queryUserName", queryUserName);
req.setAttribute("queryUserRole", queryUserRole);
//返回前端
try {
req.getRequestDispatcher("userlist.jsp").forward(req, resp);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
启动 tomcat,登录进入用户管理处
rollpage.jsp 专门用户处理分页
整理一下用户管理界面的思路:
- 从前端获取数据(查询)
- 判断请求是否需要执行,看参数的值判断
- 为了实现分页,需要计算处当前页面和总页面,页面大小。。。
- 用户列表展示
- 最后返回前端
上述所有控制只操作了两张表
供应商管理与订单管理页面
供应商管理与订单管理页面实际上就是查询对应的表
同上
如何部署发布呢?
里面得代码是字节码 .class 文件,字节码文件可以通过 IDEA 打开,同时也可以反编译为代码,但是可能反编译出来得代码与源码不同!
首先注意端口要和 IDEA 中得一致,如果 IDEA 中运行测试使用得 8080 得端口,就需要在 conf 得 server.xml 中修改成一致得端口号,并将编译产生得项目放入到 webapps 目录下,然后在 bin 目录下,执行 startup.bat,即可通过在 IDEA 中设置得入口请求,访问页面。