下面学习下Spring+JDBC组合开发,采用Spring + JDBC开发的项目也是比较多的,我们接着来看下采用Spring+JDBC组合开发需要继承的步骤,第一步是配置数据源;第二步是配置事务,因为我们打算使用Spring给我们提供的容器管理事务
Spring+JDBC组合开发
-------------------------------------------------------------------------
使用Spring+JDBC集成步骤如下:
配置数据源,如:
<!--数据源产品给我们提供了org.apache.commons.dbcp.BasicDataSource这么一个类,把这个类定义成一个bean,并且是单例的bean,类里面有个方法close,指定当类对象被销毁的时候,我们应该执行它的close方法-->
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
- <property name="url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8"/>
- <property name="username" value="root"/>
- <property name="password" value="123456"/>
- <!-- 连接池启动时的初始值 -->
- <property name="initialSize" value="1"/>
- <!-- 连接池的最大值 -->
- <property name="maxActive" value="500"/>
- <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
- <property name="maxIdle" value="2"/>
- <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
- <property name="minIdle" value="1"/>
- </bean>
- 配置事务。配置事务时,需要在xml配置文件中引入用于声明事务的tx命名空间,事务的配置方式有两种:注解方式和基于XML配置方式。
- 采用注解方式配置事务
- --------------------------------------------------------------
- 采用注解方式
- <!--用来指定JDBC的数据源事务管理器,因为刚才提到过我们打算使用Spring的事务管理功能,我们不再手工的控制事务的打开,提交或回滚,那么事务的提交和回滚都交给了Spring容器帮我们做。 这个类是Spring为我们提供的,专门针对数据源的数据管理器,这个类里面有个属性dataSource,这里要求我们为它注入一个数据源,这个数据源我们在前面定义了-->
- [code="xmlbean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- perty name="dataSource" ref="dataSource"/>
- </bean>
<!-- 采用@Transactional注解方式使用事务 因为我们目前要使用注解方式配置事务,所以我们要打开这个配置项。 注意这里有个前缀tx,这是一个tx命名空间,还要在配置文件里申明这个命名空间-->
<tx:annotation-driven transaction-manager="txManager"/>
@Service @Transactional
public class PersonServiceBean implements PersonService {
}
在spring配置文件中引入用于声明事务的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>
<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 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:aop="http://www.springframework.org/schema/aop"
- 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/context http://www.springframework.org/schema/context/spring-context-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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
- <property name="url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8"/>
- <property name="username" value="root"/>
- <property name="password" value="456"/>
- <!-- 连接池启动时的初始值 -->
- <property name="initialSize" value="1"/>
- <!-- 连接池的最大值 -->
- <property name="maxActive" value="500"/>
- <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
- <property name="maxIdle" value="2"/>
- <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
- <property name="minIdle" value="1"/>
- </bean>
- <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"/>
- </bean>
- <tx:annotation-driven transaction-manager="txManager"/>
- <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
- <property name="dataSource" ref="dataSource"/>
- </bean>
- </beans>
接下来该如何开发呢?首先要在数据里建个Person表,里面有id,是主键,采用数据库的id自增长方式生成;然后再提供一个name,就提供两个字段,见图:
然后写针对这个表的添删改查操作,建一个业务bean,先定义它的接口
PersonService.java
- 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);
- }
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
- 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;
- }
- }
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
- 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) {
- }
- }
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
- <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"/>
当然也可以采用自动扫描的方式交给Spring管理。
要对数据库的表进行添删改查,我们需要通过数据源来进行操作,那么在PersonServiceBean这个业务bean里怎么获取数据源呢?我们可以使用Spring给我们提供的依赖注入功能,
PersonServiceBean.java
- 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) {
- }
- }
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
- <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
- <property name="dataSource" ref="dataSource"/>
- </bean>
数据源已经注入到dataSource属性中去了