http://blog.csdn.net/yerenyuan_pku/article/details/52882435

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

使用Spring+JDBC集成步骤如下: 
第一步,配置数据源。我们使用DBCP数据库连接池。 
我们首先在Eclipse中新建一个普通的Java Project,名称为springAndJDBC。接着导入所需Jar包到项目中,所需Jar包共有: 
 
然后我们在Sping配置文件中——beans.xml配置数据源,即在beans.xml中加入如下内容:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/jdbc?useUnicode=true&amp;characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="yezi" />
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="1" />
<!-- 连接池的最大值 -->
<property name="maxActive" value="500" />
<!-- 最大空闲值。当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="2" />
<!-- 最小空闲值。当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1" />
</bean>
  • 1

第二步,配置事务。因为我们打算使用Spring给我们提供的容器来管理事务。配置事务时,需要在XML配置文件中引入用于声明事务的tx命名空间,事务的配置方式有两种:注解方式和基于XML配置方式。 
我们首先在Spring配置文件中引入用于声明事务的tx命名空间:

<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> </beans>
  • 1

接着我们采用注解方式配置声明式事务,需要在Spring配置文件中添加如下内容:

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

以上配置指定了Spring的事务管理器(由于我们打算使用Spring的事务管理功能),这样,我们不再手工控制事务的打开、提交或回滚,而是都交给Spring的事务管理器来管理。 
提示:org.springframework.jdbc.datasource.DataSourceTransactionManager是Spring为我们提供的专门针对数据源的事务管理器。 
由于我们采用注解——@Transactional的方式来配置声明式事务,所以我们还要在Spring配置文件中添加如下内容:

<tx:annotation-driven transaction-manager="txManager" />

以上配置隐式地注册了对注解——@Transactional进行解析的处理器。经过以上两步,Spring与JDBC整合的环境就算搭建好了,Spring配置文件——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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/jdbc?useUnicode=true&amp;characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="yezi" />
<!-- 连接池启动时的初始值 -->
<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" />
</beans>
  • 1

Spring集成的JDBC编码和事务管理

Spring与JDBC整合的环境搭建好之后,我们就要编写JDBC代码了。首先在jdbc数据库中创建一张person表,如下: 

接着在src目录下新建一个cn.itcast.bean包,并在该包下新建一个JavaBean——Person.java,其代码为:

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;
}
}
  • 1

再接着在src目录下新建一个cn.itcast.service包,并在该包下新建一个接口——PersonService.java,其代码为:

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);
}
  • 1

紧接着在src目录下新建一个cn.itcast.service.impl包,并在该包下新建一个PersonService接口的实现类——PersonServiceBean.java。因为我们要对数据库person表进行增删改查,所以需要通过数据源dataSource进行操作,但是我们最好不要直接通过数据源dataSource进行操作,而是应使用Spring为我们提供的JdbcTemplate类进行JDBC操作,因为这个辅助类封装了比较多的JDBC代码,故PersonServiceBean类的代码应为:

/**
* 使用JdbcTemplate进行insert/update/delete/select操作
* @author li ayun
*
*/
public class PersonServiceBean implements PersonService {
private JdbcTemplate jdbcTemplate; public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
} @Override
public void save(Person person) {
jdbcTemplate.update("insert into person(name) value(?)", new Object[]{person.getName()},
new int[]{java.sql.Types.VARCHAR});
} @Override
public void update(Person person) {
jdbcTemplate.update("update person set name=? where id=?", new Object[]{person.getName(), person.getId()},
new int[]{java.sql.Types.VARCHAR, java.sql.Types.INTEGER});
} /**
* 使用JdbcTemplate获取一条记录
*/
@Override
public Person getPerson(Integer personid) {
return jdbcTemplate.queryForObject("select * from person where id=?", new Object[]{personid},
new int[]{java.sql.Types.INTEGER}, new PeronRowMapper());
} /**
* 使用JdbcTemplate获取多条记录
*/
@Override
public List<Person> getPersons() {
return jdbcTemplate.query("select * from person", new PeronRowMapper());
} @Override
public void delete(Integer personid) {
jdbcTemplate.update("delete from person where id=?", new Object[]{personid},
new int[]{java.sql.Types.INTEGER});
} }

接下来还要在cn.itcast.service.impl包下新建一个类——PeronRowMapper.java,其代码为:

public class PeronRowMapper implements RowMapper<Person> {

    @Override
public Person mapRow(ResultSet rs, int index) throws SQLException {
Person person = new Person(rs.getString("name"));
person.setId(rs.getInt("id"));
return person;
} }
  • 1

有人可能会问,为何mapRow()方法内部一开始不调用一下rs.next()方法?答案是外部调用mapRow()方法时,已经执行了诸如if(rs.next()) { ... } 这样的代码。 
然后我们要将PersonServiceBean交给Spring进行管理,即需要在Spring配置文件中添加如下内容:

<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
<property name="dataSource" ref="dataSource" />
</bean>

最后,我们就要在src目录下新建一个junit.test包,并在该包下新建一个单元测试类——PersonServiceTest.java,对我们编写的业务bean的JDBC代码进行测试。

public class PersonServiceTest {
private static PersonService personService; @BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
personService = (PersonService) cxt.getBean("personService");
} catch (Exception e) { // 若出错,则打印在控制台上
e.printStackTrace();
}
} @Test
public void save() {
personService.save(new Person("李阿昀"));
} @Test
public void getPerson() {
Person person = personService.getPerson(1);
System.out.println(person.getName());
} @Test
public void update() {
Person person = personService.getPerson(1);
person.setName("李子");
personService.update(person);
} @Test
public void delete() {
personService.delete(1);
} @Test
public void save_5() {
for (int i = 1; i <= 5; i++) {
personService.save(new Person("李阿昀" + i));
}
} @Test
public void getBeans() {
for (Person person : personService.getPersons()) {
System.out.println(person.getName());
}
}
}
  • 1

分别测试以上方法,都顺利通过,大发! 
从PersonServiceBean类的代码中我们可以看出该PersonServiceBean并没有受Spring的事务管理,因为我们没有为PersonServiceBean标注@Transactional注解,若要是不定义这个@Transactional注解,那么像save()方法中的每条sql语句都会在各自的事务中进行执行。若save()方法中有2条语句,例如:

public void save(Person person) {
jdbcTemplate.update("insert into person(name) value(?)", new Object[]{person.getName()},
new int[]{java.sql.Types.VARCHAR}); jdbcTemplate.update("insert into person(name) value(?)", new Object[]{person.getName()},
new int[]{java.sql.Types.VARCHAR});
}
  • 1

这样,2条语句都会在各自的事务中执行,他们是无法保证在同一个事务中执行的,从而会出现一些问题。因此为了保证多条语句在同一个事务中执行,我们应该使用Spring容器给我们提供的声明式事务,即在该PersonServiceBean加上@Transactional注解。这样,该PersonServiceBean的所有业务方法在方法执行前打开事务,方法执行后关闭事务。 
使用属性占位符方式配置数据源 
在使用Spring+JDBC组合开发过程中,有人喜欢把数据库连接等信息放在一个属性文件中,接着使用属性占位符方式将属性文件中的内容引用进来。 
现在类路径底下新建一个属性文件——jdbc.properties,其内容为: 

接着,我们就要将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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- 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}" />
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="${jdbc.initialSize}" />
<!-- 连接池的最大值 -->
<property name="maxActive" value="${jdbc.maxActive}" />
<!-- 最大空闲值。当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="${jdbc.maxIdle}" />
<!-- 最小空闲值。当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="${jdbc.minIdle}" />
</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>

此时再来测试PersonServiceTest类中的各个方法,仍然都会顺利通过。如须查看源码,可点击Spring+JDBC组合开发进行下载。

 
 

(转)Spring+JDBC组合开发的更多相关文章

  1. Spring笔记——Spring+JDBC组合开发

      使用Spring+JDBC集成步骤如下:   1. 配置数据源 2. 配置事务.配置事务时,需要在xml配置文件中引入用于声明事务的tx命名空间,事务的配置方式有两种:注解方式和基于XML配置方式 ...

  2. Spring + JDBC 组合开发集成步骤

    1:配置数据源,如: <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="h ...

  3. SpringMVC笔记——Spring+MyBatis组合开发简单实例

    简介 SSH框架很强大,适合大型项目开发.但学无止境,多学会一门框架组合开发会让自己增值许多. SSM框架小巧精致,适合中小型项目快速开发,对于新手来说也是简单上手的.在SSM框架搭建之前,我们先学习 ...

  4. Spring JDBC数据库开发

    针对数据库操作,Spring框架提供了JdbcTemplate类. 1.Spring JDBC的配置 创建配置文件applicationContext.xml,添加如下代码: <!--配置数据源 ...

  5. 【Spring】Spring的数据库开发 - 1、Spring JDBC的配置和Spring JdbcTemplate的解析

    Spring JDBC 文章目录 Spring JDBC Spring JdbcTemplate的解析 Spring JDBC的配置 简单记录-Java EE企业级应用开发教程(Spring+Spri ...

  6. (转)Spring4.2.5+Hibernate4.3.11组合开发

    http://blog.csdn.net/yerenyuan_pku/article/details/52887573 搭建和配置Spring与Hibernate整合的环境 今天我们来学习Spring ...

  7. Spring的数据库开发

                                Spring JDBC框架操作mysql数据库 Spring中的JDBC为我们省去连接和关闭数据库的代码,我们着重关注对数据库的操作.Sprin ...

  8. Spring JDBC

    转载:博客主页:http://blog.csdn.NET/chszs 一.概述 在Spring JDBC模块中,所有的类可以被分到四个单独的包:1)core即核心包,它包含了JDBC的核心功能.此包内 ...

  9. Maven+druid+MyBatis+Spring+Oracle+Dubbo开发环境搭建

    1.开发工具使用: MyEclipse或Eclipse,数据库使用Oracle.需要用到的软件有Zookeeper(注册中心),Tomcat(Web容器)和Maven(包管理). 2.初始环境配置: ...

随机推荐

  1. 在Service里调用AlertDialog

    用常规的方法在AlertDialog的时候,会报错,大意是「can not add window in this view」. 原因是Service是没有界面的,只有Activity才能添加界面. 解 ...

  2. HDU5890:Eighty seven(Bitset优化背包)

    Mr. Fib is a mathematics teacher of a primary school. In the next lesson, he is planning to teach ch ...

  3. iOS 开发图片资源选择png格式还是jpg格式

    对于iOS本地应用程序来说最简单的答案就是始终使用PNG,除非你有非常非常好的理由不用它. 当iOS应用构建的时候,Xcode会通过一种方式优化.png文件而不会优化其它文件格式.它优化得相当的好 他 ...

  4. javascript之数组的6种去重方法

    去重 var arr=[11,11,333,4,4,5,66,66,7]; // 方法一:在新数组内判断不存在时加入 var newarr1=[]; function quchong1(){ for( ...

  5. 【总结——HTTP协议】

    一.HTTP协议简介 什么是HTTP?全称是HyperText Transfer Protocal,即:超文本传输协议,从1990年开始就在WWW上广泛应用,是现今在WWW上应用最多的协议,目前版本是 ...

  6. mysql负载均衡方案

    mysql负载均衡方案 一.直接连接 数据库的读写分离方案很多,这里介绍基于mysql数据库的读写分离方案. 比较常见的读写分离方案如下: 1 基于查询分离 最简单的分离方法是将读和写分发到主和从服务 ...

  7. Logistic/Softmax Regression

    辅助函数 牛顿法介绍 %% Logistic Regression close all clear %%load data x = load('ex4x.dat'); y = load('ex4y.d ...

  8. 任务28:RequestDelegate管道实现思路

    任务28:RequestDelegate管道实现思路 管道的实现机制 RequestDelegate是管道的核心.ApplicationBuilder就是接收了很多个RequestDelegae把它拼 ...

  9. clipse maven 项目 出现红色叹号 解决方法

    感叹号代表jar包不全,看你是maven工程,pom.xml报错,应该是jar包路径不对.打开pom.xml文件,查看报错位置和报错信息. 缺失的jar包的配置去http://search.maven ...

  10. 游戏服务端pomelo安装配置

    一.安装环境 Linux Ubantu 二.安装需要的组件 1.安装nodejs 注:debian下nodejs没有相应的apt包,所以无法用apt-get安装,只能通过nodejs的源码包安装, 这 ...