一、使用Druid实现数据库连接
使用Druid作为Spring的数据源对象管理是一种常见的方式,因为Druid提供了强大的连接池和监控功能,可以更好地管理和优化数据库连接。
下面是一个简单的Spring数据源对象管理使用Druid实现的示例:
首先,在Spring配置文件中配置Druid数据源对象:
<!--pom.xml配置依赖-->
<!--druid数据库连接-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
然后在Spring配置文件中配置Druid数据源,例如在application.xml中进行配置:
<!--druid,使用set注入的方式-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/book"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
然后在App中编写测试代码
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
System.out.println(dataSource);
二、使用c3p0实现数据库连接
当使用Spring进行数据源对象管理时,还可以使用c3p0作为数据源,c3p0是开源的JDBC连接池,具有高性能、可靠性和稳定性。下面是如何在Spring中使用c3p0作为数据源的步骤:
1.首先,在pom.xml文件中添加c3p0依赖:
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.4</version>
</dependency>
2.在Spring配置文件中配置c3p0数据源,例如在application.xml中进行配置:
<!--c3p0-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="dataSourceName" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/book"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
3.最后在app中获取bean测试输出
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
System.out.println(dataSource);
三、加载properties文件
在Spring应用程序中,可以将数据库配置信息放在jdbc.properties文件中,并通过在Spring配置文件中引用此文件来实现连接数据库。以下是具体的步骤:
-
在项目的src/main/resources目录下创建一个名为jdbc.properties的文件。
-
在jdbc.properties中设置数据库连接参数,例如:
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/mydatabase
jdbc.user=root
jdbc.password=password
- 在Spring配置文件中使用application.xml标签加载jdbc.properties文件,并设置属性占位符的前缀和后缀,
- 修改xml的头部文件例如:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
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-4.2.xsd">
3.往application.xml加入property-placeholder去加载properties,在application.xml
<context:property-placeholder location="classpath:jdbc.properties"/>
4.使用${}加载需要的参数,在application.xml
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="dataSourceName" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
5.测试
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
System.out.println(dataSource);