1.配置数据源

(1).添加支持数据源的jar包commons-dbcp.jar 、commons-pool.jar
当然也要添加其他的spring用到的jar以及这里用到的数据库mysql的jar  mysql-connector-java-5.0.7-bin.jar
(2).在bean.xml中添加配置文件代码
     <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://127.0.0.1:3306/yangyang"/>
<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>
2.配置事务
配置事务时,需要在命名空间中添加引用事务声明的tx命名空间,配置事务有两种方式,一种是基于注解,一种是基于xml方式
这里先来实现基于注解的方式。
 配置spring对事务的支持:
     <!-- spring的事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 采用@Transactional使用注解方式处理事务 -->
<tx:annotation-driven transaction-manager="txManager"/>

3.编写业务bean,这里只给出实现部分的代码:

 package com.yangyang.service.impl;

 import java.util.List;

 import javax.sql.DataSource;

 import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional; import com.yangyang.model.Person;
import com.yangyang.service.PersonService; //受spring的事务管理
@Transactional
public class PersonServiceImpl implements PersonService{ private JdbcTemplate jdbcTemplate;//通过jdbcTemplate来操作数据源
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
} @Override
public void save(Person person) {
jdbcTemplate.update("insert into person(name) values(?)", 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});
} @Override
public void delete(Integer personId) {
jdbcTemplate.update("delete from person where id=?",new Object[]{personId},
new int[]{java.sql.Types.INTEGER});
} @Override
public Person getPerson(Integer personId) {
return (Person)jdbcTemplate.queryForObject("select * from person where id=?", new Object[]{personId},new int[]{java.sql.Types.INTEGER}
,new PersonRowMapper());
} @Override
public List<Person> getPersons() {
return (List<Person>)jdbcTemplate.query("select * from person",new PersonRowMapper());
} }

对查询支持的RowMapper 类

 package com.yangyang.service.impl;

 import java.sql.ResultSet;
import java.sql.SQLException; import org.springframework.jdbc.core.RowMapper; import com.yangyang.model.Person; public class PersonRowMapper implements RowMapper{ @Override
public Object mapRow(ResultSet rs, int index) throws SQLException {
Person person=new Person();
person.setName(rs.getString("name"));
person.setId(rs.getInt("id"));
return person;
} }

当然我们也需要将业务bean配置到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-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://127.0.0.1:3306/yangyang"/>
<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> <!-- spring的事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 采用@Transactional使用注解方式处理事务 -->
<tx:annotation-driven transaction-manager="txManager"/> <bean id="personService" class="com.yangyang.service.impl.PersonServiceImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>

接下来编写单元测试来测试我们的类,这里省略了,可以看到配置成功。

当然我们会想把jdbc连接的部分以properties的形式抽出来,这样就有

driverClassName=org.gjt.mm.mysql.Driver
url=jdbc:mysql://127.0.0.1:3306/yangyang
username=root
password=123456
initialSize=1
maxActive=500
maxIdle=2
minIdle=1

然后在bean中改为:

 <?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">
<!-- 加上aop的命名空间以及DTD验证 --> <!-- spring对外部文件的支持 -->
<context:property-placeholder location="classpath:resources/jdbc.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="${initialSize}"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="${maxActive}"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="${maxIdle}"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="${minIdle}"/>
</bean> <!-- spring的事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 采用@Transactional使用注解方式处理事务 -->
<tx:annotation-driven transaction-manager="txManager"/> <bean id="personService" class="com.yangyang.service.impl.PersonServiceImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>

执行单元测试发现也是ok的

下面再来利用xml配置的方式来实现事务,在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">
<!-- 加上aop的命名空间以及DTD验证 --> <!-- spring对外部文件的支持 -->
<context:property-placeholder location="classpath:resources/jdbc.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="${initialSize}"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="${maxActive}"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="${maxIdle}"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="${minIdle}"/>
</bean> <!-- spring的事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 使用配置的形式使用注解 -->
<aop:config>
<aop:pointcut id="transactionPointcut" expression="exception(* com.yangyang.service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/>
</aop:config> <tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!--get开头的方法不添加事务 -->
<tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice> <bean id="personService" class="com.yangyang.service.impl.PersonServiceImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>

这样,我们不需要在业务类中加入任何注解,通过配置也能实现事务鼓管理

一步一步深入spring(7)-- 整合spring和JDBC的环境的更多相关文章

  1. Spring Kafka整合Spring Boot创建生产者客户端案例

    每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code 创建一个kafka-producer-master的maven工程.整个项目结构如下: ...

  2. Spring Boot 整合Spring Data JPA

    Spring Boot整合Spring Data JPA 1)加入依赖 <dependency> <groupId>org.springframework.boot</g ...

  3. Spring Boot整合Spring Batch

    引言 Spring Batch是处理大量数据操作的一个框架,主要用来读取大量数据,然后进行一定的处理后输出指定的形式.比如我们可以将csv文件中的数据(数据量几百万甚至几千万都是没问题的)批处理插入保 ...

  4. Spring Boot整合Spring Security

    Spring Boot对于该家族的框架支持良好,但是当中本人作为小白配置还是有一点点的小问题,这里分享一下.这个项目是使用之前发布的Spring Boot会员管理系统重新改装,将之前filter登录验 ...

  5. Spring boot 整合spring Data JPA+Spring Security+Thymeleaf框架(上)

    近期上班太忙所以耽搁了给大家分享实战springboot 框架的使用. 以下是spring boot 整合多个框架的使用. 首先是准备工作要做好. 第一  导入框架所需的包,我们用的事maven 进行 ...

  6. spring boot整合spring Data JPA和freemarker

    1.spring Data JPA简介 是一个替代hibernate的一个作用于数据库的框架. 2.整合 1.导入依赖 <dependency> <groupId>org.sp ...

  7. Spring Boot整合Spring Security自定义登录实战

    本文主要介绍在Spring Boot中整合Spring Security,对于Spring Boot配置及使用不做过多介绍,还不了解的同学可以先学习下Spring Boot. 本demo所用Sprin ...

  8. Spring Cloud实战 | 第九篇:Spring Cloud整合Spring Security OAuth2认证服务器统一认证自定义异常处理

    本文完整代码下载点击 一. 前言 相信了解过我或者看过我之前的系列文章应该多少知道点我写这些文章包括创建 有来商城youlai-mall 这个项目的目的,想给那些真的想提升自己或者迷茫的人(包括自己- ...

  9. Spring Boot 整合Spring Data以及rabbitmq,thymeleaf,向qq邮箱发送信息

    首先得将自己的qq开启qq邮箱的POP3/SMTP服务 说明: p,e为路由key. 用户系统完成登录的时候,将{手机号-时间-IP}保存到队列Phone-queue中,msg-sys系统获得消息打印 ...

  10. 简单使用:spring boot整合spring Data JPA

    JPA顾名思义就是Java Persistence API的意思,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. 1.jpa具有什么优势? (1).标准 ...

随机推荐

  1. 关于小改CF协同过滤至MapReducer上的一些心得

    至上次重写ID3 MR版之后,手贱继续尝试CF.之前耳闻CF这两年内非常火,论内某大神也给了单机版(90%代码来自于其).所以想试试能否改到MR上.整体来说,CF本身的机制以相似性为核心,与迭代调用几 ...

  2. VS2013中实现angular代码智能提示

    第一步:在项目同添加angular js文件的引用: 这里使用NuGet包管理器来给项目添加angular js install-package angularjs 第二步:添加智能提示js文件 我们 ...

  3. select刷新后,保持选定状态,Cookies存储select选定状态信息

    //cookies存储select选定值,防止刷新后没了 window.onload = function () { var cooki = document.cookie; if (cooki != ...

  4. jquery插件FlexiGrid的使用

    jquery插件FlexiGrid的使用 已不推荐下载,如要下载去这个连接下载最新的 http://gundumw100.iteye.com/blog/545610  更新于2009-11-30 先看 ...

  5. J2EE

    随笔分类 - J2EE   关于SpringMVC Json使用 摘要: 很简单的一个东西,这里就不做过多介绍了,写一个小Demo,随手记录下.首先,在搭好SpringMVC工程环境之后,如果想用Sp ...

  6. [译]ava 设计模式之享元

    (文章翻译自Java Design Pattern: Flyweight) 享元模式用于最小化内存开销.它做的就是使用其他相似的对象尽可能多的分享数据. 1.享元模式类图 2.享元模式Java代码 / ...

  7. 怎样使用万用表来测试板子上的TX和RX引脚

    最近在看openwrt时看到的一个方法,直接看下面写的: 文章来自:http://wiki.openwrt.org/doc/hardware/port.serial 经实际测试时发现,将万用表的负表笔 ...

  8. 让png在ie下正常显示 用到了jquery插件DD_belatedPNG 但是在ie6中这句话 前面添加有效 后面移除无效 IE6 jq removeClass无效

    jQuery(this).addClass("background").siblings().removeClass("background") 导致IE6移除 ...

  9. 【分布式存储系统sheepdog 】

    Sheepdog,是由NTT的3名日本研究员开发的开源项目,主要用来为虚拟机提供块设备. 其架构例如以下: 以下,我们将从架构.模块等几个方面来介绍下: 一.架构图 如上图: 採用无中心节点的全对称架 ...

  10. jQuery MiniUI

    今天找到一个关于jQuery的又一个不错的UI,特此保存下.备用. http://www.miniui.com/