SMBMS項目-準備工作

項目搭建準備工作

1、基礎準備工作

  • 搭建一-個maven web項目
  • 配置Tomcat
  • 測試項目是否能夠跑起來
  • 導入項目中會遇到的jar包
    • jsp,Servlet,mysq|驅動, jstl, stand..

2、創建項目結構

image-20200520151730274

3、編寫實體類;

  • ORM映射:表-類映射,pojo類

image-20200520151848908

4、編寫基礎公共類

4.1資料庫配置文件db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306?/smbms?useUnicode=true&characterEncoding=utf-8&serverTimeZone=GMT
user=root
password=123456
4.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 execute(Connection connection,String sql,Object[] params,ResultSet resultSet,PreparedStatement preparedStatement) 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){
        boolean flag=true;
        if (resultSet!=null){
            try {
                resultSet.close();
                resultSet=null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                flag=false;
            }
        }
        if (preparedStatement!=null){
            try {
                preparedStatement.close();
                preparedStatement=null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                flag=false;
            }
        }
        if (connection!=null){
            try {
                connection.close();
                connection=null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                flag=false;
            }
        }
        return false;
    }
}
4.3編寫字元編碼過濾器,並在web.xml中註冊
public class CharacterEncodingFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        chain.doFilter(req,resp);
    }

    public void destroy() {

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

5、導入靜態資源

image-20200520152823186

後續正在學習中。。。

if(隨筆.hasnext()){
    response.sendRedirect("/下一篇隨筆");
}else{
    Systom.out.println("菜雞兒作者正在努力學習中,請稍後");
    reader.wait();
}

image-20200520183320241

Tags: