带你初步认识搭建项目(底层)

准备工作

  1. 配置环境

  2. 测试网页嫩不能跑起来

  3. 建包

    pojo包 实体类

    dao 操作数据库,持久化操作

    service 业务

    servlet

    filter 过滤器

    util 工具类

  4. 编写实体类--------------------------一个表就是一个实体类:序列化get,set一下

    ORM映射

  5. 编写基础公共类

    数据库配置文件 在resource下写一个file 写drive,url,username,password

    在dao层用static初始化

    public class User {
       public static String username;
       public static String password;
       public static String driver;
       public static String url;

       static {
           Properties properties = new Properties();
           InputStream resourceAsStream = User.class.getClassLoader().getResourceAsStream("db.properties");
           try {
               properties.load(resourceAsStream);
          } catch (IOException e) {
               e.printStackTrace();
          }
           String username = properties.getProperty("username");
           String password = properties.getProperty("password");
           String driver = properties.getProperty("driver");
           String url = properties.getProperty("url");

      }

       //获取数据库连接
       public static Connection getConnection() {
           Connection connection = null;
           try {
               Class.forName(driver);
               connection = DriverManager.getConnection(url, username, password);
          } catch (ClassNotFoundException e) {
               e.printStackTrace();
          } catch (SQLException e) {
               e.printStackTrace();
          }
           return connection;
      }

       //编写查询公共方法
       public static ResultSet excute(Connection connection, String sql, Object[] params, PreparedStatement preparedStatement, ResultSet resultSet) throws Exception {
           preparedStatement = connection.prepareStatement(sql);
           for (int i = 0; i < params.length; i++) {
               //index不能为0
               preparedStatement.setObject(i + 1, params[i]);
          }
           resultSet = preparedStatement.executeQuery();
           return resultSet;
      }

       //编写增删改的公共方法
       public static int excute(Connection connection, String sql, Object[] params, PreparedStatement preparedStatement) throws Exception {
           preparedStatement = connection.prepareStatement(sql);
           for (int i = 0; i < params.length; i++) {
               //index不能为0
               preparedStatement.setObject(i + 1, params[i]);
          }
           int updateRows = preparedStatement.executeUpdate();
           return updateRows;
      }

       //关闭连接
       public static boolean closeSourse(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;
      }
    }

    字符编码过滤器

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    servletRequest.setCharacterEncoding("utf-8");
    servletResponse.setCharacterEncoding("utf-8");
    filterChain.doFilter(servletRequest,servletResponse);
      }
  6. 导入静态资源

猜你喜欢

转载自www.cnblogs.com/ltdh/p/12488981.html