JavaWeb笔记-超市订单管理系统

5 篇文章 0 订阅

SMBMS-超市订单管理系统


项目搭建准备工作

1、创建一个maven项目

2、使用tomcat

3、测试项目能不能跑起来

4、导入项目中常用jar包

​ jsp、Servlet、mysql驱动

5、创建项目包结构

  • dao
  • filter
  • service
  • servlet
  • utils
  • pojo

6、编写实体类

​ ORM——关系映射(表-类映射)

7、编写基础公共类

​ 1、数据库配置文件

​ 新建db.properties里面写:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306?useUnicode=true&characterEncoding=utf-8
username=root
password=jiawensili1029

​ 2、操作数据库的基本类

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 {
        // 通过类加载器读取对应的资源
        InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
        var properties = new 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("com.shen.dao.BaseDao.driver");
            Class.forName("driver"); // 因为就在自己这个类里,直接写属性名就行了,执行这句话的时候,会首先链接这个类的
            connection = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            // 抛一个大异常,因为异常种类有点多
            e.printStackTrace();
        }
        return connection;
    }

    // 编写查询公共方法,这种写法比较通用
    // 专注于编写sql和传递参数params
    // 多一个Result参数方便重载,把Result和PreparedStatement作为参数,方便关闭管理
    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 throwables) {
                flag = false;
            }
        }
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
                preparedStatement = null;
            } catch (SQLException throwables) {
                flag = false;
            }
        }
        if (resultSet != null) {
            try {
                resultSet.close();
                resultSet = null;
            } catch (SQLException throwables) {
                flag = false;
            }
        }
        return flag;
    }
}

​ 3、编写字符编码过滤器

8、导入静态资源

登录功能实现

  1. 编写前端页面

  2. 设置首页

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

    // 面向接口编程
    public interface UserDao {
        // 得到要登录的用户
        public User getLoginUser(Connection connection,String userCode);
    }
    
  4. 编写Dao接口实现类

    public class UserDaoImpl implements UserDao {
        @Override
        public User getLoginUser(Connection connection, String userCode) {
            PreparedStatement pstm = null;
            ResultSet rs = null;
            User user = null;
            if(connection!=null) {
                String sql = "select * from smbms_user where usercode=?";
                Object[] params = {userCode};
                try {
                    rs = BaseDao.execute(connection, sql, params, rs, pstm);
                    if(rs.next()){
                        user = new User();
                        user.setId(rs.getInt("id"));
                        user.setAddress(rs.getString("address"));
                        user.setAge(rs.getInt("age"));
                        user.setUserCode(rs.getString("userCode"));
                        user.setBirthday(rs.getDate("birthday"));
                        user.setCreatedBy(rs.getInt("createdBy"));
                        user.setCreationDate(rs.getDate("creationDate"));
                        user.setGender(rs.getInt("gender"));
                        user.setModifyBy(rs.getInt("modifyBy"));
                        user.setModifyDate(rs.getDate("modifyDate"));
                        user.setPhone(rs.getString("phone"));
                        user.setUserName(rs.getString("userName"));
                        user.setUserPassword(rs.getString("userPassword"));
                        user.setUserRole(rs.getInt("userRole"));
                        user.setUserRoleName(rs.getString("roleName"));
                        BaseDao.closeResource(null,pstm,rs); // connection不用关,要当业务层关
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            return user;
        }
    }
    
  5. 编写业务层接口

    public interface UserService {
        // 用户登录
        public User login(String userCode,String password);
    }
    
  6. 编写业务层实现类

    public class UserServiceImpl implements UserService{
        // 业务层都会调用Dao层,所以我们要引入Dao层,注意这里的UserDao是接口类型,面向对象编程哦~!
        private UserDao userDao;
    
        // 以后这个东西交给容器来做
        public UserServiceImpl() {
            this.userDao = new UserDaoImpl();
        }
    
        // 前端传过来用户名和密码
        @Override
        public User login(String userCode, String password) {
            Connection connection = null;
            User user = null;
            connection = BaseDao.getConnection();
            // 通过业务层调用对应的具体的数据库操作
            user = userDao.getLoginUser(connection,userCode);
            BaseDao.closeResource(connection,null,null);
            return user;
        }
    
        @Test
        public void test(){
            UserService userService = new UserServiceImpl();
            User admin = userService.login("admin","12344");
            System.out.println(admin.getId());
        }
    
    
    }
    
  7. 编写Servlet

    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");
    
            // 和数据库中的密码进行对比,调用业务层。
            UserService userService = new UserServiceImpl();
            User user = userService.login(userCode, userPassword);
    
            if(user!=null){
                // 查有此人,可以登录
                // 将用户的信息放到Session中;
                req.getSession().setAttribute(Constants.USER_SESSION,user);
                // 跳转到内部主页
                resp.sendRedirect("jsp/frame.jsp");
            }else {
                // 查无此人,转发回登录页面,顺带提示用户名或者密码错误
                req.getRequestDispatcher("login.jsp").forward(req,resp);
            }
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            super.doPost(req, resp);
        }
    }
    

    注意,doget和dopost,使用哪个,取决于前端form表单里method类型

  8. 注册Servlet,要和前端所给名字相同

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


登录功能优化

注销功能:

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

public class LogoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("logout---get---start");
        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);
    }
}

登录拦截优化

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) request;
        User user = (User)req.getSession().getAttribute(Constants.USER_SESSION);
        if(user == null){
            // 已经被移除或者注销了,或者未登录
            resp.sendRedirect(req.getContextPath()+"/error.jsp");
        }else {
            chain.doFilter(request,response);
        }
    }

注意,如果user==null,chain不会往下走

error:重定向次数过多

因为重定向的页面error也在jsp文件夹下面,所以给他拿出来就好了

密码修改

功能建议自底向上写 —> 首先需要思考功能和架构

jquery写了js 更新以后,需要清除浏览器里的内容,否则我更新了,但我没更新

用户管理实现

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tOwvrdEj-1625065802008)(/Users/shenhangran/Desktop/学习笔记/我的/JavaWeb笔记-超市订单管理系统.assets/image-20210512141933641.png)]

  1. 写一个用来分页的工具类
  2. 导入用户列表页面

获取用户数量

获取用户列表

获取角色列表

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值