搭建和配置Spring与jdbc整合的环境

 下面学习下Spring+JDBC组合开发,采用Spring + JDBC开发的项目也是比较多的,我们接着来看下采用Spring+JDBC组合开发需要继承的步骤,第一步是配置数据源;第二步是配置事务,因为我们打算使用Spring给我们提供的容器管理事务

Spring+JDBC组合开发
-------------------------------------------------------------------------
使用Spring+JDBC集成步骤如下:
配置数据源,如:
<!--数据源产品给我们提供了org.apache.commons.dbcp.BasicDataSource这么一个类,把这个类定义成一个bean,并且是单例的bean,类里面有个方法close,指定当类对象被销毁的时候,我们应该执行它的close方法-->

Xml代码 复制代码
  1. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  2.     <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>  
  3.     <property name="url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&amp;characterEncoding=UTF-8"/>  
  4.     <property name="username" value="root"/>  
  5.     <property name="password" value="123456"/>  
  6.      <!-- 连接池启动时的初始值 -->  
  7.      <property name="initialSize" value="1"/>  
  8.      <!-- 连接池的最大值 -->  
  9.      <property name="maxActive" value="500"/>  
  10.      <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->  
  11.      <property name="maxIdle" value="2"/>  
  12.      <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->  
  13.      <property name="minIdle" value="1"/>  
  14. </bean>  
  15.   
  16. 配置事务。配置事务时,需要在xml配置文件中引入用于声明事务的tx命名空间,事务的配置方式有两种:注解方式和基于XML配置方式。   
  17.   
  18. 采用注解方式配置事务   
  19. --------------------------------------------------------------   
  20. 采用注解方式   
  21. <!--用来指定JDBC的数据源事务管理器,因为刚才提到过我们打算使用Spring的事务管理功能,我们不再手工的控制事务的打开,提交或回滚,那么事务的提交和回滚都交给了Spring容器帮我们做。  这个类是Spring为我们提供的,专门针对数据源的数据管理器,这个类里面有个属性dataSource,这里要求我们为它注入一个数据源,这个数据源我们在前面定义了-->  
  22. [code="xmlbean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  23.     perty name="dataSource" ref="dataSource"/>  
  24. </bean>  


<!-- 采用@Transactional注解方式使用事务  因为我们目前要使用注解方式配置事务,所以我们要打开这个配置项。 注意这里有个前缀tx,这是一个tx命名空间,还要在配置文件里申明这个命名空间-->
  <tx:annotation-driven transaction-manager="txManager"/>

@Service @Transactional
public class PersonServiceBean implements PersonService {
}


在spring配置文件中引入用于声明事务的tx命名空间
--------------------------------------------------------------------

Java代码 复制代码
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.        xmlns:aop="http://www.springframework.org/schema/aop"  
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xmlns:tx="http://www.springframework.org/schema/tx"  
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  8.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   
  9.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
  10.            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">   
  11.   
  12. </beans>  
<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的参考手册也可以找到.
我们已经把事务配置的命名空间引入进来了,那么接下来将要定义这个配置项,定义了配置项之后就可以使用注解方式来配置事务,实际上这个配置项是对注解进行处理,前面也提到过:注解本身是干不了活的,是因为后面有一个处理器,那么这个注解才有了生命,<tx:annotation-driven transaction-manager="txManager"/>这个配置实际上是专门对注解进行解析的。把<tx:annotation-driven transaction-manager="txManager"/>加入beans.xml中,配置项里的transaction-manager这个属性是要指定事务管理器。
经过这么几步,我们已经配置好了Spring+JDBC的集成,
beans.xml

Xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xmlns:aop="http://www.springframework.org/schema/aop"  
  6.        xmlns:tx="http://www.springframework.org/schema/tx"  
  7.        xsi:schemaLocation="http://www.springframework.org/schema/beans   
  8.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  9.            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd   
  10.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
  11.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  12.   
  13.      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  14.         <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>  
  15.         <property name="url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&amp;characterEncoding=UTF-8"/>  
  16.         <property name="username" value="root"/>  
  17.         <property name="password" value="456"/>  
  18.          <!-- 连接池启动时的初始值 -->  
  19.          <property name="initialSize" value="1"/>  
  20.          <!-- 连接池的最大值 -->  
  21.          <property name="maxActive" value="500"/>  
  22.          <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->  
  23.          <property name="maxIdle" value="2"/>  
  24.          <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->  
  25.          <property name="minIdle" value="1"/>  
  26.      </bean>  
  27.   
  28.     <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  29.        <property name="dataSource" ref="dataSource"/>  
  30.     </bean>  
  31.     <tx:annotation-driven transaction-manager="txManager"/>  
  32.   
  33.     <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">  
  34.         <property name="dataSource" ref="dataSource"/>  
  35.     </bean>  
  36. </beans>  


接下来该如何开发呢?首先要在数据里建个Person表,里面有id,是主键,采用数据库的id自增长方式生成;然后再提供一个name,就提供两个字段,见图:


然后写针对这个表的添删改查操作,建一个业务bean,先定义它的接口
PersonService.java

Java代码 复制代码
  1. package cn.itcast.service;   
  2.   
  3. import java.util.List;   
  4.   
  5. import cn.itcast.bean.Person;   
  6.   
  7. public interface PersonService {   
  8.     //保存person   
  9.     public void save(Person person);   
  10.   
  11.     //更新person   
  12.     public void update(Person person);   
  13.   
  14.     //获取person   
  15.     public Person getPerson(Integer personid);   
  16.   
  17.     //获取所有person   
  18.     public List<Person> getPersons();   
  19.   
  20.     //删除指定id的person   
  21.     public void delete(Integer personid);   
  22. }  
package cn.itcast.service;

import java.util.List;

import cn.itcast.bean.Person;

public interface PersonService {
	//保存person
	public void save(Person person);

	//更新person
	public void update(Person person);

	//获取person
	public Person getPerson(Integer personid);

	//获取所有person
	public List<Person> getPersons();

	//删除指定id的person
	public void delete(Integer personid);
}



为了使得我们的JDBC操作更面向对象一些,这里再提供一个实体bean,它只是一个普普通通的java bean而已,在person bean里面,我们给它提供两个对应于数据库表字段的属性,同时也给它提供一个默认的构造函数
Person.java

Java代码 复制代码
  1. package cn.itcast.bean;   
  2.   
  3. public class Person {   
  4.     private Integer id;   
  5.     private String name;   
  6.        
  7.     public Person(){}   
  8.        
  9.     public Person(String name) {   
  10.         this.name = name;   
  11.     }   
  12.     public Integer getId() {   
  13.         return id;   
  14.     }   
  15.     public void setId(Integer id) {   
  16.         this.id = id;   
  17.     }   
  18.     public String getName() {   
  19.         return name;   
  20.     }   
  21.     public void setName(String name) {   
  22.         this.name = name;   
  23.     }   
  24. }  
package cn.itcast.bean;

public class Person {
	private Integer id;
	private String name;
	
	public Person(){}
	
	public Person(String name) {
		this.name = name;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}



对接口创建一个实现类,PersonServiceBean.java

Java代码 复制代码
  1. package cn.itcast.service.impl;   
  2.   
  3. import java.util.List;   
  4.   
  5. import org.springframework.transaction.annotation.Transactional;   
  6.   
  7. import cn.itcast.bean.Person;   
  8. import cn.itcast.service.PersonService;   
  9.   
  10. @Transactional  
  11. public class PersonServiceBean implements PersonService {   
  12.   
  13.     public void delete(Integer personid) {   
  14.     }   
  15.   
  16.     public Person getPerson(Integer personid) {   
  17.         return null;   
  18.     }   
  19.   
  20.     public List<Person> getPersons() {   
  21.         return null;   
  22.     }   
  23.   
  24.     public void save(Person person) {   
  25.     }   
  26.   
  27.     public void update(Person person) {   
  28.     }   
  29. }  
package cn.itcast.service.impl;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bean.Person;
import cn.itcast.service.PersonService;

@Transactional
public class PersonServiceBean implements PersonService {

	public void delete(Integer personid) {
	}

	public Person getPerson(Integer personid) {
		return null;
	}

	public List<Person> getPersons() {
		return null;
	}

	public void save(Person person) {
	}

	public void update(Person person) {
	}
}


创建好后要把这个业务bean交给Spring容器管理,采用基于xml配置方式交给Spring管理,把下面代码加入beans.xml

Xml代码 复制代码
  1. <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"/>  

当然也可以采用自动扫描的方式交给Spring管理。

要对数据库的表进行添删改查,我们需要通过数据源来进行操作,那么在PersonServiceBean这个业务bean里怎么获取数据源呢?我们可以使用Spring给我们提供的依赖注入功能,
PersonServiceBean.java

Java代码 复制代码
  1. package cn.itcast.service.impl;   
  2.   
  3. import java.util.List;   
  4.   
  5. import javax.sql.DataSource;   
  6.   
  7. import org.springframework.transaction.annotation.Transactional;   
  8.   
  9. import cn.itcast.bean.Person;   
  10. import cn.itcast.service.PersonService;   
  11.   
  12. @Transactional  
  13. public class PersonServiceBean implements PersonService {   
  14.   
  15.     private DataSource dataSource;   
  16.   
  17.     //只需要set方法,get方法就不提供了   
  18.     public void setDataSource(DataSource dataSource) {   
  19.         this.dataSource = dataSource;   
  20.     }   
  21.   
  22.     public void delete(Integer personid) {   
  23.     }   
  24.   
  25.     public Person getPerson(Integer personid) {   
  26.         return null;   
  27.     }   
  28.   
  29.     public List<Person> getPersons() {   
  30.         return null;   
  31.     }   
  32.   
  33.     public void save(Person person) {   
  34.     }   
  35.   
  36.     public void update(Person person) {   
  37.     }   
  38. }  
package cn.itcast.service.impl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bean.Person;
import cn.itcast.service.PersonService;

@Transactional
public class PersonServiceBean implements PersonService {

	private DataSource dataSource;

	//只需要set方法,get方法就不提供了
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public void delete(Integer personid) {
	}

	public Person getPerson(Integer personid) {
		return null;
	}

	public List<Person> getPersons() {
		return null;
	}

	public void save(Person person) {
	}

	public void update(Person person) {
	}
}


提供了数据源属性dataSource后呢,就可以在配置文件中对数据源属性进行注入,当然这个注入也可以使用注解的方式,这里采用的是基于XML配置文件的方式,添加下面代码进beans.xml

Xml代码 复制代码
  1. <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">  
  2.     <property name="dataSource" ref="dataSource"/>  
  3. </bean>  

数据源已经注入到dataSource属性中去了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值