Spring提供兩種方式訪問數據庫,一種是使用DataSource直接使用JDBC的方式對數據庫進行操作,獲取一個Connection,接着再獲取一個Statement執行SQL語句。另一種就是用JdbcTemplate進行一種類似Hibernate那樣的對象持久化的操作,JdbcTemplate也是在DateSource也是建立在DataSource基礎之上的,要使用DataSource進行初始化
使用DataSource訪問數據庫
DataSource的配置方式大致有三種,直接使用XML,.properties文件,使用context:property-placeholder進行配置。
直接使用XML配置
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url"
value="jdbc:sqlserver://localhost:1433;DatabaseName=spring" />
<property name="username" value="sa" />
<property name="password" value="********" />
</bean>
上述方式很直白,就是直接用XML元素來標記數據庫連接的屬性。bean元素裏的destroy-method是指dataSource中用來關閉JDBC連接的方法。有點析構函數的味道。
使用properties文件配置
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:jdbc.properties"></property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
properties文件和直接使用XML的區別就是要額外使用一個文件來存儲數據庫連接屬性,注意上述XML代碼中的第一個bean中的locations屬性的值中的classpath表示src目錄,所以jdbc.properties這個文件應當放在src的目錄下。properties文件格式如下:jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url=jdbc:sqlserver://localhost:1433;DatabaseName=spring
jdbc.username=sa
jdbc.password=********
使用context:property-placeholder進行配置
Spring3中提供了一個簡單的context:property-placeholder元素進行結合properties文件進行配置,這個元素的作用就相當於上一種方法PropertyPlaceHolderConfigurer這個bean,也需要一個額外的properties文件。
<context:property-placeholder location="classpath:jdbc.properties" /><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> 注意:如果使用上述方式配置DataSource,還需要在beans元素中增加對context前綴的說明以及其對應的schema的地址,這是applicationContext的頭部就變成如下:
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:property-placeholder location="classpath:conf/conf_a.properties"/>
……
</beans>
PS:上述XML文件中形如${XXX}這種格式的就是EL表達式,最先用在JSP中,不瞭解的可以百度一下。DataSource的使用
使用DataSource的方式也有兩種,可以直接在程序中用ApplicationContext.getBean來獲取,也可以在要使用DataSource的DAO中的setDataSource方法前加入@Resource註解來注入DataSource。
使用JdbcTemplate操作數據庫
參考:
http://hackpro.iteye.com/blog/1037376