From e0639d90c4e988d7bc0950b582625d1e0ff969cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E9=9C=96=E9=BA=92?= Date: Mon, 28 Sep 2026 08:54:31 +0800 Subject: [PATCH] =?UTF-8?q?SpringStudy=20-=20=E4=BF=AE=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E5=AE=98=E6=96=B9Druid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SpringFramework-ZY-V1/pom.xml | 36 +++++++++++++ .../main/java/com/ycjy/DAO/SelectData.java | 34 ++++++++++++ .../main/java/com/ycjy/Entity/DeptEntity.java | 31 +++++++++++ .../src/main/java/com/ycjy/MainServlet.java | 49 +++++++++++++++++ .../java/com/ycjy/Service/DeptService.java | 18 +++++++ .../src/main/java/com/ycjy/Util/DBUtil.java | 52 +++++++++++++++++++ .../src/main/resources/application-config.xml | 47 +++++++++++++++++ .../src/main/resources/db.properties | 23 ++++++++ 8 files changed, 290 insertions(+) create mode 100644 SpringFramework-ZY-V1/pom.xml create mode 100644 SpringFramework-ZY-V1/src/main/java/com/ycjy/DAO/SelectData.java create mode 100644 SpringFramework-ZY-V1/src/main/java/com/ycjy/Entity/DeptEntity.java create mode 100644 SpringFramework-ZY-V1/src/main/java/com/ycjy/MainServlet.java create mode 100644 SpringFramework-ZY-V1/src/main/java/com/ycjy/Service/DeptService.java create mode 100644 SpringFramework-ZY-V1/src/main/java/com/ycjy/Util/DBUtil.java create mode 100644 SpringFramework-ZY-V1/src/main/resources/application-config.xml create mode 100644 SpringFramework-ZY-V1/src/main/resources/db.properties diff --git a/SpringFramework-ZY-V1/pom.xml b/SpringFramework-ZY-V1/pom.xml new file mode 100644 index 0000000..1615104 --- /dev/null +++ b/SpringFramework-ZY-V1/pom.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + + com.linlangjz + SpringStudy + 1.0-SNAPSHOT + + + SpringFramework-ZY-V1 + war + + + + org.springframework + spring-context + + + + mysql + mysql-connector-java + + + + jakarta.servlet + jakarta.servlet-api + provided + + + com.alibaba + druid + + + \ No newline at end of file diff --git a/SpringFramework-ZY-V1/src/main/java/com/ycjy/DAO/SelectData.java b/SpringFramework-ZY-V1/src/main/java/com/ycjy/DAO/SelectData.java new file mode 100644 index 0000000..6b6aaf3 --- /dev/null +++ b/SpringFramework-ZY-V1/src/main/java/com/ycjy/DAO/SelectData.java @@ -0,0 +1,34 @@ +package com.ycjy.DAO; + +import com.ycjy.Entity.DeptEntity; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class SelectData { + private DataSource dataSource; + + public void setDataSource(DataSource dataSource) { + this.dataSource = dataSource; + } + + public DeptEntity selectDeptNameById(Integer id) throws SQLException { + String sql = "select id,deptname from dept where id = ?"; + try (Connection connection = dataSource.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setInt(1, id); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + if (resultSet.next()) { + DeptEntity deptEntity = new DeptEntity(); + deptEntity.setDeptID(resultSet.getInt("id")); + deptEntity.setDeptName(resultSet.getString("deptname")); + return deptEntity; + } + return null; + } + } + } +} \ No newline at end of file diff --git a/SpringFramework-ZY-V1/src/main/java/com/ycjy/Entity/DeptEntity.java b/SpringFramework-ZY-V1/src/main/java/com/ycjy/Entity/DeptEntity.java new file mode 100644 index 0000000..5b012ab --- /dev/null +++ b/SpringFramework-ZY-V1/src/main/java/com/ycjy/Entity/DeptEntity.java @@ -0,0 +1,31 @@ +package com.ycjy.Entity; + +// 部门实体 +public class DeptEntity { + private Integer deptID; + private String deptName; + + public Integer getDeptID() { + return deptID; + } + + public void setDeptID(Integer deptID) { + this.deptID = deptID; + } + + public String getDeptName() { + return deptName; + } + + public void setDeptName(String deptName) { + this.deptName = deptName; + } + + @Override + public String toString() { + return "DeptEntity{" + + "deptID='" + deptID + '\'' + + ", deptName='" + deptName + '\'' + + '}'; + } +} \ No newline at end of file diff --git a/SpringFramework-ZY-V1/src/main/java/com/ycjy/MainServlet.java b/SpringFramework-ZY-V1/src/main/java/com/ycjy/MainServlet.java new file mode 100644 index 0000000..e93424d --- /dev/null +++ b/SpringFramework-ZY-V1/src/main/java/com/ycjy/MainServlet.java @@ -0,0 +1,49 @@ +package com.ycjy; + +import com.ycjy.Entity.DeptEntity; +import com.ycjy.Service.DeptService; +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.sql.SQLException; + +@WebServlet("/") +public class MainServlet extends HttpServlet { + private DeptService deptService; + + @Override + public void init() throws ServletException { + ApplicationContext ctx = new ClassPathXmlApplicationContext("application-config.xml"); + this.deptService = (DeptService) ctx.getBean("deptService"); + } + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { + setType(req, resp); + printDept(req, resp); + } + + // 查询部门并输出 + private void printDept(HttpServletRequest req, HttpServletResponse resp) throws IOException { + Integer id = Integer.valueOf(req.getParameter("id")); + try { + DeptEntity result = deptService.select(id); + resp.getWriter().print(result); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + // 设置字符编码 + private static void setType(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException { + req.setCharacterEncoding("UTF-8"); + resp.setContentType("application/json;charset=UTF-8"); + } +} diff --git a/SpringFramework-ZY-V1/src/main/java/com/ycjy/Service/DeptService.java b/SpringFramework-ZY-V1/src/main/java/com/ycjy/Service/DeptService.java new file mode 100644 index 0000000..4fdd5a1 --- /dev/null +++ b/SpringFramework-ZY-V1/src/main/java/com/ycjy/Service/DeptService.java @@ -0,0 +1,18 @@ +package com.ycjy.Service; + +import com.ycjy.DAO.SelectData; +import com.ycjy.Entity.DeptEntity; + +import java.sql.SQLException; + +public class DeptService { + private SelectData selectData; + + public void setSelectData(SelectData selectData) { + this.selectData = selectData; + } + + public DeptEntity select(Integer id) throws SQLException { + return selectData.selectDeptNameById(id); + } +} \ No newline at end of file diff --git a/SpringFramework-ZY-V1/src/main/java/com/ycjy/Util/DBUtil.java b/SpringFramework-ZY-V1/src/main/java/com/ycjy/Util/DBUtil.java new file mode 100644 index 0000000..f6aac07 --- /dev/null +++ b/SpringFramework-ZY-V1/src/main/java/com/ycjy/Util/DBUtil.java @@ -0,0 +1,52 @@ +package com.ycjy.Util; + +import com.alibaba.druid.pool.DruidDataSource; + +import java.io.InputStream; +import java.util.Properties; + +// 数据库连接工具类 +public class DBUtil { + private static DruidDataSource dataSource; + + static { + try { + // 加载配置文件 + Properties properties = new Properties(); + InputStream is = DBUtil.class.getClassLoader().getResourceAsStream("db.properties"); + properties.load(is); + + // 创建 Druid 数据源 + dataSource = new DruidDataSource(); + dataSource.setDriverClassName(properties.getProperty("db.driver")); + dataSource.setUrl(properties.getProperty("db.url")); + dataSource.setUsername(properties.getProperty("db.username")); + dataSource.setPassword(properties.getProperty("db.password")); + + // 连接池参数 + dataSource.setInitialSize(Integer.parseInt(properties.getProperty("druid.initialSize", "5"))); + dataSource.setMinIdle(Integer.parseInt(properties.getProperty("druid.minIdle", "5"))); + dataSource.setMaxActive(Integer.parseInt(properties.getProperty("druid.maxActive", "20"))); + dataSource.setMaxWait(Long.parseLong(properties.getProperty("druid.maxWait", "60000"))); + dataSource.setTimeBetweenEvictionRunsMillis( + Long.parseLong(properties.getProperty("druid.timeBetweenEvictionRunsMillis", "60000"))); + dataSource.setMinEvictableIdleTimeMillis( + Long.parseLong(properties.getProperty("druid.minEvictableIdleTimeMillis", "300000"))); + dataSource.setValidationQuery(properties.getProperty("druid.validationQuery", "SELECT 1")); + dataSource.setTestWhileIdle(Boolean.parseBoolean(properties.getProperty("druid.testWhileIdle", "true"))); + dataSource.setTestOnBorrow(Boolean.parseBoolean(properties.getProperty("druid.testOnBorrow", "false"))); + dataSource.setTestOnReturn(Boolean.parseBoolean(properties.getProperty("druid.testOnReturn", "false"))); + dataSource.setPoolPreparedStatements( + Boolean.parseBoolean(properties.getProperty("druid.poolPreparedStatements", "true"))); + dataSource.setMaxPoolPreparedStatementPerConnectionSize( + Integer.parseInt(properties.getProperty("druid.maxPoolPreparedStatementPerConnectionSize", "20"))); + + // 监控统计拦截 + dataSource.setFilters(properties.getProperty("druid.filters", "stat")); + + if (is != null) is.close(); + } catch (Exception e) { + System.out.println("Druid 数据源初始化异常: " + e.getMessage()); + } + } +} \ No newline at end of file diff --git a/SpringFramework-ZY-V1/src/main/resources/application-config.xml b/SpringFramework-ZY-V1/src/main/resources/application-config.xml new file mode 100644 index 0000000..6bee2d5 --- /dev/null +++ b/SpringFramework-ZY-V1/src/main/resources/application-config.xml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/SpringFramework-ZY-V1/src/main/resources/db.properties b/SpringFramework-ZY-V1/src/main/resources/db.properties new file mode 100644 index 0000000..8ea13f2 --- /dev/null +++ b/SpringFramework-ZY-V1/src/main/resources/db.properties @@ -0,0 +1,23 @@ +# MySQL Driver +db.driver=com.mysql.cj.jdbc.Driver +# MySQL Url +db.url=jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC +# MySQL User +db.username=root +# MySQL Pwd +db.password=root +# ===== Druid ConnectPoolConfig ===== +druid.initialSize=5 +druid.minIdle=5 +druid.maxActive=20 +druid.maxWait=60000 +druid.timeBetweenEvictionRunsMillis=60000 +druid.minEvictableIdleTimeMillis=300000 +druid.validationQuery=SELECT 1 +druid.testWhileIdle=true +druid.testOnBorrow=false +druid.testOnReturn=false +druid.poolPreparedStatements=true +druid.maxPoolPreparedStatementPerConnectionSize=20 +# ?????? +druid.filters=stat \ No newline at end of file