核心
- 通过将数据库连接池和jdbc模板的配置交给Spring来管理的过程中,我们可以深刻体会到应用SpringIOC(控制反转)和DI(依赖注入)的好处!简化开发,维护方便!
- 对于连接池我们使用了xml方式对其进行注册和依赖注入,之所以不使用注解的方式,是因为该连接池不是我们自己定义的,属于源码!切记对于注解和xml方式我们要针对不同场景灵活使用!
附录一:使用Spring进行JDBC访问的核心配置文件
项目地址:
- applicationContext.xml:
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置数据库连接池===================== -->
<!-- 第一种:配置Spring内置的数据库连接池-->
<!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
属性注入
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean> -->
<!-- 第二种:配置DBCP数据库连接池 -->
<!-- <bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean> -->
<!-- 第三种:配置C3P0数据库连接池 -->
<!-- <bean id = "dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"/>
<property name="user" value="root"/>
<property name="password" value="123456"/>
</bean> -->
<!-- 第四种:引入外部属性文件【实际开发使用】 -->
<!-- 1.通过一个bean标签引入(很少使用) -->
<!-- <bean class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"/>
</bean> -->
<!-- 2.通过context标签引入自动加载 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id = "dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 配置Spring的JDBC的模板 -->
<bean id = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
附录二:使用Spring进行JDBC访问的外部引入文件
项目地址:
- jdbc.properties:
jdbc.driverClass = com.mysql.jdbc.Driver
jdbc.url= jdbc:mysql://localhost:3306/spring
jdbc.username = root
jdbc.password = 123456