SpringStudy
- 修改为官方Druid
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
<?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>
|
||||
<parent>
|
||||
<groupId>com.linlangjz</groupId>
|
||||
<artifactId>SpringStudy</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>SpringFramework-ZY-V1</artifactId>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
</dependency>
|
||||
<!-- MySql -->
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
</dependency>
|
||||
<!-- servlet -->
|
||||
<dependency>
|
||||
<groupId>jakarta.servlet</groupId>
|
||||
<artifactId>jakarta.servlet-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
|
||||
<!-- 加载 db.properties -->
|
||||
<context:property-placeholder location="classpath:db.properties"/>
|
||||
|
||||
<!-- Druid 数据源 -->
|
||||
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
|
||||
init-method="init" destroy-method="close">
|
||||
<property name="driverClassName" value="${db.driver}"/>
|
||||
<property name="url" value="${db.url}"/>
|
||||
<property name="username" value="${db.username}"/>
|
||||
<property name="password" value="${db.password}"/>
|
||||
|
||||
<property name="initialSize" value="${druid.initialSize}"/>
|
||||
<property name="minIdle" value="${druid.minIdle}"/>
|
||||
<property name="maxActive" value="${druid.maxActive}"/>
|
||||
<property name="maxWait" value="${druid.maxWait}"/>
|
||||
<property name="timeBetweenEvictionRunsMillis" value="${druid.timeBetweenEvictionRunsMillis}"/>
|
||||
<property name="minEvictableIdleTimeMillis" value="${druid.minEvictableIdleTimeMillis}"/>
|
||||
<property name="validationQuery" value="${druid.validationQuery}"/>
|
||||
<property name="testWhileIdle" value="${druid.testWhileIdle}"/>
|
||||
<property name="testOnBorrow" value="${druid.testOnBorrow}"/>
|
||||
<property name="testOnReturn" value="${druid.testOnReturn}"/>
|
||||
<property name="poolPreparedStatements" value="${druid.poolPreparedStatements}"/>
|
||||
<property name="maxPoolPreparedStatementPerConnectionSize"
|
||||
value="${druid.maxPoolPreparedStatementPerConnectionSize}"/>
|
||||
<property name="filters" value="${druid.filters}"/>
|
||||
</bean>
|
||||
|
||||
<!-- DAO -->
|
||||
<bean id="selectData" class="com.ycjy.DAO.SelectData">
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
</bean>
|
||||
|
||||
<!-- Service -->
|
||||
<bean id="deptService" class="com.ycjy.Service.DeptService">
|
||||
<property name="selectData" ref="selectData"/>
|
||||
</bean>
|
||||
</beans>
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user