日期:2014-05-16  浏览次数:20376 次

使用Spring+JDBC组合步骤

使用Spring+JDBC组合步骤如下:

一、首先:配置数据源如:

在配置数据源时,先添加spring开发能力,添加库文件{

Spring 2.5 AOP Libraries

Spring 2.5 Core Libraries

Spring 2.5 Persistence Core Libraries

Spring 2.5 Persistence JDBC Libraries??? //这个可不要忘了加

}

注意: ${}是把dataSource的属性放到properties文档里面

 <bean id="dataSource"
  class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName"
   value="${driverClassName}">
  </property>
  <property name="url"
   value="${url}">
  </property>
  <property name="username" value="${username}"></property>
  <property name="password" value="${password}"></property>
  <!-- 连接池启动时的初始值 -->
  <property name="initialSize" value="${initialSize}"></property>
  <!-- 连接池的最大值 -->
  <property name="maxActive" value="${maxActive}"></property>
  <!-- 最大空间值、当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,直至减少的maxIdle为止 -->
  <property name="maxIdle" value="${maxIdle}"></property>
  <!-- 最小空间值,当空间的连接数少于阀值时,连接池就会预申请一些连接,以免洪峰来时 来不及申请 -->
  <property name="minIdle" value="${minIdle}"></property>
 </bean>

?可以把配置放到属性文件里面去 jdbc.properties

driverClassName=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/test?useUnicode\=true&characterEncoding\=UTF-8
username=root
password=fly
initialSize=1
maxActive=500
maxIdle=2
minIdle=1

?在spring容器中加入下面代码

<context:property-placeholder location="classpath:jdbc.properties"/>

二、配置事务,配置事务时,需要在xml配置文件中引入用于声明事务的tx命名空间,

事务的配置有两种:注解方式和基于XML配置方式

在这里我采用“注解方式”如:? <bean id="txManager"
? class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
? abstract="false" lazy-init="default"
? dependency-check="default">
? <property name="dataSource">?????????? //要求注入数据源,dataSource是我们自己定义的数据源
?? <ref bean="dataSource" />
? </property>
?</bean>

? 我们采用注解方式,

<!-- 采用@Transaction注解方式使用事务 -->

<tx:annotation-driven transaction-manager="txManager"/>?? //transaction-manager属性指定事务管理器

首先添加tx:的命名空间:红色的是tx的命名空间

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

</beans>

?经过这几步的配置,我们已经配置好了spring到jdbc的集成

spring容器中的全部配置:如下:可以直接拷贝使用

<?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:aop="http://www.springframework.org/schema/aop"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd