数据源就是连接池,就是不用创建连接池对象了,让Spring给咱们创建。
手动创建数据源
第一步:先导入依赖坐标,导入C3P0坐标和druid坐标,还有就是数据库驱动坐标,还有一个单元测试坐标
<dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.41</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.9</version> </dependency>
第二步:手动创建C3p0连接池,手动创建druid连接池
@Test public void testC3P0() throws Exception { ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setDriverClass("com.mysql.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test"); dataSource.setUser("root"); dataSource.setPassword("123456"); Connection connection = dataSource.getConnection(); System.out.println("C3P0获取到的连接地址" + connection); connection.close(); } @Test public void testDruid() throws Exception { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/mysql"); dataSource.setUsername("root"); dataSource.setPassword("123456"); DruidPooledConnection connection = dataSource.getConnection(); System.out.println("druid获取到的连接地址" + connection); connection.close(); }
可以将字符串提取出来,写到配置文件中。
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test jdbc.username=root jdbc.password=123456
@Test public void testDruid1() throws Exception { // 加载类路径下的jdbc.properties ResourceBundle rb = ResourceBundle.getBundle("jdbc"); DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(rb.getString("jdbc.driver")); dataSource.setUrl(rb.getString("jdbc.url")); dataSource.setUsername(rb.getString("jdbc.username")); dataSource.setPassword(rb.getString("jdbc.password")); DruidPooledConnection connection = dataSource.getConnection(); System.out.println("druid获取到的连接地址" + connection); connection.close(); }
ComboPooledDataSource 对象也是可以使用Spring创建。
导入Spring坐标
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.4.RELEASE</version> </dependency>
配置applicationContext.xml文件
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property> <property name="user" value="root"></property> <property name="password" value="123456"></property> </bean>
写代码
@Test public void testC3p0() throws SQLException { ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); ComboPooledDataSource dataSource = (ComboPooledDataSource) app.getBean("dataSource"); Connection connection = dataSource.getConnection(); System.out.println(connection); connection.close(); }
同时可以将数据抽出来。
<?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"> <!--<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property> <property name="user" value="root"></property> <property name="password" value="123456"></property> </bean> --> <context:property-placeholder location="jdbc.properties"></context:property-placeholder> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> </beans>