翠湖月色
No Buddha tree at all,And bright mirror nor.Now nothing at all,How dusts any more ?
首页
新随笔
联系
聚合
管理
随笔 - 245 文章 - 384 trackbacks - 0
<
2006年12月
>
日
一
二
三
四
五
六
26
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
本博客系个人收集材料及学习记录之用,各类“大侠”勿扰!
留言簿
(7)
给我留言
查看公开留言
查看私人留言
随笔分类
JavaEE(34)
Linux与Unix(6)
Web Services and SOA(16)
人文历史(17)
名词解释(2)
品读英语(18)
基础编程篇(9)
所感所悟(25)
报表设计与开发(12)
数据库(Sql server,My sql)(1)
数据结构与算法设计(9)
有关网络(5)
杂录(29)
经典视听(12)
网站应用(6)
脚本语言及页面设计(8)
软件测试技术(8)
阅读笔记(9)
收藏夹
开源项目(2)
编程开发(3)
网络应用(2)
My Favorite Web Sites
CSDN Java 频道
IBM developerWorks中国
Java开源大全
J道:Java解决之道
SpringFramwork中文论坛
中国万维网联盟论坛
中文Java学习网站-孙卫琴版主
中文Java技术主力站点-JR论坛
绿色软件下载-霏凡软件站
蝙蝠英语学习网
名Bloger
Martin Fowler's blog
No1
算法研究、C/C++
月光博客
非著名Bloger
willing的Blog
德贤Blog
放水老倌
搜索
积分与排名
积分 - 73063
排名 - 132
最新评论
1. re: 数据结构之线性表- 一元稀疏多项式计算器
好感动 呜呜TT
终于让我找到了
我会好好看滴
谢谢
--烦死数据结构
2. re: SQL server存储过程
唐,你的论文就是事务嘛!
--倌
3. re: ActiveBpel引擎专题-ActiveBPEL2.x用户手册[未登录]
你好,我配置ActiveBPEL的持久化总是不成功,是什么原因呢?
--hh
4. re: Struts+Spring+Hibernate in action之:简单例子开发
麻烦你,我也要一份,谢谢。sdjnqiying@163.com
--qy
5. re: Struts+Spring+Hibernate in action之:简单例子开发
yangmeng88714@126.com太感谢了
--杨萌
学习笔记-Springs声明性JDBC事务管理
近日在看台湾人林信良的《Spring技术手册》,这本书总体上简单易懂,适合初学者。但是在声明性JDBC事务管理这节中的例子程序写的不够详细。下来看看并略加修改了下。
首先,在MySQL中建立一个表myuser。注意要让MySQL支持事务,要选择InnoDB类型的表。
Create table myuser(
id int (11) not null auto_increment primary key,
name varchar(100) not null default '',
age int
)type=InnoDB;
这里有个实体类user.java:
package
onlyfun.caterpillar;
public
class
User
{
private
Integer id;
private
String name;
private
Integer age;
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;
}
public
Integer getAge()
{
return
age;
}
public
void
setAge(Integer age)
{
this
.age
=
age;
}
}
为了面向接口编程,我们实现一个接口,让DAO类实现这个接口。
package
onlyfun.caterpillar;
public
interface
IUserDAO
{
public
void
insert(User user);
public
User find(Integer id);
}
具体的DAO类如下:在这个类中我们模拟事务处理。
package
onlyfun.caterpillar;
import
java.util.Iterator;
import
java.util.List;
import
java.util.Map;
import
javax.sql.DataSource;
import
org.springframework.jdbc.core.JdbcTemplate;
public
class
UserDAO
implements
IUserDAO
{
private
JdbcTemplate jdbcTemplate;
public
void
setDataSource(DataSource dataSource)
{
jdbcTemplate
=
new
JdbcTemplate(dataSource);
}
public
void
insert(User user)
{
//
在这个方法中我们执行了数据插入和数据查询两个操作,用来模拟事务操作。我们故意在查询的语句中把数据库表写成smyuser。
//
这样,在insert方法执行过程中,会由于查询语句出错而撤销之前的插入语句的效果。
String name
=
user.getName();
int
age
=
user.getAge().intValue();
jdbcTemplate.update(
"
INSERT INTO myuser (name,age)
"
+
"
VALUES('
"
+
name
+
"
',
"
+
age
+
"
)
"
);
List rows
=
jdbcTemplate.queryForList(
"
SELECT * FROM smyuser WHERE id=4
"
);
Iterator it
=
rows.iterator();
if
(it.hasNext())
{
Map userMap
=
(Map) it.next();
Integer i
=
new
Integer(userMap.get(
"
id
"
).toString());
String names
=
userMap.get(
"
name
"
).toString();
Integer ages
=
new
Integer(userMap.get(
"
age
"
).toString());
User users
=
new
User();
users.setId(i);
users.setName(name);
users.setAge(age);
System.out.println(
"
names:
"
+
users.getName());
}
}
public
User find(Integer id)
{
List rows
=
jdbcTemplate.queryForList(
"
SELECT * FROM myuser WHERE id=
"
+
id.intValue());
Iterator it
=
rows.iterator();
if
(it.hasNext())
{
Map userMap
=
(Map) it.next();
Integer i
=
new
Integer(userMap.get(
"
id
"
).toString());
String name
=
userMap.get(
"
name
"
).toString();
Integer age
=
new
Integer(userMap.get(
"
age
"
).toString());
User user
=
new
User();
user.setId(i);
user.setName(name);
user.setAge(age);
return
user;
}
return
null
;
}
}
然后,在具体测试类中我们这样:
package
onlyfun.caterpillar;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.
support.FileSystemXmlApplicationContext;
public
class
SpringDAODemo
{
public
static
void
main(String[] args)
{
ApplicationContext context
=
new
FileSystemXmlApplicationContext(
"
beans-config.xml
"
);
User user
=
new
User();
user.setName(
"
1matthew
"
);
user.setAge(
new
Integer(
30
));
IUserDAO userDAO
=
(IUserDAO) context.getBean(
"
userDAOProxy
"
);
userDAO.insert(user);
user
=
userDAO.find(
new
Integer(
16
));
System.out.println(
"
name:
"
+
user.getName());
}
}
最后,我们来看配置文件:在这个文件中我们注入事务管理。
<?
xml version="1.0" encoding="UTF-8"
?>
<!
DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd"
>
<
beans
>
<
bean
id
="dataSource"
class
="org.apache.commons.dbcp.BasicDataSource"
destroy-method
="close"
>
<
property
name
="driverClassName"
>
<
value
>
com.mysql.jdbc.Driver
</
value
>
</
property
>
<
property
name
="url"
>
<
value
>
jdbc:mysql://localhost:3306/test
</
value
>
</
property
>
<
property
name
="username"
>
<
value
>
root
</
value
>
</
property
>
<
property
name
="password"
>
<
value
>
131421
</
value
>
</
property
>
</
bean
>
<
bean
id
="transactionManager"
class
="org.springframework.jdbc.datasource.DataSourceTransactionManager"
>
<
property
name
="dataSource"
>
<
ref
bean
="dataSource"
/>
</
property
>
</
bean
>
<
bean
id
="userDAO"
class
="onlyfun.caterpillar.UserDAO"
>
<
property
name
="dataSource"
>
<
ref
bean
="dataSource"
/>
</
property
>
</
bean
>
<
bean
id
="userDAOProxy"
class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
>
<
property
name
="proxyInterfaces"
>
<
list
>
<
value
>
onlyfun.caterpillar.IUserDAO
</
value
>
</
list
>
</
property
>
<
property
name
="target"
>
<
ref
bean
="userDAO"
/>
</
property
>
<
property
name
="transactionManager"
>
<
ref
bean
="transactionManager"
/>
</
property
>
<
property
name
="transactionAttributes"
>
<
props
>
<
prop
key
="insert*"
>
PROPAGATION_REQUIRED
</
prop
>
</
props
>
</
property
>
</
bean
>
</
beans
>
datasource这个bean中我们配置了数据源的相关属性。在userDAOProxy这个bean中,我们配置了target、transactionManager、transactionAttributes属性。在transactionAttributes中,我们指定所有insert*方法中操作会注入事务管理。
程序的运行结果是,由于在insert方法中的查询语句出错,会引起之前的insert语句效果被撤销。你也可以把查询语句先设置正确,看看效果。
posted on 2006-12-30 11:17
matthew
阅读(457)
评论(0)
编辑
收藏
所属分类:
阅读笔记
IT新闻
新用户注册
刷新评论列表
标题
姓名
主页
验证码
*
内容(请不要发表任何与政治相关的内容)
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
相关文章:
A Gentle Transition from XML to RDF—部分翻译
reading paper系列2
reading paper系列1
学习笔记-Springs声明性JDBC事务管理
Void类型的指针-C语言
《Core Java》中的一个例子程序-日历
《C Primer Plus》-在结构中使用字符数组还是字符指针?
《C Primer Plus》-指向结构的指针
《Understanding SOA With Web Services》-编制与编排规范
相关链接:
网站导航:
博客园
BlogJava
博客生活
IT博客网
C++博客
PHP博客
博客园社区
管理博客
教师博客
天文博客
汽车博客
足球博客
股票博客
电子博客
管理