Javaweb开发一般步骤

一 概念

以超市订单管理系统为例进行说明。

SMBMS:超市订单管理系统。

二 功能描述

Javaweb开发一般步骤_第1张图片

三 数据库

Javaweb开发一般步骤_第2张图片

四 准备工作

考虑是否使用Maven?如果使用,需要添加依赖,如果不使用Maven,需要考虑引入Jar包。

这里使用Maven进行搭建。

五 搭建步骤

1 搭建一个Maven web项目

Javaweb开发一般步骤_第3张图片

Javaweb开发一般步骤_第4张图片

web.xml



2 配置Tomcat

Javaweb开发一般步骤_第5张图片

Javaweb开发一般步骤_第6张图片

3 测试项目是否可以跑起来

Javaweb开发一般步骤_第7张图片

4 导入项目中的jar包



  4.0.0
  org.example
  smbms
  1.0-SNAPSHOT
  war
  
    
      javax.servlet
      servlet-api
      2.5
    
    
      javax.servlet.jsp
      javax.servlet.jsp-api
      2.3.3
    
    
      mysql
      mysql-connector-java
      5.1.46
    
  

5 创建项目包结构

Javaweb开发一般步骤_第8张图片

6 编写实体类

ORM映射:表-类映射

7 编写基础公共类

7.1 数据库配置文件

db.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=utf8
username=root
password=123456

7.2 编写数据库功能类

package com.cakin.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @ClassName: BaseDao
 * @Description: 操作数据库的公共类
 * @Date: 2020/6/20
 * @Author: cakin
 */
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");
    }

    /**
     * 功能描述:获取数据库连接
     *
     * @return Connection 数据库连接
     * @author cakin
     * @date 2020/6/20
     */
    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 sql               查询语句
     * @param object            对象数组
     * @param resultSet         结果集
     * @param preparedStatement 预编译语句对象
     * @return ResultSet 结果集
     * @author cakin
     * @date 2020/6/20
     */
    public static ResultSet execute(Connection connection, String sql, Object[] object, ResultSet resultSet, PreparedStatement preparedStatement) throws SQLException {
        // 预编译的SQL,在后面直接执行就可以了
        PreparedStatement preparedStatement1 = connection.prepareStatement(sql);
        for (int i = 0; i < object.length; i++) {
            // setObject,占位符从1开始,但我们的数组从0开始
            preparedStatement.setObject(i + 1, object[i]);
        }
        resultSet = preparedStatement.executeQuery();
        return resultSet;
    }

    /**
     * 功能描述:增删改的公共方法
     *
     * @param connection        数据库连接
     * @param sql               查询语句
     * @param object            对象数组
     * @param preparedStatement 预编译语句对象
     * @return ResultSet 结果集
     * @author cakin
     * @date 2020/6/20
     */
    public static int execute(Connection connection, String sql, Object[] object, PreparedStatement preparedStatement) throws SQLException {
        // 预编译的SQL,在后面直接执行就可以了
        PreparedStatement preparedStatement1 = connection.prepareStatement(sql);
        for (int i = 0; i < object.length; i++) {
            // setObject,占位符从1开始,但我们的数组从0开始
            preparedStatement.setObject(i + 1, object[i]);
        }
        int updateCount = preparedStatement.executeUpdate();
        return updateCount;
    }

    /**
     * 功能描述:关闭资源
     *
     * @param connection        数据库连接
     * @param resultSet         结果集
     * @param preparedStatement 预编译语句对象
     * @return boolean boolean类型
     * @author cakin
     * @date 2020/6/20
     */
    public static boolean closeResource(Connection connection, ResultSet resultSet, PreparedStatement preparedStatement) {
        boolean flag = true;
        if (connection != null) {
            try {
                connection.close();
                connection = null;
            } catch (SQLException throwables) {
                flag = false;
                throwables.printStackTrace();
            }
        }

        if (resultSet != null) {
            try {
                resultSet.close();
                resultSet = null;
            } catch (SQLException throwables) {
                flag = false;
                throwables.printStackTrace();
            }
        }

        if (preparedStatement != null) {
            try {
                preparedStatement.close();
                preparedStatement = null;
            } catch (SQLException throwables) {
                flag = false;
                throwables.printStackTrace();
            }
        }
        return flag;
    }
}

7.3 编写过滤器

package com.cakin.filter;

import javax.servlet.*;
import java.io.IOException;

/**
 * @ClassName: CharacterEncodingFileter
 * @Description: 字符编码过滤器
 * @Date: 2020/6/20
 * @Author: cakin
 */
public class CharacterEncodingFileter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {

    }
    /**
     * 功能描述:解决中文乱码问题
     *
     * @author cakin
     * @date 2020/6/20
     * @param request 请求
     * @param response 响应
     * @param chain 过滤器链
     */
    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() {

    }
}

7.4 编写web.xml



    
    
        CharacterEncodingFileter
        com.cakin.filter.CharacterEncodingFileter
    
    
        CharacterEncodingFileter
        /*
    

8 导入静态资源

Javaweb开发一般步骤_第9张图片

你可能感兴趣的:(JavaWeb,java)