• 超适合练手的一套JavaWeb项目 (超市后台管理系统)


    GIF动态图演示

    在这里插入图片描述

    百度百度网盘提取项目 带数据库![链接]:https://pan.baidu.com/s/13F2rxszZRLGDt9pr6ixYUg提取码:关注私信我发送!

    一、项目搭建准备工作

    • 1.搭建一个maven web项目
    • 2.配置Tomcat
    • 3.测试项目是否能够跑起来
    • 4.导入项目中遇到的jar包
    • 5.创建项目结构
      在这里插入图片描述

    6.编写实体类

    ORM映射:表类映射
    
    • 1

    7.编写基础公共类

    1.数据库配置文件

    db.properties文件代码

    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306?useUnicode=true&characterEncoding=utf-8
    username=root
    password=111111
    
    • 1
    • 2
    • 3
    • 4

    2.编写数据库的公共类

    java代码

    package com.syj.dao;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.*;
    import java.util.Properties;
    
    //操作数据库的公共类
    public class BaseDao {
        private static String driver;
        private static String url;
        private static String username;
        private static String password;
    
        //静态代码块,类加载的时候就初始化了
        static {
            Properties properties = new Properties();
            //通过类加载器读取对应的资源
            InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
    
            try {
                properties.load(is);
            } catch (IOException e) {
                e.printStackTrace();
            }
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
        }
    
        //获取数据库的链接
        public static Connection getConnection(){
            Connection connection = null;
            try {
                Class.forName(driver);
                connection = DriverManager.getConnection(url, username, password);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return  connection;
        }
    
        //编写查询公共类
        public static ResultSet execute( Connection connection,String sql,Object[] params,ResultSet resultSet, PreparedStatement preparedStatement ) throws SQLException {
           preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                //setObject,占位符从1开始,但是我们的数组是从0开始!
                preparedStatement.setObject(i+1,params[i]);
            }
    
            resultSet = preparedStatement.executeQuery();
            return resultSet;
        }
    
        //编写增删改查公共方法
        public static int execute( Connection connection,String sql,Object[] params, PreparedStatement preparedStatement ) throws SQLException {
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                //setObject,占位符从1开始,但是我们的数组是从0开始!
                preparedStatement.setObject(i+1,params[i]);
            }
    
            int updateRows = preparedStatement.executeUpdate();
            return updateRows;
        }
    
        public static boolean closeResource(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet){
            boolean flag = true;
    
            if(resultSet != null){
                try {
                    resultSet.close();
                    //GC回收
                    resultSet = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                    flag = false;
                }
            }
    
            if(preparedStatement != null){
                try {
                    preparedStatement.close();
                    //GC回收
                    preparedStatement = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                    flag = false;
                }
            }
    
            if(connection != null){
                try {
                    connection.close();
                    //GC回收
                    connection = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                    flag = false;
                }
            }
    
            return flag;
        }
    }
    
    • 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

    3.编写字符编码过滤器

    8.导入静态资源

    二、登录功能实现

    1.编写前端页面

    2.设置欢迎界面

    xml代码

    
        
            login.jsp
        
    
    • 1
    • 2
    • 3
    • 4

    3.编写dao层登录用户的接口

    java dao层接口代码

    package com.syj.dao.user;
    import com.syj.entity.User;
    import java.sql.Connection;
    import java.sql.SQLException;
    public interface UserDao {
        //得到登录的用户
        public User getLoginUser(Connection connection,String userCode) throws SQLException;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    4.编写dao接口的实现类

    java dao接口的实现类代码

    package com.syj.dao.user;
    import com.syj.dao.BaseDao;
    import com.syj.entity.User;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    public class UserDaoImpl implements UserDao{
        //得到要登录的用户
        public User getLoginUser(Connection connection, String userCode) throws SQLException {
            PreparedStatement pstm = null;
            ResultSet rs = null;
            User user = null;
    
            if(connection != null){
                String sql = "select * from smbms_user where userCode=?";
                Object[] params = {userCode};
                rs = BaseDao.execute(connection,pstm,rs,sql,params);
                if(rs.next()){
                    user = new User();
                    user.setId(rs.getInt("id"));
                    user.setUserCode(rs.getString("userCode"));
                    user.setUserName(rs.getString("userName"));
                    user.setUserPassword(rs.getString("userPassword"));
                    user.setGender(rs.getInt("gender"));
                    user.setBirthday(rs.getDate("birthday"));
                    user.setPhone(rs.getString("phone"));
                    user.setAddress(rs.getString("address"));
                    user.setUserRole(rs.getInt("userRole"));
                    user.setCreatedBy(rs.getInt("createdBy"));
                    user.setCreationDate(rs.getTimestamp("creationDate"));
                    user.setModifyBy(rs.getInt("modifyBy"));
                    user.setModifyDate(rs.getTimestamp("modifyDate"));
                }
                BaseDao.closeResource(null,pstm,rs);
            }
            return user;
        }
    }
    
    • 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

    5.业务层接口

    java service接口代码

    package com.syj.service.user;
    import com.syj.entity.User;
    public interface UserService {
            //用户登录
        public User login(String userCode,String password);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    6.业务层实现类

    java serviceImpl实现类代码

    package com.syj.service.user;
    import com.syj.dao.BaseDao;
    import com.syj.dao.user.UserDao;
    import com.syj.dao.user.UserDaoImpl;
    import com.syj.entity.User;
    import java.sql.Connection;
    import java.sql.SQLException;
    public class UserServiceImpl implements UserService{
    
        private UserDao userDao;
        public UserServiceImpl(){
            userDao = new UserDaoImpl();
        }
    
        public User login(String userCode, String password) {
            Connection connection = null;
            User user = null;
    
            connection = BaseDao.getConnection();
            try {
                user = userDao.getLoginUser(connection,userCode);
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                BaseDao.closeResource(connection,null,null);
            }
            return user;
        }
    
    }
    
    • 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

    7.编写Servlet

    java 代码

    package com.syj.servlet;
    import com.syj.entity.User;
    import com.syj.service.user.UserService;
    import com.syj.service.user.UserServiceImpl;
    import com.syj.util.Constants;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    public class LoginServlet extends HttpServlet {
        
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String userCode = req.getParameter("userCode");
            String userPassword = req.getParameter("userPassword");
    
            UserService userService = new UserServiceImpl();
            User user = userService.login(userCode, userPassword);
    
            if(user != null){
                req.getSession().setAttribute(Constants.USER_SESSION,user);
                resp.sendRedirect("jsp/frame.jsp");
            }else{
                req.setAttribute("error","用户名或者密码不正确");
                req.getRequestDispatcher("login.jsp").forward(req,resp);
            }
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    
    • 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

    8.注册servlet

    xml代码

    
            LoginServlet
            com.syj.servlet.LoginServlet
        
        
            LoginServlet
            /login.do
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    9.测试访问确保以上功能成功

    三、登陆功能优化

    注销功能:

    思路:移除Session,返回登录界面
    java servlet代码

    package com.syj.servlet;
    import com.syj.util.Constants;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    public class LogoutServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
          req.getSession().removeAttribute(Constants.USER_SESSION);
          resp.sendRedirect("/login.jsp");
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    xml 代码

    
        
            LogoutServlet
            com.syj.servlet.LogoutServlet
        
        
            LogoutServlet
            /jsp/logout.do
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    四、登录拦截优化

    编写一个过滤器并注册

    java 代码

    package com.syj.filter;
    
    import com.syj.entity.User;
    import com.syj.util.Constants;
    
    import javax.servlet.*;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class SysFilter implements Filter {
        public void init(FilterConfig filterConfig) throws ServletException {
    
        }
    
        public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
            HttpServletRequest request = (HttpServletRequest) req;
            HttpServletResponse response = (HttpServletResponse) resp;
    
            User user = (User) request.getSession().getAttribute(Constants.USER_SESSION);
    
            if(user==null){
                response.sendRedirect("/smbms/error.jsp");
            }else{
                chain.doFilter(req,resp);
            }
    
    
        }
    
        public void destroy() {
    
        }
    }
    
    • 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

    xml 代码

       
        
            SysFilter
            com.syj.filter.SysFilter
        
        
            SysFilter
            /jsp/*
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    测试,登录,注销,权限,以上功能都要保证执行成功

    五、密码修改

    1.导入前端素材

    pwdmodify.jsp

    密码修改

    2.写项目,建议从底层向上写

    3.UserDao接口

    //修改用户密码
        public int updatePwd(Connection connection,int id,int password) throws SQLException;
    
    • 1
    • 2

    4.UserDao接口实现类

    //修改用户密码
        public int updatePwd(Connection connection, int id, int password) throws SQLException {
            PreparedStatement pstm = null;
            int execute = 0;
            if(connection != null){
                String sql = "update smbms_user set userPassword = ? where id = ?";
                Object[] params = {password,id};
                execute = BaseDao.execute(connection,pstm,sql,params);
                BaseDao.closeResource(null,pstm,null);
            }
    
            return execute;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    5.UserService层

      //根据用户Id修改密码
        public boolean updatePwd(int id,int pwd);
    
    • 1
    • 2

    6.UserService实现类

    public boolean updatePwd(int id, int pwd) {
            Connection connection = null;
            boolean flag = false;
            //修改密码
            try {
                connection = BaseDao.getConnection();
                if(userDao.updatePwd(connection,id,pwd) > 0){
                    flag = true;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                BaseDao.closeResource(connection,null,null);
            }
            return false;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    7. servlet控制器层

    记得实现复用,需要提取方法

     @Override
    package com.mario.servlet;
    
    import com.mario.entity.User;
    import com.mario.service.user.UserService;
    import com.mario.service.user.UserServiceImpl;
    import com.mario.util.Constants;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class UserServlet extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String method = req.getParameter("method");
            if(method.equals("savepwd") && method != null){
                this.updatePwd(req,resp);
            }
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    
        public void updatePwd(HttpServletRequest req, HttpServletResponse resp){
            Object obj = req.getSession().getAttribute(Constants.USER_SESSION);
            String newpassword = req.getParameter("newpassword");
    
            System.out.println(newpassword);
    
            boolean flag = false;
    
            if(obj != null && newpassword != null){
                UserService userService = new UserServiceImpl();
                flag = userService.updatePwd(((User) obj).getId(), newpassword);
                if(flag){
                    req.setAttribute("message","修改密码成功");
                    req.getSession().removeAttribute(Constants.USER_SESSION);
                }else{
                    req.setAttribute("message","密码修改失败");
                }
            }else{
                req.setAttribute("message","新密码有问题");
            }
            try {
                req.getRequestDispatcher("pwdmodify.jsp").forward(req,resp);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    • 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

    8. 测试

    优化密码修改使用Ajax

    1.阿里巴巴的fastjson

    pom.xml

    
          com.alibaba
          fastjson
          1.2.61
        
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.后台修改的代码

    package com.mario.servlet;
    import com.alibaba.fastjson.JSONArray;
    import com.mario.entity.User;
    import com.mario.service.user.UserService;
    import com.mario.service.user.UserServiceImpl;
    import com.mario.util.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.util.HashMap;
    import java.util.Map;
    
    public class UserServlet extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String method = req.getParameter("method");
            if(method.equals("savepwd") && method != null){
                this.updatePwd(req,resp);
            }else if(method.equals("pwdmodify") && method != null){
                this.pwdModify(req,resp);
            }
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    
        //修改密码
        public void updatePwd(HttpServletRequest req, HttpServletResponse resp){
            Object obj = req.getSession().getAttribute(Constants.USER_SESSION);
            String newpassword = req.getParameter("newpassword");
    
            System.out.println(newpassword);
    
            boolean flag = false;
    
            if(obj != null && newpassword != null){
                UserService userService = new UserServiceImpl();
                flag = userService.updatePwd(((User) obj).getId(), newpassword);
                if(flag){
                    req.setAttribute("message","修改密码成功");
                    req.getSession().removeAttribute(Constants.USER_SESSION);
                }else{
                    req.setAttribute("message","密码修改失败");
                }
            }else{
                req.setAttribute("message","新密码有问题");
            }
            try {
                req.getRequestDispatcher("pwdmodify.jsp").forward(req,resp);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        //验证旧密码,session中有用户的密码
        public void pwdModify(HttpServletRequest req, HttpServletResponse resp){
            Object obj = req.getSession().getAttribute(Constants.USER_SESSION);
            String oldpassword = req.getParameter("oldpassword");
    
            Map resultMap = new HashMap();
    
            if(obj==null){
                resultMap.put("result","sessionerror");
            }else if(StringUtils.isNullOrEmpty(oldpassword)){
                resultMap.put("result","error");
            }else{
                String userPassword = ((User) obj).getUserPassword();
                if(oldpassword.equals(userPassword)){
                    resultMap.put("result","true");
                }else{
                    resultMap.put("result","false");
                }
            }
    
            try {
                resp.setContentType("application/json");
                PrintWriter writer = resp.getWriter();
                writer.write(JSONArray.toJSONString(resultMap));
                writer.flush();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    • 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

    3.测试

    六、用户管理实现

    1.获取用户数量

    1.UserDao

     //查询用户总数
        public int getUserCount(Connection connection,String username,int userRole) throws SQLException;
    
    • 1
    • 2

    2.UserDaoImpl

        public int getUserCount(Connection connection, String username, int userRole) throws SQLException {
            PreparedStatement pstm = null;
            ResultSet rs = null;
            int count = 0;
            if(connection != null){
                StringBuilder sql = new StringBuilder();
                sql.append("select count(1) as count from smbms_user u,smbms_role r where u.userRole = r.id");
                ArrayList list = new ArrayList();
    
                if(!StringUtils.isNullOrEmpty(username)){
                    sql.append(" and u.userName like ? ");
                    list.add("%"+username+"%");
                }
    
                if(userRole>0){
                    sql.append(" and u.userRole = ? ");
                    list.add(userRole);
                }
    
                Object[] params = list.toArray();
                rs = BaseDao.execute(connection,pstm,rs,sql.toString(),params);
                if(rs.next()){
                    count = rs.getInt("count");
                }
                BaseDao.closeResource(null,pstm,rs);
            }
            return count;
        }
    
    • 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

    3.UserService

     //查询记录数
        public int getUserCount(String username,int userRole);
    
    • 1
    • 2

    4.UserServiceImpl

     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 e) {
                e.printStackTrace();
            }finally {
                BaseDao.closeResource(connection,null,null);
            }
            return count;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2.用户列表页导入

    1.UserDao

     //通过用户输入的条件查询用户列表
        public List getUserList(Connection connection, String userName, int userRole, int currentPageNo, int pageSize) throws  Exception;
    
    • 1
    • 2

    2.UserDaoImpl

     //通过用户输入的条件查询用户列表
        public List getUserList(Connection connection, String userName, int userRole, int currentPageNo, int pageSize) throws Exception {
            List userList = new ArrayList();
            PreparedStatement pstm=null;
            ResultSet rs=null;
            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 list = new ArrayList();
                if(!StringUtils.isNullOrEmpty(userName)){
                    sql.append(" and u.userName like ?");
                    list.add("%"+userName+"%");
                }
                if(userRole > 0){
                    sql.append(" and u.userRole = ?");
                    list.add(userRole);
                }
                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;
        }
    
    • 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

    3.UserService

     //根据条件查询用户列表
        public List getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize);
    
    • 1
    • 2

    4.UserServiceImpl

    public List getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
            Connection connection = null;
            List userList = null;
            try {
                connection = BaseDao.getConnection();
                userList = userDao.getUserList(connection, queryUserName,queryUserRole,currentPageNo,pageSize);
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                BaseDao.closeResource(connection, null, null);
            }
            return userList;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    3.获取角色操作

    1.RoleDao

    //获取角色列表
        public List getRoleList(Connection connection) throws SQLException;
    
    • 1
    • 2

    2.RoleDaoImpl

    package com.mario.dao.role;
    
    import com.mario.dao.BaseDao;
    import com.mario.entity.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;
    
    public class RoleDaoImpl implements RoleDao{
        //获取角色列表
        public List getRoleList(Connection connection) throws SQLException {
            PreparedStatement pstm = null;
            ResultSet rs = null;
            ArrayList rolesList = new ArrayList();
    
    
            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();
                    _role.setId( rs.getInt("id"));
                    _role.setRoleCode( rs.getString("roleCode"));
                    _role.setRoleName( rs.getString("roleName"));
                    rolesList.add(_role);
                }
                BaseDao.closeResource(null, pstm, rs);
            }
            return rolesList;
        }
    
    }
    
    • 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

    3.RoleService

     //获取角色列表
        public List getRoleList();
    
    • 1
    • 2

    4.RoleServiceImpl

    package com.mario.service.role;
    
    import com.mario.dao.BaseDao;
    import com.mario.dao.role.RoleDao;
    import com.mario.dao.role.RoleDaoImpl;
    import com.mario.entity.Role;
    
    import java.sql.Connection;
    import java.util.List;
    
    public class RoleServiceImpl implements RoleService{
    
        private RoleDao roleDao;
        public RoleServiceImpl(){
            roleDao = new RoleDaoImpl();
        }
    
        //获取角色列表
        public List getRoleList() {
            Connection connection=null;
            List roleList=null;
            try {
                connection= BaseDao.getConnection();
                roleList = roleDao.getRoleList(connection);
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                BaseDao.closeResource(connection,null,null);
            }
            return roleList;
        }
    }
    
    • 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

    4.用户显示的Servlet

    • 获取用户前端的数据(查询)

    • 判断请求是否需要执行,看参数的值判断

    • 为了实现分页,需要计算出当前页面和总页面,页面的大小

    • 用户列表展示

    • 返回前端

      package com.mario.servlet;

      import com.alibaba.fastjson.JSONArray;
      import com.mario.entity.Role;
      import com.mario.entity.User;
      import com.mario.service.role.RoleService;
      import com.mario.service.role.RoleServiceImpl;
      import com.mario.service.user.UserService;
      import com.mario.service.user.UserServiceImpl;
      import com.mario.util.Constants;
      import com.mario.util.PageSupport;
      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.util.HashMap;
      import java.util.List;
      import java.util.Map;

      public class UserServlet extends HttpServlet {

      @Override
      protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
          String method = req.getParameter("method");
          if(method.equals("savepwd") && method != null){
              this.updatePwd(req,resp);
          }else if(method.equals("pwdmodify") && method != null){
              this.pwdModify(req,resp);
          }else if(method.equals("query") && method != null){
              this.query(req,resp);
          }
      }
      
      @Override
      protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
          doGet(req, resp);
      }
      
      public void query(HttpServletRequest req, HttpServletResponse resp){
          //接收前端传来的参数
          String queryUserName = req.getParameter("queryname");
          String temp = req.getParameter("queryUserRole");//从前端传回来的用户角色码不知是否为空或者是有效角色码,所以暂存起来
          String pageIndex = req.getParameter("pageIndex");
          int queryUserRole=0;
      
          //通过UserServiceImpl得到用户列表,用户数
          UserServiceImpl userService = new UserServiceImpl();
          List userList = null;//用来存储用户列表
      
          //设置每页显示的页面容量
          int pageSize = 5;
          //设置当前的默认页码
          int currentPageNo=1;
      
          //前端传来的参数若不符合查询sql语句,即如果用户不进行设置,值为空会影响sql查询,需要给它们进行一些约束
          if(queryUserName==null){//这里为空,说明用户没有输入要查询的用户名,则sql语句传值为"",%%,会查询所有记录
              queryUserName="";
          }
          if(temp!=null && !temp.equals("")){
              //不为空,说明前端有传来的用户所设置的userCode,更新真正的角色码
              queryUserRole=Integer.parseInt(temp);//强制转换,前端传递的参数都是默认字符串,要转成int类型
          }
          if(pageIndex!=null){//说明当前用户有进行设置跳转页面
              currentPageNo=Integer.valueOf(pageIndex);
          }
      
          //有了用户名和用户角色后可以开始查询了,所以需要显示当前查询到的总记录条数
          int totalCount = userService.getUserCount(queryUserName, queryUserRole);
          //根据总记录条数以及当前每页的页面容量可以算出,一共有几页,以及最后一页的显示条数
          PageSupport pageSupport = new PageSupport();
          pageSupport.setCurrentPageNo(currentPageNo);
          pageSupport.setPageSize(pageSize);
          pageSupport.setTotalCount(totalCount);
      
          //可显示的总页数
          int totalPageCount=pageSupport.getTotalPageCount();
      
          //约束首位页,即防止用户输入的页面索引小于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 roleService = new RoleServiceImpl();
          List roleList = roleService.getRoleList();//用来存储角色表
          //得到了用户表与角色表以及各种经过处理后的参数,都存进req中
          req.setAttribute("roleList",roleList);
          req.setAttribute("totalCount", totalCount);
          req.setAttribute("currentPageNo", currentPageNo);
          req.setAttribute("totalPageCount", totalPageCount);
          req.setAttribute("queryUserName", queryUserName);
          req.setAttribute("queryUserRole", queryUserRole);
          
          //将所得到的的所有req参数送回给前端
          try {
              req.getRequestDispatcher("userlist.jsp").forward(req,resp);
          } catch (ServletException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
      
      //修改密码
      public void updatePwd(HttpServletRequest req, HttpServletResponse resp){
          Object obj = req.getSession().getAttribute(Constants.USER_SESSION);
          String newpassword = req.getParameter("newpassword");
      
          System.out.println(newpassword);
      
          boolean flag = false;
      
          if(obj != null && newpassword != null){
              UserService userService = new UserServiceImpl();
              flag = userService.updatePwd(((User) obj).getId(), newpassword);
              if(flag){
                  req.setAttribute("message","修改密码成功");
                  req.getSession().removeAttribute(Constants.USER_SESSION);
              }else{
                  req.setAttribute("message","密码修改失败");
              }
          }else{
              req.setAttribute("message","新密码有问题");
          }
          try {
              req.getRequestDispatcher("pwdmodify.jsp").forward(req,resp);
          } catch (ServletException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
      
      //验证旧密码,session中有用户的密码
      public void pwdModify(HttpServletRequest req, HttpServletResponse resp){
          Object obj = req.getSession().getAttribute(Constants.USER_SESSION);
          String oldpassword = req.getParameter("oldpassword");
      
          Map resultMap = new HashMap();
      
          if(obj==null){
              resultMap.put("result","sessionerror");
          }else if(StringUtils.isNullOrEmpty(oldpassword)){
              resultMap.put("result","error");
          }else{
              String userPassword = ((User) obj).getUserPassword();
              if(oldpassword.equals(userPassword)){
                  resultMap.put("result","true");
              }else{
                  resultMap.put("result","false");
              }
          }
      
          try {
              resp.setContentType("application/json");
              PrintWriter writer = resp.getWriter();
              writer.write(JSONArray.toJSONString(resultMap));
              writer.flush();
              writer.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
      
      • 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
      • 143
      • 144
      • 145
      • 146
      • 147
      • 148

      }

    七、订单管理(增删改查)

    八、供应商管理(增删改查)

    文章上面的代码只有一部分具体看项目里面的代码…

    &

    &

    &

    先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

  • 相关阅读:
    Linux下载安装 RabbitMQ​
    htop安装使用
    初识java——jdk?环境变量?及关于安装jdk的步骤
    Java-反射:实现map自动封装进实体类中 理解springmvc 参数封装底层原理
    使用Spark SQL读取阿里云OSS的数据
    中序遍历迭代算法(非递归算法)
    Jenkins+maven+testng+htmlreport单元自动化测试
    数据库系列:InnoDB下实现高并发控制
    关于获得淘宝商品评论的那些事
    几种常见的垃圾回收器和垃圾回收算法
  • 原文地址:https://blog.csdn.net/m0_67401228/article/details/126066942