首页 > 其他分享 >26-用户管理功能实现

26-用户管理功能实现

时间:2022-09-22 09:56:08浏览次数:52  
标签:功能 java int req resultSet 26 用户 sql import

26-用户管理功能实现

概述

本文主要讲述用户管理功能的实现

简单分析

用户管理页面包含三个查询,从userlist.jsp中也可以看到

  1. 需要查询角色列表,筛选的时候可以筛选
  2. 需要返回分页信息,当前页面,总页面数量,总用户数量
  3. 需要查询用户列表的总数
  4. 需要查询用户列表详细信息

将jsp文件中需要的信息设置到req中的属性中

实践

UserDao.java


// 获取用户列表数量
int getUserNum(Connection conn, PreparedStatement preparedStatement, String userName, int userRole);

// 获取用户列表
List getUserList(Connection conn, PreparedStatement preparedStatement, String userName, int UserRole, int currentPageNo, int pageSize);

UserDaoImpl.java

 @Override
    public int getUserNum(Connection conn, PreparedStatement preparedStatement, String userName, int userRole) {
        StringBuilder sql = new StringBuilder();
        sql.append("select count(1) as count from smbms_user u, smbms_role r where u.userRole = r.id");
        ArrayList<Object> arrayList = new ArrayList<>();
        if (!StringUtils.isNullOrEmpty(userName)) {
            sql.append(" and u.userName like ?");
            arrayList.add("%"+userName+"%");
        }

        if (userRole>0) {
            sql.append(" and u.userRole=?");
            arrayList.add(userRole);
        }

        ResultSet resultSet = null;
        int count = 0;
        System.out.println(sql);
        System.out.println(arrayList.toArray());
        try {
            resultSet = BaseDao.execute(conn, sql.toString(), arrayList.toArray(), preparedStatement, resultSet);
            if(resultSet.next()){
                count = resultSet.getInt("count");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }

    @Override
    public List getUserList(Connection conn, PreparedStatement preparedStatement, String userName, int userRole,
        int currentPageNo, int pageSize) {
        StringBuilder sql = new StringBuilder();
        sql.append("select u.address, u.birthday,u.userCode,u.userName,u.userPassword,u.userRole,u.createdBy,u.creationDate,u.modifyBy,u.modifyDate,u.gender,u.id from smbms_user u, smbms_role r where u.userRole = r.id");
        ArrayList<Object> arrayList = new ArrayList<>();
        if (!StringUtils.isNullOrEmpty(userName)) {
            sql.append(" and u.userName like ?");
            arrayList.add("%"+userName+"%");
        }

        if (userRole>0) {
            sql.append(" and u.userRole=?");
            arrayList.add(userRole);
        }

        sql.append(" order by u.creationDate DESC limit ?,?");
        currentPageNo = (currentPageNo-1)*pageSize;
        arrayList.add(currentPageNo);
        arrayList.add(pageSize);


        ResultSet resultSet = null;
        int count = 0;
        System.out.println(sql);
        System.out.println(arrayList.toArray().toString());
        List<User> users = new ArrayList<>();
        try {
            resultSet = BaseDao.execute(conn, sql.toString(), arrayList.toArray(), preparedStatement, resultSet);
            while (resultSet.next()){
                User _user = new User();
                _user.setAddress(resultSet.getString("address"));
                _user.setBirthday(resultSet.getDate("birthday"));
                _user.setUserCode(resultSet.getString("userCode"));
                _user.setUserName(resultSet.getString("userName"));
                _user.setUserPassword(resultSet.getString("userPassword"));
                _user.setUserRole(resultSet.getString("userRole"));
                _user.setCreatedBy(resultSet.getInt("createdBy"));
                _user.setCreationDate(resultSet.getDate("creationDate"));
                _user.setModifyBy(resultSet.getInt("modifyBy"));
                _user.setModifyDate(resultSet.getDate("modifyDate"));
                _user.setGender(resultSet.getInt("gender"));
                _user.setId(resultSet.getInt("id"));
                users.add(_user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return users;
    }

RoleDao.java

package com.kuang.dao.role;

import com.kuang.pojo.Role;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * 功能描述
 *
 * @since 2022-09-20
 */
public interface RoleDao {
    List<Role> getRoleList(Connection conn, PreparedStatement preparedStatement) throws SQLException;
}

RoleDaoImpl.java

package com.kuang.dao.role;

import com.kuang.dao.BaseDao;
import com.kuang.pojo.Role;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述
 *
 * @since 2022-09-20
 */
public class RoleDaoImpl implements RoleDao{
    @Override public List<Role> getRoleList(Connection conn, PreparedStatement preparedStatement) throws SQLException {
        String sql = "select id,roleCode,roleName from smbms_role";
        ResultSet resultSet = null;
        Object[] params ={};
        ResultSet rs = BaseDao.execute(conn, sql, params, preparedStatement, resultSet);
        List<Role> list = new ArrayList<>();
        while(rs.next()){
            Role _role = new Role();
            _role.setId(rs.getInt("id"));
            _role.setRoleCode(rs.getString("roleCode"));
            _role.setRoleName(rs.getString("roleName"));
            list.add(_role);
        }
        return list;
    }
}

RoleService.java

package com.kuang.service;

import com.kuang.pojo.Role;

import java.sql.SQLException;
import java.util.List;

/**
 * 功能描述
 *
 * @since 2022-09-20
 */
public interface RoleService {
    List<Role> getRoleList() throws SQLException;
}

RoleServiceImpl.java

package com.kuang.service;

import com.kuang.dao.BaseDao;
import com.kuang.dao.role.RoleDao;
import com.kuang.dao.role.RoleDaoImpl;
import com.kuang.pojo.Role;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 功能描述
 *
 * @since 2022-09-20
 */
public class RoleServiceImpl implements RoleService{

    private RoleDao roleDao;

    public RoleServiceImpl() {
        roleDao = new RoleDaoImpl();
    }

    @Override public List<Role> getRoleList() throws SQLException {
        Connection connection = BaseDao.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet rs=null;
        List<Role> roleList = roleDao.getRoleList(connection, preparedStatement);
        BaseDao.closeResource(connection,preparedStatement, rs);
        return roleList;
    }

    @Test
    public void test() throws SQLException {
        RoleServiceImpl roleService = new RoleServiceImpl();
        List<Role> roleList = roleService.getRoleList();
        System.out.println(roleList);
    }
}

UserService.java

    // 获取用户列表数量
    int getUserNum(String userName, int userRole);

    // 获取用户列表
    List getUserList(String userName, int UserRole, int currentPageNo, int pageSize);

UserServiceImpl.java


    @Override public int getUserNum(String userName, int userRole) {
        PreparedStatement preparedStatement = null;
        int i = userDao.getUserNum(connection, preparedStatement, userName, userRole);
        BaseDao.closeResource(null, preparedStatement, null);
        return i;
    }

    @Override public List getUserList(String userName, int UserRole, int currentPageNo, int pageSize) {
        PreparedStatement preparedStatement = null;
        List userList = userDao.getUserList(connection, preparedStatement, userName, UserRole, currentPageNo, pageSize);
        BaseDao.closeResource(null, preparedStatement, null);
        return userList;
    }

UserServlet.java


package com.kuang.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.kuang.pojo.Role;
import com.kuang.pojo.User;
import com.kuang.service.RoleService;
import com.kuang.service.RoleServiceImpl;
import com.kuang.service.UserService;
import com.kuang.service.UserServiceImpl;
import com.kuang.utils.Constants;
import com.mysql.jdbc.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

/**
 * 功能描述
 *
 * @since 2022-09-07
 */
public class UserServlet extends HttpServlet {
    private static final long serialVersionUID = 4477827924112311146L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method != null) {
            if (method.equals("pwdmodify")) {
                validatePwd(req, resp);
            } else if (method.equals("savepwd")) {
                updatePwd(req, resp);
            } else if (method.equals("query")){
                query(req, resp);
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String queryUserName = req.getParameter("queryname");
        String temp = req.getParameter("queryUserRole");
        String pageIndex = req.getParameter("pageIndex");
        int queryUserRole = 0;

        if (queryUserName == null){
            queryUserName = "";
        }

        if (!StringUtils.isNullOrEmpty(temp)){
            queryUserRole = Integer.parseInt(temp);
        }
        int currentPageNo = 1;
        if (pageIndex != null){
            currentPageNo = Integer.parseInt(pageIndex);
        }


        int pageSize = 5;

        int totalCount = 0;


        RoleService roleService = new RoleServiceImpl();
        List<Role> roleList = null;
        try {
            roleList = roleService.getRoleList();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        req.setAttribute("roleList", roleList);


        List<User> userList = null;
        UserService userService = new UserServiceImpl();
        userList = userService.getUserList(queryUserName,queryUserRole,currentPageNo, pageSize);
        totalCount = userService.getUserNum(queryUserName,queryUserRole);
        int totalPageCount = totalCount/pageSize+1;
        req.setAttribute("roleList", roleList);
        req.setAttribute("userList", userList);
        req.setAttribute("totalCount", totalCount);
        req.setAttribute("totalPageCount", totalPageCount);
        req.setAttribute("currentPageNo", currentPageNo);
        req.setAttribute("queryUserName", queryUserName);
        req.setAttribute("queryUserRole", queryUserRole);
        req.getRequestDispatcher("/jsp/userlist.jsp").forward(req, resp);
    }

    public void updatePwd(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        Object o = req.getSession().getAttribute(Constants.USER_SESSION);
        String newPwd = req.getParameter("newpassword");
        if (o != null && !StringUtils.isNullOrEmpty(newPwd)) {
            User user = (User) o;
            UserService userService = new UserServiceImpl();
            boolean flag = userService.updateUserPwd(user.getId(), newPwd);
            if (flag) {
                req.setAttribute(Constants.MESSAGE, "修改成功,请重新登录");
                // 需移除当前Session
                req.getSession().removeAttribute(Constants.USER_SESSION);

            } else {
                req.setAttribute(Constants.MESSAGE, "修改失败");

            }
        } else {
            req.setAttribute(Constants.MESSAGE, "新密码有问题");

        }
        req.getRequestDispatcher("/jsp/pwdmodify.jsp").forward(req, resp);
    }

    private void validatePwd(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Object o = req.getSession().getAttribute(Constants.USER_SESSION);
        String oldPwd = req.getParameter("oldpassword");
        HashMap<String, String> map = new HashMap<>();
        if (o == null) {
            map.put("result", "sessionerror");
        } else {
            if (StringUtils.isNullOrEmpty(oldPwd)) {
                map.put("result", "error");
            } else {
                User user = (User) o;
                String pwd = user.getUserPassword();
                if (pwd.equals(oldPwd)) {
                    map.put("result", "true");
                } else {
                    map.put("result", "false");
                }
            }
        }
        PrintWriter writer = resp.getWriter();
        ObjectMapper mapper = new ObjectMapper();
        writer.write(mapper.writeValueAsString(map));
        writer.flush();
        writer.close();

    }
}

标签:功能,java,int,req,resultSet,26,用户,sql,import
From: https://www.cnblogs.com/Oh-mydream/p/16718138.html

相关文章