SSM2.1【Spring:Spring配置数据源】
数据源(数据库连接池)的作用
数据源的开发步骤
数据源的手动创建 VS Spring配置数据源
1 | 虽然将数据库基本连接信息配置到applicationContext.xml中已经完成解耦合,但是实际开发中方便数据库的管理,同样会将数据库的基本连接信息单独抽取到一个properties文件中 |
代码
pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.haifei</groupId> 8 <artifactId>SSM2_spring_ioc_anno</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 12 <dependencies> 13 <dependency> 14 <groupId>org.springframework</groupId> 15 <artifactId>spring-context</artifactId> 16 <version>5.0.5.RELEASE</version> 17 </dependency> 18 <dependency> 19 <groupId>junit</groupId> 20 <artifactId>junit</artifactId> 21 <version>4.11</version> 22 </dependency> 23 <dependency> 24 <groupId>mysql</groupId> 25 <artifactId>mysql-connector-java</artifactId> 26 <version>5.1.26</version> 27 </dependency> 28 <dependency> 29 <groupId>c3p0</groupId> 30 <artifactId>c3p0</artifactId> 31 <version>0.9.1.2</version> 32 </dependency> 33 <dependency> 34 <groupId>com.alibaba</groupId> 35 <artifactId>druid</artifactId> 36 <version>1.0.9</version> 37 </dependency> 38 </dependencies> 39 40 41 <build> 42 <plugins> 43 <plugin> 44 <groupId>org.apache.maven.plugins</groupId> 45 <artifactId>maven-compiler-plugin</artifactId> 46 <version>3.1</version> 47 <configuration> 48 <target>1.8</target> 49 <source>1.8</source> 50 </configuration> 51 </plugin> 52 </plugins> 53 </build> 54 55 56 </project>
jdbc.properties
1 jdbc.driver=com.mysql.jdbc.Driver 2 jdbc.url=jdbc:mysql://localhost:3306/test 3 jdbc.username=root 4 jdbc.password=root
applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 8 9 10 <bean id="dataSource_c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 11 <property name="driverClass" value="com.mysql.jdbc.Driver"></property> 12 <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property> 13 <property name="user" value="root"></property> 14 <property name="password" value="root"></property> 15 </bean> 16 17 <!--加载外部的properties文件--> 18 <!--需要在顶部beans中配置命名空间context,并在xsi中添加对应的约束路径--> 19 <context:property-placeholder location="classpath:jdbc.properties"/> 20 <bean id="dataSource_druid" class="com.alibaba.druid.pool.DruidDataSource"> 21 <property name="driverClassName" value="${jdbc.driver}"></property> 22 <property name="url" value="${jdbc.url}"></property> 23 <property name="username" value="${jdbc.username}"></property> 24 <property name="password" value="${jdbc.password}"></property> 25 </bean> 26 27 28 </beans>
DataSourceTest.java
1 package com.haifei.test; 2 3 import com.alibaba.druid.pool.DruidDataSource; 4 import com.mchange.v2.c3p0.ComboPooledDataSource; 5 import org.junit.Test; 6 import org.springframework.context.ApplicationContext; 7 import org.springframework.context.support.ClassPathXmlApplicationContext; 8 9 import javax.sql.DataSource; 10 import java.beans.PropertyVetoException; 11 import java.sql.Connection; 12 import java.util.ResourceBundle; 13 14 public class DataSourceTest { 15 16 /** 17 * 手动创建c3p0数据源(数据库连接池) 18 */ 19 @Test 20 public void test1() throws Exception { 21 ComboPooledDataSource dataSource = new ComboPooledDataSource(); 22 dataSource.setDriverClass("com.mysql.jdbc.Driver"); 23 dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test"); 24 dataSource.setUser("root"); 25 dataSource.setPassword("root"); 26 27 Connection connection = dataSource.getConnection(); 28 System.out.println(connection); //com.mchange.v2.c3p0.impl.NewProxyConnection@129a8472 29 connection.close(); //归还连接到连接池中 30 } 31 32 33 /** 34 * 手动创建druid数据源 35 */ 36 @Test 37 public void test2() throws Exception { 38 DruidDataSource dataSource = new DruidDataSource(); 39 dataSource.setDriverClassName("com.mysql.jdbc.Driver"); 40 dataSource.setUrl("jdbc:mysql://localhost:3306/test"); 41 dataSource.setUsername("root"); 42 dataSource.setPassword("root"); 43 44 Connection connection = dataSource.getConnection(); 45 System.out.println(connection); //com.mysql.jdbc.JDBC4Connection@2471cca7 46 connection.close(); 47 } 48 49 50 /** 51 * 手动创建c3p0数据源(加载properties配置文件方式) 52 * 抽取为配置文件,方便解耦 53 */ 54 @Test 55 public void test3() throws Exception { 56 //读取配置文件 57 ResourceBundle rb = ResourceBundle.getBundle("jdbc"); //该方法专门读取.properties文件,参数仅传入文件名即可 58 String driver = rb.getString("jdbc.driver"); 59 String url = rb.getString("jdbc.url"); 60 String username = rb.getString("jdbc.username"); 61 String password = rb.getString("jdbc.password"); 62 // System.out.println(driver + "--" + url + "--" + username + "--" + password); //com.mysql.jdbc.Driver--jdbc:mysql://localhost:3306/test--root--root 63 64 //创建数据源对象,设置连接参数 65 ComboPooledDataSource dataSource = new ComboPooledDataSource(); 66 dataSource.setDriverClass(driver); 67 dataSource.setJdbcUrl(url); 68 dataSource.setUser(username); 69 dataSource.setPassword(password); 70 /* 71 Spring依赖注入(配置Bean) 72 bean的class值来源:右键ComboPooledDataSource,copy referrnce 得到其全路径 73 属性property的name值来源:setXyyy --> Xyyy --> xyyy 74 */ 75 76 Connection connection = dataSource.getConnection(); 77 System.out.println(connection); //com.mchange.v2.c3p0.impl.NewProxyConnection@1b0375b3 78 connection.close(); 79 } 80 81 82 /** 83 * 手动创建druid数据源(加载properties配置文件方式) 84 * 抽取为配置文件,方便解耦 85 */ 86 @Test 87 public void test4() throws Exception { 88 ResourceBundle rb = ResourceBundle.getBundle("jdbc"); 89 String driver = rb.getString("jdbc.driver"); 90 String url = rb.getString("jdbc.url"); 91 String username = rb.getString("jdbc.username"); 92 String password = rb.getString("jdbc.password"); 93 94 DruidDataSource dataSource = new DruidDataSource(); 95 dataSource.setDriverClassName(driver); 96 dataSource.setUrl(url); 97 dataSource.setUsername(username); 98 dataSource.setPassword(password); 99 100 Connection connection = dataSource.getConnection(); 101 System.out.println(connection); //com.mysql.jdbc.JDBC4Connection@5fe5c6f 102 connection.close(); 103 } 104 105 106 /** 107 * Spring容器产生c3p0数据源对象 108 */ 109 @Test 110 public void test5() throws Exception { 111 ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); 112 113 // ComboPooledDataSource dataSource = (ComboPooledDataSource)app.getBean("dataSource_c3p0"); 114 // DataSource dataSource = (DataSource)app.getBean("dataSource_c3p0"); 115 116 //因为此时spring配置文件只配置了一个class="com.mchange.v2.c3p0.ComboPooledDataSource",所以该可以: 117 ComboPooledDataSource dataSource = app.getBean(ComboPooledDataSource.class); 118 // DataSource dataSource = app.getBean(DataSource.class); //报错 119 120 Connection connection = dataSource.getConnection(); 121 System.out.println(connection); 122 connection.close(); 123 } 124 125 126 /** 127 * Spring容器产生druid数据源对象 128 * 采用加载外部的properties文件的方式配置bean 129 */ 130 @Test 131 public void test6() throws Exception { 132 ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); 133 134 // DruidDataSource dataSource = (DruidDataSource)app.getBean("dataSource_druid"); 135 DruidDataSource dataSource = app.getBean(DruidDataSource.class); 136 137 Connection connection = dataSource.getConnection(); 138 System.out.println(connection); 139 connection.close(); 140 } 141 142 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!