ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

狂神说 javaweb 30集:SMBMS(超市管理项目)_SMBMS 项目搭建(一)

2022-05-22 23:00:07  阅读:235  来源: 互联网

标签:String 30 private connection SMBMS params Integer pstm javaweb


30.SMBMS 项目搭建

15、SMBMS(超市管理项目)

SMBMS(超市管理项目)

在这里插入图片描述

数据库:

在这里插入图片描述

 

项目如何搭建? 考虑是不是用maven? jar包,依赖

搭建项目准备工作

  1. 搭建一个maven web 项目

  2. 配置Tomcat

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

  4. 导入项目中需要的jar包; jsp,Servlet,mysql驱动jstl,stand…

  5. 构建项目包结构

    在这里插入图片描述

  6. 编写实体类 ROM映射:表-类映射

    ORM:对象关系映射

    实体类中的属性与数据库表中的字段一一对应

    注意:由于Address表存放的是地址,没有实际含义,因此不编写对应实体类

    1、用户类

     private Integer id; // 用户ID
     private String userCode;    // 用户编码
     private String userName;    // 用户名
     private String userPassword;    // 用户密码
     private Integer gender; // 性别
     private Date birthday;  // 出生日期
     private String phone;   // 电话
     private String address; // 地址
     private Integer userRole;   // 用户角色ID
     private Integer createdBy;  // 创建者ID
     private Date creationDate;// 创建时间
     private Integer modifyBy;   // 修改者ID
     private Date modifyDate;    // 修改时间
     ​
     private Integer age;    // 年龄,通过当前时间-出生年份得出
     private String userRoleBane;    // 用户角色名称
     ​

    2、角色类

    属性

     private Integer id; // 角色ID
     private String roleCode;    // 角色编码
     private String roleName;    // 角色名
     private Integer createdBy;  // 创建者ID
     private Date creationDate;// 创建时间
     private Integer modifyBy;   // 修改者ID
     private Date modifyDate;    // 修改时间
     ​

    3、账单类

    属性

     private Integer id; // 账单ID
     private String billCode;    // 账单编码
     private String productName; // 商品名
     private String productDesc; // 商品描述
     private String productUnit; // 商品单价
     private BigDecimal productCount; // 商品数量
     private BigDecimal totalPrice; // 总金额
     private Integer isPayment; // 是否支付
     private Integer createdBy;  // 创建者ID
     private Date creationDate;// 创建时间
     private Integer modifyBy;   // 修改者ID
     private Date modifyDate;    // 修改时间
     private Integer providerId; // 供应商ID
     ​
     private String providerName;    //供应商名称
     ​

    4、供应商类

    属性

     private Integer id; // 供应商ID
     private String proCode; // 供应商编码
     private String proName; // 供应商名称
     private String proDesc; // 供应商描述
     private String proContact; // 联系人名称
     private String proPhone;   // 供应商电话
     private String proAddress; // 供应商地址
     private String proFax; // 供应商传真
     private Integer createdBy;  // 创建者ID
     private Date creationDate;// 创建时间
     private Integer modifyBy;   // 修改者ID
     private Date modifyDate;    // 修改时间
     ​

 

 

7.编写基础公共类 : 编写BaseDAO

  1. 数据库配置文件(mysql5.xx和8.xx的编写有差异)

    创建数据库配置文件:db.properties

     driver=com.mysql.cj.jdbc.Driver
     url=jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=utf8&userSSL=false&serverTimezone=GMT%2B8
     username=root
     password=1111
  2. 编写公共DAO类

    编写公共DAO类,便于其他DAO直接调用。

    加载驱动

    获取服务器连接

    获取SQL执行对象

    执行SQL语句

    释放连接

    BaseDao.java

     package com.study.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 params = new Properties();
             String configFile = "db.properties";
             InputStream is = BaseDao.class.getClassLoader().getResourceAsStream(configFile);
             try {
                 params.load(is);
            } catch (IOException e) {
                 e.printStackTrace();
            }
             driver = params.getProperty("driver");
             url = params.getProperty("url");
             username = params.getProperty("username");
             password = params.getProperty("password");
        }
     ​
     ​
         /**
          * 获取数据库连接
          *
          * @return
          */
         public static Connection getConnection() {
             Connection connection = null;
             try {
                 Class.forName(driver);
                 connection = DriverManager.getConnection(url, username, password);
            } catch (Exception e) {
                 e.printStackTrace();
            }
             return connection;
        }
     ​
         /**
          * 查询数据库的公共操作
          *
          * @param connection
          * @param pstm
          * @param rs
          * @param sql
          * @param params
          * @return
          */
         public static ResultSet execute(Connection connection, PreparedStatement pstm, ResultSet rs, String sql, Object[] params) {
             try {
                 pstm = connection.prepareStatement(sql);
                 for (int i = 0; i < params.length; i++) {
                     pstm.setObject(i + 1, params[i]);
                }
                 rs = pstm.executeQuery();
            } catch (SQLException throwables) {
                 throwables.printStackTrace();
            }
             return rs;
        }
     ​
         /**
          * 更新数据库的公共操作
          *
          * @param connection
          * @param pstm
          * @param sql
          * @param params
          * @return
          * @throws Exception
          */
         public static int execute(Connection connection, PreparedStatement pstm, String sql, Object[] params) {
             int updateRows = 0;
             try {
                 pstm = connection.prepareStatement(sql);
                 for (int i = 0; i < params.length; i++) {
                     pstm.setObject(i + 1, params[i]);
                }
                 updateRows = pstm.executeUpdate();
            } catch (SQLException throwables) {
                 throwables.printStackTrace();
            }
             return updateRows;
        }
     ​
         /**
          * 释放资源
          *
          * @param connection
          * @param pstm
          * @param rs
          * @return
          */
         public static boolean closeResource(Connection connection, PreparedStatement pstm, ResultSet rs) {
             boolean flag = true;
             if (rs != null) {
                 try {
                     rs.close();
                     rs = null;//GC回收
                } catch (SQLException e) {
                     e.printStackTrace();
                     flag = false;
                }
            }
             if (pstm != null) {
                 try {
                     pstm.close();
                     pstm = null;//GC回收
                } catch (SQLException e) {
                     e.printStackTrace();
                     flag = false;
                }
            }
             if (connection != null) {
                 try {
                     connection.close();
                     connection = null;//GC回收
                } catch (SQLException e) {
                     e.printStackTrace();
                     flag = false;
                }
            }
             return flag;
        }
     }
     ​
  3. 编写字符编码过滤器

    CharacterEncodingFilter.java

     public class CharacterEncodingFilter implements Filter {
     ​
         public void init(FilterConfig filterConfig) throws ServletException {
     ​
        }
     ​
         public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
             request.setCharacterEncoding( "utf-8" );
             response.setCharacterEncoding( "utf-8" );
     ​
             chain.doFilter( request,response );
     ​
        }
     ​
         public void destroy() {
     ​
        }
     }

    xml

     <filter>
         <filter-name>CharacterEncodingFilter</filter-name>
         <filter-class>com.study.filter.CharacterEncodingFilter</filter-class>
     </filter>
     <filter-mapping>
         <filter-name>CharacterEncodingFilter</filter-name>
         <url-pattern>/*</url-pattern>
     </filter-mapping>

 

  1. 导入静态资源

 

 

标签:String,30,private,connection,SMBMS,params,Integer,pstm,javaweb
来源: https://www.cnblogs.com/jianchizuo/p/16299466.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有