前面介绍了 Spring 怎样集成 BoneCP,下面将通过一个简单示例演示 BoneCP 和 Spring Boot 集成,然后通过 JdbcTemplate 从 MySQL 5.7.* 中获取数据库时间戳。
Spring Boot 项目的依赖如下:
<?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>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.13</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.hxstrive.jdbc_pool</groupId> <artifactId>bonecp_spring_boot</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- 数据源 BoneCP 依赖 --> <!-- https://mvnrepository.com/artifact/com.jolbox/bonecp --> <dependency> <groupId>com.jolbox</groupId> <artifactId>bonecp</artifactId> <version>0.8.0.RELEASE</version> </dependency> <!-- MySQL 驱动依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!-- Spring Boot JDBC依赖驱动 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- Spring Boot Web 驱动依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot 开发和测试依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
使用 @Configuration 声明一个配置类,配置 JdbcTemplate 和 DataSource(数据源),代码如下:
package com.hxstrive.jdbc_pool.bonecp.config; import com.jolbox.bonecp.BoneCPDataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.core.JdbcTemplate; import javax.sql.DataSource; /** * JdbTemplate 配置 * @author hxstrive.com */ @Configuration public class JdbcTemplateConfig { /** * 配置 JdbcTemplate * @param dataSource 数据源 * @return */ @Bean public JdbcTemplate getJdbcTemplate(DataSource dataSource) { return new JdbcTemplate(dataSource); } /** * 配置数据源 * @return 返回 com.jolbox.bonecp.BoneCPDataSource 数据源 */ @Bean public DataSource getDataSource() { BoneCPDataSource dataSource = new BoneCPDataSource(); // 数据库驱动 dataSource.setDriverClass("com.mysql.jdbc.Driver"); // 相应驱动的jdbcUrl,你懂的 dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test?useSSL=false"); // 数据库的用户名 dataSource.setUsername("root"); // 数据库的密码 dataSource.setPassword("aaaaaa"); // 每个分区最大的连接数 dataSource.setMaxConnectionsPerPartition(200); // 每个分区最小的连接数 dataSource.setMinConnectionsPerPartition(5); // 分区数 ,默认值2,最小1,推荐3-4,视应用而定 dataSource.setPartitionCount(4); // 每次去拿数据库连接的时候一次性要拿几个,默认值:2 dataSource.setAcquireIncrement(2); // 缓存prepared statements的大小,默认值:0 dataSource.setStatementsCacheSize(10); return dataSource; } }
Spring Boot 项目的启动类,代码如下:
package com.hxstrive.jdbc_pool.bonecp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 系统入口 * @author hxstrive.com */ @SpringBootApplication public class BoneCpHello { public static void main(String[] args) { SpringApplication.run(BoneCpHello.class, args); } }
通过 @SpringBootTest 注解声明一个测试类,通过单元测试进行测试,代码如下:
package com.hxstrive.jdbc_pool.bonecp; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.jdbc.core.JdbcTemplate; import java.sql.Timestamp; /** * 测试类 * @author hxstrive.com */ @SpringBootTest public class BoneCpHelloTest { @Autowired private JdbcTemplate jdbcTemplate; @Test public void contexts() { // 获取数据库当前的时间戳 Timestamp ts = jdbcTemplate.queryForObject("select CURRENT_TIMESTAMP() as ct", Timestamp.class); System.out.println(ts); } }
运行测试类,输出如下:
... 省略 ... . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.7.13) 2023-07-03 13:03:16.724 INFO 9904 --- [ main] c.h.jdbc_pool.bonecp.BoneCpHelloTest : Starting BoneCpHelloTest using Java 1.8.0_45 on hxstrive with PID 9904 (started by Administrator in D:\~my_workspace\jdbc_pool\bonecp_spring_boot) 2023-07-03 13:03:16.727 INFO 9904 --- [ main] c.h.jdbc_pool.bonecp.BoneCpHelloTest : No active profile set, falling back to 1 default profile: "default" 2023-07-03 13:03:21.907 INFO 9904 --- [ main] c.h.jdbc_pool.bonecp.BoneCpHelloTest : Started BoneCpHelloTest in 5.849 seconds (JVM running for 9.301) # 看这里,这是结果 2023-07-03 13:03:23.0 2023-07-03 13:03:23.504 INFO 9904 --- [ionShutdownHook] com.jolbox.bonecp.BoneCP : Shutting down connection pool... 2023-07-03 13:03:23.520 INFO 9904 --- [ionShutdownHook] com.jolbox.bonecp.BoneCP : Connection pool has been shutdown.