时隔多日,我们终于更新专栏啦!上一篇教程中我们完成了仪表盘页面的制作,这篇教程我们来实现用户管理界面。
首先,我们来明确一下用户管理页面的需求,也就是说这个页面里应该有哪些功能。
前端页面不复杂,使用了Bootstrap-table来展示数据,并且用户数据相对不多,我们选用了前端分页。
闲话不多说,我们一起来学习下前端代码(manage_user.jsp)
<%--
Created by IntelliJ IDEA.
User: yocco
Date: 2020/01/06
Time: 15:03
To change this template use File | Settings | File and Code Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path;
%>
用户管理页面
后端涉及到查询并返回用户数据、新增用户、修改用户、删除用户、重置密码这几个Controller和相关服务,我们来看一下代码。
1. 控制层Controller
/**
* 查询用户数据
*/
@RequestMapping("/getUserList")
@ResponseBody
public List getUserList(HttpServletRequest request) {
logger.info("/getUserList --> start");
User user = new User();
user.setUsername(request.getParameter("loginName"));
user.setCname(request.getParameter("cname"));
user.setMobile(request.getParameter("mobile"));
user.setStatus(request.getParameter("userStatus"));
List list = userService.getUserListPage(user);
logger.info("/getUserList --> end");
return list;
}
/**
* 新增用户
*/
@ResponseBody
@RequestMapping(value = "/add", method = RequestMethod.POST)
public ResultObj addUser(HttpServletRequest request, HttpServletResponse response) {
logger.info("/user/add --> start");
ResultObj result = new ResultObj();
try {
result = userService.addUser(request);
} catch (Exception e) {
logger.error("新增用户失败 - 系统错误请稍后再试." + e.getMessage());
result.setCode("3");
result.setMsg("新增用户失败 - 系统错误请稍后再试.");
}
logger.info("/user/add --> end");
return result;
}
/**
* 更新用户
*/
@ResponseBody
@RequestMapping(value = "/update", method = RequestMethod.POST)
public ResultObj updateUser(HttpServletRequest request, HttpServletResponse response) {
logger.info("/user/update --> start");
ResultObj result = new ResultObj();
try {
result = userService.updateUserByUsername(request);
} catch (Exception e) {
logger.error("/user/update - 修改用户失败, 系统错误请稍后再试, 异常信息: " + e.getMessage());
result.setCode("2");
result.setMsg("修改用户失败,系统错误请稍后再试");
}
logger.info("/user/update --> end");
return result;
}
/**
* 删除用户
*/
@ResponseBody
@RequestMapping(value = "/delete", method = RequestMethod.POST)
public ResultObj deleteUser(HttpServletRequest request) {
logger.info("/user/delete --> start");
ResultObj result = new ResultObj();
String deleteUsername = request.getParameter("delete_username");
try {
result = userService.deleteUserByUsername(deleteUsername);
} catch (Exception e) {
logger.error("删除用户失败 - 系统错误请稍后再试." + e.getMessage());
result.setCode("2");
result.setMsg("删除用户失败,系统错误请稍后再试");
}
logger.info("/user/delete --> end");
return result;
}
/**
* 重置密码
*/
@ResponseBody
@RequestMapping(value = "/resetUserPw", method = RequestMethod.POST)
public ResultObj resetUser(HttpServletRequest request) {
logger.info("/resetUserPw --> start");
ResultObj result = new ResultObj();
String resetUsername = request.getParameter("reset_username");
String pword = CipherUtil.generatePassword(OmsUtils.getDefaultPwd());
try {
result = userService.updatePwByUsername(resetUsername, pword);
} catch (Exception e) {
logger.error("重置用户登录密码失败 - 系统错误请稍后再试." + e.getMessage());
result.setCode("2");
result.setMsg("重置用户登录密码失败,系统错误请稍后再试");
}
logger.info("/resetUserPw --> end");
return result;
}
2. 服务层Service
/**
* 查询用户数据
*/
public List getUserListPage(User user) {
return this.userDao.getUserListPage(user);
}
/**
* 新增用户
*/
@Transactional
public ResultObj addUser(HttpServletRequest request) throws Exception {
User user = new User();
String addUsername = request.getParameter("update_username");
int userExisted = checkUserExisted(addUsername);
if (userExisted > 0) {
logger.error("新增用户失败 - 系统已存在该用户名,请修改用户名后再提交.");
return new ResultObj("1", "新增用户失败 - 系统已存在该用户名,请修改用户名后再提交.");
}
user.setUsername(addUsername);
user.setPassword(CipherUtil.generatePassword(OmsUtils.getDefaultPwd()));
user.setCname(request.getParameter("update_cname"));
user.setEmail(request.getParameter("update_email"));
user.setMobile(request.getParameter("update_mobile"));
user.setAddr(request.getParameter("update_addr"));
user.setValidUntilTime(formatValidUntilTime(request.getParameter("update_valid_until")));
user.setStatus(request.getParameter("update_status"));
user.setCreateTime(new Date());
int inserted = userDao.addUser(user);
logger.info("/user/add - username: " + addUsername + ", insert count: " + inserted);
ResultObj result = new ResultObj();
if (inserted >= 1) {
result.setCode("0");
result.setMsg("新增用户成功");
} else {
result.setCode("2");
result.setMsg("新增用户失败 - 请稍后再试.");
}
return result;
}
/**
* 更新用户
*/
@Transactional
public ResultObj updateUserByUsername(HttpServletRequest request) {
User user = new User();
user.setUsername(request.getParameter("update_username"));
user.setCname(request.getParameter("update_cname"));
user.setEmail(request.getParameter("update_email"));
user.setMobile(request.getParameter("update_mobile"));
user.setAddr(request.getParameter("update_addr"));
user.setValidUntilTime(formatValidUntilTime(request.getParameter("update_valid_until")));
user.setStatus(request.getParameter("update_status"));
int updated = userDao.updateUserByUsername(user);
ResultObj result = new ResultObj();
if (updated >= 1) {
result.setCode("0");
result.setMsg("修改用户成功");
logger.info("/user/update - 修改用户成功, username: " + user.getUsername());
} else {
logger.error("/user/update - 修改用户失败, 系统没有查询到该用户, username: " + user.getUsername());
result.setCode("1");
result.setMsg("修改用户失败,系统没有查询到该用户");
}
return result;
}
/**
* 删除用户
*/
public ResultObj deleteUserByUsername(String username) {
ResultObj result = new ResultObj();
int deleted = userDao.deleteByUsername(username);
logger.info("/user/delete - username: " + username + ", delete count: " + deleted);
if (deleted == 1) {
result.setCode("0");
result.setMsg("删除用户成功");
} else {
result.setCode("1");
result.setMsg("删除用户失败,系统没有查询到该用户");
}
return result;
}
/**
* 重置用户密码
*/
public ResultObj updatePwByUsername(String resetUsername, String pword) {
int reseted = userDao.resetPwByUsername(resetUsername, pword);
ResultObj result = new ResultObj();
logger.info("/resetUserPw - username: " + resetUsername + ", reset count: " + reseted);
if (reseted == 1) {
result.setCode("0");
result.setMsg("重置用户登录密码成功");
} else {
result.setCode("1");
result.setMsg("重置用户登录密码失败,系统没有查询到该用户【" + resetUsername + "】");
}
return result;
}
3. 持久层Dao,UserMapper.xml
insert into user
username,
password,
status,
email,
cname,
mobile,
addr,
create_time,
valid_until_time,
profile,
#{username,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},
#{status,jdbcType=VARCHAR},
#{email,jdbcType=VARCHAR},
#{cname,jdbcType=VARCHAR},
#{mobile,jdbcType=VARCHAR},
#{addr,jdbcType=VARCHAR},
#{createTime,jdbcType=TIMESTAMP},
#{validUntilTime,jdbcType=DATE},
#{profile,jdbcType=LONGVARBINARY},
update user
set
status = #{status,jdbcType=VARCHAR},
email = #{email,jdbcType=VARCHAR},
cname = #{cname,jdbcType=VARCHAR},
mobile = #{mobile,jdbcType=VARCHAR},
addr = #{addr,jdbcType=VARCHAR},
valid_until_time = #{validUntilTime,jdbcType=DATE}
where username = #{username,jdbcType=VARCHAR}
delete from user
where username = #{username,jdbcType=VARCHAR}
update user
set password = #{password,jdbcType=VARCHAR}
where username = #{username,jdbcType=VARCHAR}