首先,在 applicationContext.xml 核心配置文件中需要用到 context 命名空间,
然后 使用 标签来指定导入的资源属性文件 db.properties 的位置就OK
1. 属性文件: db.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test_spring5?useUnicode=true&characterEncoding=utf8&useSSL=true
jdbc.user=root
jdbc.password=123456
c3p0.initialPoolSize=5
c3p0.acquireIncrement=5
c3p0.maxPoolSize=20
c3p0.minPoolSize=5
c3p0.maxStatements=200
c3p0.maxStatementsPerConnection=5
2. 核心配置文件:applicationContext.xml
3. 测试类:
@Test
public void test() throws SQLException {
// 1.初始化ioc容器(装对象的容器)
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2. javax.sql.DataSource;
DataSource dataSource = (DataSource) context.getBean("dataSource");
System.out.println(dataSource.getConnection());
}
----
com.mchange.v2.c3p0.impl.NewProxyConnection@7d3a22a9 [wrapping: com.mysql.jdbc.JDBC4Connection@1d082e88]
c3p0配置参数参考文章:https://www.cnblogs.com/JavaSubin/p/5294721.html
使用其他连接池,比如:DBCP/Druid。
其实都是一样的原理,在Java中,它们都继承 javax.sql.DataSource接口。
可参考我的这篇文章:Java JDBC使用连接池 JDBC 连接池(DBCP/Druid) 。也会很快掌握Spring使用DBCP/Druid,这里就没不用演示了。
ends ~