Spring访问数据库--DataSource方式和JdbcTemplate方式

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章