超市订单管理系统updating

Posted hitenine

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了超市订单管理系统updating相关的知识,希望对你有一定的参考价值。

SMBMS

技术图片

数据库:

技术图片

项目搭建准备工作

使用Maven

  1. 搭配一个maven web项目

  2. 配置Tomcat

  3. 测试项目是否能够跑起来

  4. 导入项目中会遇到的jar包
    jsp, Servlet, mysql驱动, jstl, standard...

  5. 创建包结构
    技术图片

  6. 编写实体类
    ORM映射:表-类映射
    技术图片

  7. 编写基础公共类

    1. 数据库配置文件

      driver=com.mysql.cj.jdbc.Driver
      url=jdbc:mysql://localhost:3306/smbms?useSSL=true&useUnicode=true&characterEncoding=utf-8
      username=root
      password=123456
    2. 编写数据库公共类

      package com.hitenine.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();
              }
              System.out.println("ok");
              return connection;
          }
      
          /*
          有些bug
           */
          //编写查询公共方法
          public static ResultSet execute(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet, String sql, Object[] params) throws SQLException {
              //预编译的sql,在后面直接执行就可以了
              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, PreparedStatement preparedStatement, String sql, Object[] params) throws SQLException {
              //预编译的sql,在后面直接执行就可以了
              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 (null != resultSet) {
                  try {
                      resultSet.close();
                      //GC回收
                      resultSet = null;
                  } catch (SQLException e) {
                      e.printStackTrace();
                      flag = false;
                  }
              }
              if (null != preparedStatement) {
                  try {
                      preparedStatement.close();
                      //GC回收
                      preparedStatement = null;
                  } catch (SQLException e) {
                      e.printStackTrace();
                      flag = false;
                  }
              }
              if (null != connection) {
                  try {
                      connection.close();
                      //GC回收
                      connection = null;
                  } catch (SQLException e) {
                      e.printStackTrace();
                      flag = false;
                  }
              }
              return flag;
          }
      
      
      }
    3. 编写字符编码过滤器

  8. 导入静态资源

登录功能实现

技术图片

  1. 编写前端页面

  2. 设置首页

    <!--设置首页-->
    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>
  3. 编写dao层登录用户登录的接口

    //得到要登录的用户
    public User getLoginUser(Connection connection, String userCode) throws SQLException;
    
  4. 编写dao接口的实现类

    package com.hitenine.dao.user;
    
    import com.hitenine.dao.BaseDao;
    import com.hitenine.pojo.User;
    
    import javax.servlet.http.HttpServlet;
    import java.sql.*;
    
    public class UserDaoImpl  extends HttpServlet implements UserDao{
    
        //得到要登录的用户
        @Override
        public User getLoginUser(Connection connection, String userCode) throws SQLException {
            PreparedStatement pstm = null;
            ResultSet rs = null;
            User user = null;
    
            if (null != connection) {
                String sql = "select * from smbms_user where userCode=?";
                Object[] params = {userCode};
    
                Statement statement = connection.createStatement();
    
    
                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;
        }
    
    }
  5. 业务层接口

    //用户登录
    public User login(String userCode, String password);
  6. 业务层实现类

    package com.hitenine.service.user;
    
    import com.hitenine.dao.BaseDao;
    import com.hitenine.dao.user.UserDao;
    import com.hitenine.dao.user.UserDaoImpl;
    import com.hitenine.pojo.User;
    
    import java.sql.Connection;
    import java.sql.SQLException;
    
    public class UserServiceImpl implements UserService {
    
        //业务层都会调用dao层,所以我们要引入Dao层
        private UserDao userDao;
        public UserServiceImpl() {
            userDao = new UserDaoImpl();
        }
    
        @Override
        public User login(String userCode, String password) {
            Connection connection = null;
            User user = null;
    
            try {
                connection = BaseDao.getConnection();
                //通过业务层调用对应的数据库操作
                user = userDao.getLoginUser(connection, userCode);
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                BaseDao.closeResource(connection, null, null);
            }
    
            return user;
        }
    
  7. 编写Servlet

    package com.hitenine.servlet.user;
    
    import com.hitenine.pojo.User;
    import com.hitenine.service.user.UserServiceImpl;
    import com.hitenine.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 {
    
        //Servlet:控制层,调用业务层
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
            System.out.println("LoginServlet---start---");
    
            //获取用户名和密码
            String userCode = req.getParameter("userCode");
            String userPassword = req.getParameter("userPassword");
    
            //和数据库中的密码进行对比,调用业务层
            UserServiceImpl userService = new UserServiceImpl();
            User user = userService.login(userCode, userPassword); //这里已经把登录的人给查出来了
    
            //判断语句加上user.getUserPassword().equals(userPassword),为了便于测试暂不加
            if (null != user) { //查有此人,可以登录
                //将用户的信息放到Session中
                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);
        }
    }
  8. 注册Servlet

    <!--Servlet-->
    <servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>com.hitenine.servlet.user.LoginServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/login.do</url-pattern>
    </servlet-mapping>
  9. 测试访问,确保以上功能成功!

登录功能优化

注销功能

思路:移除Session,返回登陆页面

package com.hitenine.servlet.user;

import com.hitenine.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 {
        //移除用户的Constants.USER_SESSION
        req.getSession().removeAttribute(Constants.USER_SESSION);
        resp.sendRedirect(req.getContextPath() + "/login.jsp"); //返回登录页面
    }

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

注册xml

<servlet>
    <servlet-name>LogoutServlet</servlet-name>
    <servlet-class>com.hitenine.servlet.user.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>LogoutServlet</servlet-name>
    <url-pattern>/jsp/logout.do</url-pattern>
</servlet-mapping>

登录拦截优化

package com.hitenine.filter;

import com.hitenine.pojo.User;
import com.hitenine.util.Constants;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SysFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;

        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);

        if (null == user) { //已经被移除或者注销了,或者未登录
            resp.sendRedirect("/smbms/error.jsp");
        } else {
            chain.doFilter(request, response);
        }

    }

    @Override
    public void destroy() {

    }
}

注册xml

<!--用户登录过滤器-->
<filter>
    <filter-name>SysFilter</filter-name>
    <filter-class>com.hitenine.filter.SysFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>SysFilter</filter-name>
    <url-pattern>/jsp/*</url-pattern>
</filter-mapping>

以上是关于超市订单管理系统updating的主要内容,如果未能解决你的问题,请参考以下文章

IDEA+Java+SSM+JSP+Mysql实现超市订单系统

项目一:ssm超市订单管理系统

SMBMS超市订单管理系统

Django第四课 基于Django超市订单管理系统开发

Django第四课 基于Django超市订单管理系统开发

SSM整合改造超市订单管理系统