目录
4.3抽取jdbc.properties配置文件 配置数据源
4.1数据源的开发步骤
1)导入数据源的坐标和对应的数据库驱动坐标
2)创建数据源对象
3)设置数据源的基本连接数据
4)使用数据源获取连接资源和关闭连接资源
4.2手动创建数据源
在创建之前先导入数据源的坐标
<dependencies> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.17</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> </dependencies>
手动配置druid、c3p0数据源
package com.hzw.test; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidPooledConnection; import com.mchange.v2.c3p0.ComboPooledDataSource; import org.junit.Test; import java.beans.PropertyVetoException; import java.sql.Connection; import java.sql.SQLException; public class DataSourceTest { //测试手动设置druid数据源 @Test public void test2() throws SQLException { DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); druidDataSource.setUrl("jdbc:mysql://localhost:3306/cloud-user?useSSL=false&serverTimezone=Asia/Shanghai"); druidDataSource.setUsername("root"); druidDataSource.setPassword("root"); DruidPooledConnection connection = druidDataSource.getConnection(); System.out.println(connection); } //手动创建c3p0数据源 @Test public void test1() throws Exception { ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setDriverClass("com.mysql.cj.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/cloud-user?useSSL=false&serverTimezone=Asia/Shanghai"); dataSource.setUser("root"); dataSource.setPassword("root"); //获取数据源 Connection connection = dataSource.getConnection(); System.out.println(connection); connection.close(); } }
4.3抽取jdbc.properties配置文件 配置数据源
jdbc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/cloud-user?useSSL=false&serverTimezone=Asia/Shanghai jdbc.username=root jdbc.password=root
//手动创建c3p0数据源(加载propertise配置文件) @Test public void test3() throws Exception { //读取配置文件 ResourceBundle rb=ResourceBundle.getBundle("jdbc"); String driver = rb.getString("jdbc.driver"); String url = rb.getString("jdbc.url"); String username = rb.getString("jdbc.username"); String password = rb.getString("jdbc.password"); //创建数据源 设置连接数 ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setDriverClass(driver); dataSource.setJdbcUrl(url); dataSource.setPassword(password); dataSource.setUser(username); Connection connection = dataSource.getConnection(); System.out.println(connection); connection.close();
4.4Spring配置数据源
1.引入对应的依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.5.RELEASE</version> </dependency>
2.配置对应Bean 这里是c3p0数据源
<?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.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/cloud-user?serverTimezone=Asia/Shanghai"></property> <property name="password" value="root"></property> <property name="user" value="root"></property> </bean> </beans>
3.getBean 获取对应数据源对象
//测试Spring容器创建c3p0数据源 @Test public void test4() throws Exception { ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); DataSource dataSource = applicationContext.getBean(DataSource.class); Connection connection = dataSource.getConnection(); System.out.println(connection); connection.close(); }
4.抽取jdbc配置文件
applicatinContext.xml加载jdbc.properties配置文件获取连接信息
首先,需要引入context命名空间阿约束路径
<?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"> <!--加载外部的properties文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--通过key 加载值--> <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="password" value="${jdbc.password}"></property> <property name="user" value="${jdbc.username}"></property> </bean> </beans>
4.测试连接成功