下面将通过 Spring 配置文件 application-context.xml 配置 Druid 数据源(com.alibaba.druid.pool.DruidDataSource),然后将它作为 JdbcTemplate 的数据源。客户端程序通过 JdbcTemplate 实例获取数据库当前的时间戳。
JDK版本:1.8
Spring版本:5.3.22
MySQL版本:5.7.*
将下面依赖添加到 pom.xml 文件中:
<!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.15</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.22</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.22</version> </dependency> <!-- 数据库驱动依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency>
在项目 resources 目录下面创建名为 application-context.xml 的配置文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" lazy-init="false"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false" /> <property name="username" value="root" /> <property name="password" value="aaaaaa" /> <property name="initialSize" value="1" /> <property name="maxActive" value="50" /> <property name="maxIdle" value="0" /> <property name="maxWait" value="30000" /> <property name="filters" value="stat,wall" /> <property name="timeBetweenEvictionRunsMillis" value="3000" /> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="validationQuery" value="SELECT 'x'" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
package com.hxstrive.jdbc_pool.druid; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.JdbcTemplate; import java.sql.Timestamp; /** * 配置 Druid 为 JdbcTemplate 的数据源,利用 JdbcTemplate 获取数据库当前时间戳。 * @author hxstrive.com */ public class DruidHello { public static void main(String[] args) { // 1.从 classpath 中加载配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "application-context.xml"); // 2.获取 JdbcTemplate 实例 Bean JdbcTemplate jdbcTemplate = applicationContext.getBean(JdbcTemplate.class); System.out.println("jdbcTemplate = " + jdbcTemplate); // 3.获取数据库当前的时间戳 Timestamp ts = jdbcTemplate.queryForObject("select CURRENT_TIMESTAMP() as ct", Timestamp.class); System.out.println(ts); } }
运行代码,输出如下:
jdbcTemplate = org.springframework.jdbc.core.JdbcTemplate@2eda0940 2023-07-11 12:40:00.0