• SMBMS系统_准备工作


    构建项目Maven/jar

    初次构建项目时,思考是不是通过maven创建,使用maven的化需要导入那些依赖;
    如果不是使用maven创建项目的话,使用哪些些jar包。

    检测验证项目

    选择使用maven创建项目完成,可以使用模板,导入完成后 补充自己目录结构。
    javaWeb结构
    在java下建自己的目录地址,此次使用的是com.smbms01,resources下建立资源路径;
    在java的代码路径下,建立相应的pack包路径:dao、filter、pojo/entity、service、servlet

    导入jar包/依赖

    使用maven创建项目的情况,我们要在pom.xml中添加依赖:

     <dependencies>
         <dependency>
          <groupId>junitgroupId>
          <artifactId>junitartifactId>
          <version>4.11version>
          <scope>testscope>
        dependency>
        <dependency>
          <groupId>javax.servletgroupId>
          <artifactId>javax.servlet-apiartifactId>
          <version>4.0.1version>
        dependency>
        <dependency>
          <groupId>javax.servlet.jspgroupId>
          <artifactId>jsp-apiartifactId>
          <version>2.2version>
        dependency>
        <dependency>
          <groupId>javax.servlet.jsp.jstlgroupId>
          <artifactId>jstlartifactId>
          <version>1.2version>
        dependency>
        <dependency>
          <groupId>mysqlgroupId>
          <artifactId>mysql-connector-javaartifactId>
          <version>8.0.30version>
        dependency>
      dependencies>
    
    • 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

    junit: 测试依赖;
    javax.servlet:服务依赖;
    javax.servlet.jsp:画面jsp依赖;
    javax.servlet.jsp.jstl:jstl依赖;
    mysql-connector-java:mysql的驱动连接。

    编写相应实体类

    编写实体类与SQL中表的字段要一一对应:
    User的表结构
    在这里插入图片描述
    实体类:

    package com.smbms01.pojo;
    
    import java.util.Date;
    
    /**
     * 用户表
     */
    public class User {
    
        /* id */
        private Integer id;
        /* 用户编码 */
        private String userCode;
        /* 用户名称 */
        private String userName;
        /* 用户密码 */
        private String userPassword;
        /* 用户性别 */
        private String gender;
        /* 出生日期 */
        private String birthday;
        /* 电话 */
        private String phone;
        /* 地址 */
        private String address;
        /* 用户角色 */
        private String userRole;
        /* 创建者 */
        private String createBy;
        /* 创建时间 */
        private String createdDate;
        /* 更新者 */
        private String modifyBy;
        /* 更新时间 */
        private String modifyDate;
    
        /* 年龄 */
        private Integer age;
        /* 用戶角色名称 */
        private String userRoleName;
    
        public String getUserRoleName() {
            return userRoleName;
        }
    
        public void setUserRoleName(String userRoleName) {
            this.userRoleName = userRoleName;
        }
    
        public Integer getAge() {
            Date date = new Date();
            Integer age = Integer.valueOf(birthday) - Integer.valueOf(date.getDate());
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public User() {
            super();
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getUserCode() {
            return userCode;
        }
    
        public void setUserCode(String userCode) {
            this.userCode = userCode;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getUserPassword() {
            return userPassword;
        }
    
        public void setUserPassword(String userPassword) {
            this.userPassword = userPassword;
        }
    
        public String getGender() {
            return gender;
        }
    
        public void setGender(String gender) {
            this.gender = gender;
        }
    
        public String getBirthday() {
            return birthday;
        }
    
        public void setBirthday(String birthday) {
            this.birthday = birthday;
        }
    
        public String getPhone() {
            return phone;
        }
    
        public void setPhone(String phone) {
            this.phone = phone;
        }
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
        public String getUserRole() {
            return userRole;
        }
    
        public void setUserRole(String userRole) {
            this.userRole = userRole;
        }
    
        public String getCreateBy() {
            return createBy;
        }
    
        public void setCreateBy(String createBy) {
            this.createBy = createBy;
        }
    
        public String getCreatedDate() {
            return createdDate;
        }
    
        public void setCreatedDate(String createdDate) {
            this.createdDate = createdDate;
        }
    
        public String getModifyBy() {
            return modifyBy;
        }
    
        public void setModifyBy(String modifyBy) {
            this.modifyBy = modifyBy;
        }
    
        public String getModifyDate() {
            return modifyDate;
        }
    
        public void setModifyDate(String modifyDate) {
            this.modifyDate = modifyDate;
        }
    }
    
    
    • 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
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168

    在eclipse中,使用alt+shift+s,可以使用工具的快捷方式,生成相应的gettesetter方法。
    在idea中 ,使用alt+inster,在打开的框中,可以生产相应的gettersetter方法。

    编写SQL公共基础类

    package com.smbms01.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");
            driver = properties.getProperty("url");
            driver = properties.getProperty("username");
            driver = 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,PreparedStatement preparedStatement,ResultSet resultSet) throws SQLException {
          // 预编译的 不需要再次传值,后面直接使用就可以了
            preparedStatement = connection.prepareStatement(sql);
    
            for (int i = 0; i < params.length; i++) {
                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++) {
                preparedStatement.setObject(i+1, params[i]);
            }
            int updateRows = preparedStatement.executeUpdate();
            return updateRows;
        }
    
        public  static boolean closeResource(Connection connection,PreparedStatement preparedStatement, ResultSet resultSet) throws SQLException {
            boolean flag = true;
            if (resultSet !=null){
                try {
                    resultSet.close();
                    preparedStatement = null;
                } catch (SQLException e){
                    e.printStackTrace();
                    flag = false;
                }
            }
    
            if (preparedStatement !=null){
                try {
                    resultSet.close();
                    preparedStatement = null;
                } catch (SQLException e){
                    e.printStackTrace();
                    flag = false;
                }
            }
    
            if (connection !=null){
                try {
                    resultSet.close();
                    preparedStatement = null;
                } catch (SQLException e){
                    e.printStackTrace();
                    flag = false;
                }
            }
    
            return true;
        }
    
    }
    
    • 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

    编写字符编码过滤器

    package com.smbms01.filter;
    
    
    import javax.servlet.*;
    import java.io.IOException;
    
    /**
     * 字符编码过滤器
     */
    public class CharacterEnCodingFilter implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
    
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            servletRequest.setCharacterEncoding("utf-8");
            servletResponse.setCharacterEncoding("utf-8");
            filterChain.doFilter(servletRequest, servletResponse);
        }
    
        @Override
        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
    在这里插入代码片  
      <filter>
        <filter-name>CharacterEnCodingFilterfilter-name>
        <filter-class>com.smbms01.filter.CharacterEnCodingFilterfilter-class>
      filter>
      <filter-mapping>
        <filter-name>CharacterEnCodingFilterfilter-name>
        <url-pattern>/*url-pattern>
      filter-mapping>
      
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    导入静态资源

    需要的内容,js,图片,css样式等;

  • 相关阅读:
    Java: 字符串indexOf() /substring()/replace() 的使用
    ASEMI新能源快恢复桥EGBJ5006/HGBJ5006
    [Shell详解-7]:循环语句
    批量将大量文件归类处理的简单步骤
    记录一次扩ubuntu的文件系统的过程
    Android 桌面小组件 AppWidgetProvider
    开源医疗大模型排行榜: 健康领域大模型基准测试
    seo优化
    django项目实战基于Python实现的飞机票销售系统
    Nginx 实用配置技巧,99%用过的是老司机
  • 原文地址:https://blog.csdn.net/qq_44896379/article/details/127944341