JavaWeb 自制基础开发框架准备环境搭建
生活随笔
收集整理的這篇文章主要介紹了
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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电芯知识汇总(转载)
- 下一篇: java美元兑换,(Java实现) 美元