一步一步深入spring(7)-- 整合spring和JDBC的环境
1.配置数据源
<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"/>
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的环境的更多相关文章
- Spring Kafka整合Spring Boot创建生产者客户端案例
每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code 创建一个kafka-producer-master的maven工程.整个项目结构如下: ...
- Spring Boot 整合Spring Data JPA
Spring Boot整合Spring Data JPA 1)加入依赖 <dependency> <groupId>org.springframework.boot</g ...
- Spring Boot整合Spring Batch
引言 Spring Batch是处理大量数据操作的一个框架,主要用来读取大量数据,然后进行一定的处理后输出指定的形式.比如我们可以将csv文件中的数据(数据量几百万甚至几千万都是没问题的)批处理插入保 ...
- Spring Boot整合Spring Security
Spring Boot对于该家族的框架支持良好,但是当中本人作为小白配置还是有一点点的小问题,这里分享一下.这个项目是使用之前发布的Spring Boot会员管理系统重新改装,将之前filter登录验 ...
- Spring boot 整合spring Data JPA+Spring Security+Thymeleaf框架(上)
近期上班太忙所以耽搁了给大家分享实战springboot 框架的使用. 以下是spring boot 整合多个框架的使用. 首先是准备工作要做好. 第一 导入框架所需的包,我们用的事maven 进行 ...
- spring boot整合spring Data JPA和freemarker
1.spring Data JPA简介 是一个替代hibernate的一个作用于数据库的框架. 2.整合 1.导入依赖 <dependency> <groupId>org.sp ...
- Spring Boot整合Spring Security自定义登录实战
本文主要介绍在Spring Boot中整合Spring Security,对于Spring Boot配置及使用不做过多介绍,还不了解的同学可以先学习下Spring Boot. 本demo所用Sprin ...
- Spring Cloud实战 | 第九篇:Spring Cloud整合Spring Security OAuth2认证服务器统一认证自定义异常处理
本文完整代码下载点击 一. 前言 相信了解过我或者看过我之前的系列文章应该多少知道点我写这些文章包括创建 有来商城youlai-mall 这个项目的目的,想给那些真的想提升自己或者迷茫的人(包括自己- ...
- Spring Boot 整合Spring Data以及rabbitmq,thymeleaf,向qq邮箱发送信息
首先得将自己的qq开启qq邮箱的POP3/SMTP服务 说明: p,e为路由key. 用户系统完成登录的时候,将{手机号-时间-IP}保存到队列Phone-queue中,msg-sys系统获得消息打印 ...
- 简单使用:spring boot整合spring Data JPA
JPA顾名思义就是Java Persistence API的意思,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. 1.jpa具有什么优势? (1).标准 ...
随机推荐
- 【转】height,posHeight和pixelHeight区别
height 返回高度 带单位 posHeight 不带单位的数字 pixelHeight 转换成像素后的数字 不带单位 更多资料 http://www.1fan.com.cn
- 我的MYSQL学习心得(十一)
原文:我的MYSQL学习心得(十一) 我的MYSQL学习心得(十一) 我的MYSQL学习心得(一) 我的MYSQL学习心得(二) 我的MYSQL学习心得(三) 我的MYSQL学习心得(四) 我的MYS ...
- PHP版本的区别
原文:PHP版本的区别 以为这个已经写过了,发现没有,赶紧补充下. PHP的版本,自从进入5以后,发布新版本速度明显提升很多,从PHP5.2开始,5.3 .5.4 .5.5,就快要6.0了. 注:ec ...
- C语言学习笔记-顺序表
#include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include "coni ...
- c语言中逗号运算符和逗号表达式
原文:c语言中逗号运算符和逗号表达式 C语言提供一种特殊的运算符——逗号运算符.用它将两个表达式连接起来.如: 3+5,6+8称为逗号表达式,又称为“顺序求值运算符”.逗号表达式的一般形式为 表达式1 ...
- Android分析应用程序的构建过程
为了方便Android应用开发要求我们Androidproject编制和包装有了更深入的了解,例如,我们知道这是做什么的每一步,境和工具.输入和输出是什么.等等. 在前文<命令行下Android ...
- Knockout简单用法
Knockout简单用法 在最近做的一个项目中,页面数据全部通过js ajax调用webapi接口获取,也就是说页面的数据全部使用javascript脚本填充,这就想到了使用一个MVVM模式的js框架 ...
- 自己写CPU第九阶段(5)——实现负载存储指令2(改变运行阶段)
我们会继续上传新书<自己动手写CPU>.今天是第42篇.我尽量每周四篇,可是近期已经非常久没有实现这个目标了,一直都有事.不好意思哈. 开展晒书评送书活动,在q=%E4%BA%9A%E9% ...
- 简单分析android textview xml 的属性设置
android:ems 设置TextView的宽度为N个字符的宽度. 这样的好处就是,在定义编辑框空间输入多少字符的时候,可以根据固定的值设置编辑框宽度.保证边框和文字的宽度统一.android:ma ...
- selenium + python 部署自动化测试环境
选择selenium和python其实是怀有私心的:码两行python,熟悉熟悉. selenium优点很多,我最看重的是支持多语言,足够简单,同时支持浏览器. 实际工作中,简单实用真的太重要了 ...