2021-02-24

项目搭建准备工作

  1. 搭建一个maven web项目

  2. 配置tomcat

  3. 测试项目是否能够跑起来
    (让tomcat运行,看是否能够出现网页)

  4. 在pom.xml中导入项目中遇到的jar包

      <dependencies>
    
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>servlet-api</artifactId>
          <version>2.5</version>
        </dependency>
        <dependency>
          <groupId>javax.servlet.jsp</groupId>
          <artifactId>jsp-api</artifactId>
          <version>2.3.3</version>
        </dependency>
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.5</version>
        </dependency>
        <dependency>
          <groupId>javax.servlet.jsp.jstl</groupId>
          <artifactId>jstl-api</artifactId>
          <version>1.2</version>
        </dependency>
        <dependency>
          <groupId>taglibs</groupId>
          <artifactId>standard</artifactId>
          <version>1.1.2</version>
        </dependency>
    
      </dependencies>
    
  5. 创建项目包括结构
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3hhwmmbq-1614180893168)(C:\Users\AW\AppData\Roaming\Typora\typora-user-images\image-20210223121536033.png)]

  6. 编写实体类
    ORM映射:表-类映射
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-36OjQUJy-1614180893170)(C:\Users\AW\AppData\Roaming\Typora\typora-user-images\image-20210223121612808.png)]

  7. 编写基础公共类

    1. 数据库配置文件

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

      2.编写数据库的公共类

      //操作数据库的公共类
      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();
              }
              return connection;
          }
      
          //编写查询公共类
          public static ResultSet excute(Connection connection,String sql,Object[] params,ResultSet resultSet,PreparedStatement preparedStatement) throws SQLException {
              //预编译的SQL在后面直接执行就可以了
              preparedStatement = connection.prepareStatement(sql);
      
              for (int i = 0 ; i < params.length; i++){
                  //setObject,占位符从1开始,但是我们的数组是从领开始
                  preparedStatement.setObject(i+1,params[i]);
              }
              resultSet = preparedStatement.executeQuery();
              return resultSet;
      
          }
      
          //编写增删改公共方法
          public static int excute(Connection connection,String sql,Object[] params,PreparedStatement preparedStatement) throws SQLException {
              preparedStatement = connection.prepareStatement(sql);
      
              for (int i = 0 ; i < params.length; i++){
                  //setObject,占位符从1开始,但是我们的数组是从领开始
                  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 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;
          }
      
      }
      
    2. 编写字符编码过滤器

      package filter;
      
      import javax.servlet.*;
      import java.io.IOException;
      
      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() {
      
          }
      }
      
  8. 导入静态资源

登录功能实现

上一篇:Java中JDBC的使用


下一篇:在 Mac OS X 上创建的 .NET 命令行程序访问数据库 (使用Entity Framework 7 )