日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

JavaWeb 自制基础开发框架准备环境搭建

發(fā)布時間:2023/12/31 java 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JavaWeb 自制基础开发框架准备环境搭建 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

目錄

一、基本目錄結(jié)構(gòu)

1、創(chuàng)建webapp的maven項目

2、配置web.xml項目

3、創(chuàng)建目錄結(jié)構(gòu):

4、添加數(shù)據(jù)庫配置文件

二、數(shù)據(jù)庫模塊配置:

1、連接數(shù)據(jù)庫

2、 寫入每個表對應的實體類

?3、編寫數(shù)據(jù)庫操作的基類

三、添加編碼過濾器


一、基本目錄結(jié)構(gòu)

1、創(chuàng)建webapp的maven項目

配置pom.xml的一些常用web依賴:

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.wxl</groupId><artifactId>smbms</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><properties><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>4.0.1</version></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>javax.servlet.jsp-api</artifactId><version>2.3.3</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.27</version></dependency><dependency><groupId>javax.servlet.jsp.jstl</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><dependency><groupId>org.apache.taglibs</groupId><artifactId>taglibs-standard-impl</artifactId><version>1.2.5</version></dependency></properties> </project>

2、配置web.xml項目

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0" metadata-complete="true"><!--字符編碼過濾器--><filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>com.wxl.filter.CharacterEncodingFilter</filter-class></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping> </web-app>

3、創(chuàng)建目錄結(jié)構(gòu):

4、添加數(shù)據(jù)庫配置文件

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

?

二、數(shù)據(jù)庫模塊配置:

1、連接數(shù)據(jù)庫

2、 寫入每個表對應的實體類

?3、編寫數(shù)據(jù)庫操作的基類

package com.wxl.dao;import java.io.IOException; import java.io.InputStream; import java.sql.*; import java.util.Properties;//操作數(shù)據(jù)庫的公共類 public class BaseDao {private static String driver;private static String url;private static String username;private static String password;//靜態(tài)代碼塊,類加載的時候就初始化了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");}//獲取數(shù)據(jù)庫的鏈接public static Connection getConnection() {Connection connection = null;try {Class.forName(driver);connection = DriverManager.getConnection(url, username, password);} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException throwables) {throwables.printStackTrace();}return connection;}//編寫查詢公共類public static ResultSet execute(Connection connection, String sql, Object[] params, ResultSet resultSet, PreparedStatement preparedStatement) throws SQLException {//預編譯的sql,在后面直接執(zhí)行就可以了preparedStatement = connection.prepareStatement(sql);for (int i = 0; i < params.length; i++) {//setObject,占位符從1開始,但是我們的數(shù)組是從0開始!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++) {//setObject,占位符從1開始,但是我們的數(shù)組是從0開始!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 flag;} }

三、添加編碼過濾器

package com.wxl.filter;import javax.servlet.*; import java.io.IOException;public class CharacterEncodingFilter implements Filter{public void init(FilterConfig filterConfig) throws ServletException {}public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {servletRequest.setCharacterEncoding("utf-8");servletResponse.setCharacterEncoding("utf-8");filterChain.doFilter(servletRequest,servletResponse);}public void destroy() {} }

這里注意導包路徑!

前面已經(jīng)在web.xml里進行了注冊。

一個干凈,具有基礎功能的框架制作完成。

總結(jié)

以上是生活随笔為你收集整理的JavaWeb 自制基础开发框架准备环境搭建的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。