Spring对持久层技术支持:
JDBC : org.springframework.jdbc.core.JdbcTemplate
Hibernate3.0 : org.springframework.orm.hibernate3.HibernateTemplate
IBatis(MyBatis) : org.springframework.orm.ibatis.SqlMapClientTemplate
JPA : org.springframework.orm.jpa.JpaTemplate
配置Spring默认的连接池
<!-- 配置Spring默认的连接池
导入JDBC模板开发包
spring-jdbc-3.2.0.RELEASE.jar
spring-tx-3.2.0.RELEASE.jar
数据库驱动:
mysql-connector-java-5.1.47.jar
-->
<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/info" /> <property name="username"
value="root" /> <property name="password" value="mysql123" />
</bean>
配置DBCP连接池 :
<!-- 配置DBCP连接池
加入jar:com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
和com.springsource.org.apache.commons.pool-1.5.3.jar-->
<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/info" />
<property name="username" value="root" />
<property name="password" value="mysql123" />
</bean>
配置C3P0连接池:
<!-- 配置C3P0连接池 加入jar:com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar-->
<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/info"/><!-- 这里用的是jdbcUrl -->
<property name="user" value="root" />
<property name="password" value="mysql123" />
</bean>
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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置Spring默认的连接池
导入JDBC模板开发包
spring-jdbc-3.2.0.RELEASE.jar
spring-tx-3.2.0.RELEASE.jar
数据库驱动:
mysql-connector-java-5.1.47.jar
-->
<!-- <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/info" /> <property name="username"
value="root" /> <property name="password" value="mysql123" />
</bean>
-->
<!-- 配置DBCP连接池
加入jar:com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
和com.springsource.org.apache.commons.pool-1.5.3.jar-->
<!-- <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/info" />
<property name="username" value="root" />
<property name="password" value="mysql123" />
</bean> -->
<!-- 配置C3P0连接池 加入jar:com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar-->
<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/info"/><!-- 这里用的是jdbcUrl -->
<property name="user" value="root" />
<property name="password" value="mysql123" />
</bean>
<!-- 定义JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" /><!-- 注入连接池 -->
</bean>
</beans>
- 参数设置到属性文件中:
在src下创建jdbc.properties
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql:///spring3_day02
jdbc.user = root
jdbc.password = 123
需要在applicationContext.xml 中使用属性文件配置的内容.
* 第一种写法:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property>
</bean>
* 第二种写法:
<context:property-placeholder location="classpath:jdbc.properties"/>