java學習
java學習
用配置事務
在applicationContext.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:p
=
"
http://www.springframework.org/schema/p
"
xmlns:aop
=
"
http://www.springframework.org/schema/aop
"
xmlns:tx
=
"
http://www.springframework.org/schema/tx
"
xmlns:context
=
"
http://www.springframework.org/schema/context
"
xsi:schemaLocation
=
"
http:
//
www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http:
//
www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http:
//
www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http:
//
www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
"
>
<!--
使用注解支持事務
<
tx:annotation
-
driven transaction
-
manager
=
"
transactionManager
"
/>
-->
<!--
添加jdbc的任務管理器
-->
<
bean id
=
"
transactionManager
"
class
=
"
org.springframework.jdbc.datasource.DataSourceTransactionManager
"
>
<
property name
=
"
dataSource
"
ref
=
"
dataSource
"
></
property
>
</
bean
>
<!--
配置事務通知
-->
<
tx:advice id
=
"
txAdvice
"
transaction
-
manager
=
"
transactionManager
"
>
<
tx:attributes
>
<!--
name表示以什么開始的方法名,比如 add
*
表示add開頭的方法
propagation表示事務傳播屬性,不寫默認有
-->
<
tx:method name
=
"
save*
"
propagation
=
"
REQUIRED
"
/>
<
tx:method name
=
"
del*
"
/>
<
tx:method name
=
"
update*
"
/>
<
tx:method name
=
"
find*
"
read
-
only
=
"
true
"
/>
</
tx:attributes
>
</
tx:advice
>
<!--
配置事務切面
-->
<
aop:config
>
<
aop:pointcut expression
=
"
execution(* com.yjw.service..*.*(..))
"
id
=
"
pointcut
"
/>
<
aop:advisor advice
-
ref
=
"
txAdvice
"
pointcut
-
ref
=
"
pointcut
"
/>
</
aop:config
>
<
bean id
=
"
dataSource
"
class
=
"
com.mchange.v2.c3p0.ComboPooledDataSource
"
>
<
property name
=
"
driverClass
"
value
=
"
com.mysql.jdbc.Driver
"
/>
<
property name
=
"
jdbcUrl
"
value
=
"
jdbc:mysql:///mydb
"
/>
<
property name
=
"
properties
"
>
<
props
>
<
prop key
=
"
user
"
>
root
</
prop
>
<
prop key
=
"
password
"
>
root
</
prop
>
</
props
>
</
property
>
</
bean
>
<
bean id
=
"
simpleJdbcTemplate
"
class
=
"
org.springframework.jdbc.core.simple.SimpleJdbcTemplate
"
>
<
constructor
-
arg ref
=
"
dataSource
"
></
constructor
-
arg
>
</
bean
>
<!--
在使用事務時不能配置jdbc模板,只能配置數據流
-->
<
bean id
=
"
userdao
"
class
=
"
com.yjw.dao.UserDao
"
>
<
property name
=
"
dataSource
"
ref
=
"
dataSource
"
></
property
>
</
bean
>
<
bean id
=
"
userService
"
class
=
"
com.yjw.service.UserService
"
>
<!--
name的值只和set方法后面的有關,要一樣
-->
<
property name
=
"
dao
"
ref
=
"
userdao
"
></
property
>
</
bean
>
</
beans
>
在service里寫:
public
class
UserService
{
private
UserDao userdao;
public
void
setDao(UserDao userdao)
{
this
.userdao
=
userdao;
}
public
void
save(User user)
{
userdao.save(user);
if
(
true
)
{
throw
new
RuntimeException();
}
userdao.save(user);
}
//
只讀,會使性能提高,推薦使用
//
@Transactional(readOnly=true)
public
User findById(
int
id)
{
return
userdao.findById(id);
}
}
posted on 2012-05-03 22:17
楊軍威
閱讀(1685)
評論(0)
編輯
收藏
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
導航
BlogJava
首頁
新隨筆
聯系
聚合
管理
統計
隨筆 - 391
文章 - 34
評論 - 20
引用 - 0
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
給我留言
查看公開留言
查看私人留言
隨筆檔案
2021年9月 (1)
2021年6月 (1)
2021年4月 (1)
2021年1月 (1)
2020年12月 (1)
2020年11月 (2)
2020年10月 (1)
2020年8月 (2)
2020年7月 (2)
2020年5月 (1)
2020年4月 (7)
2020年3月 (4)
2019年11月 (1)
2019年6月 (1)
2019年2月 (1)
2018年11月 (2)
2018年8月 (1)
2018年7月 (1)
2018年6月 (1)
2018年5月 (8)
2018年4月 (3)
2018年3月 (10)
2018年2月 (5)
2018年1月 (5)
2017年12月 (12)
2017年11月 (7)
2017年9月 (13)
2017年8月 (13)
2017年7月 (10)
2017年6月 (2)
2017年3月 (3)
2017年2月 (5)
2016年9月 (2)
2016年8月 (2)
2016年7月 (3)
2016年6月 (2)
2016年5月 (4)
2016年4月 (1)
2014年12月 (6)
2013年11月 (1)
2013年9月 (4)
2013年8月 (16)
2013年7月 (6)
2013年6月 (27)
2013年5月 (21)
2013年4月 (48)
2013年3月 (61)
2013年2月 (11)
2013年1月 (33)
2012年12月 (19)
2012年11月 (8)
2012年10月 (2)
2012年9月 (2)
2012年5月 (6)
2012年4月 (4)
2011年11月 (8)
搜索
最新評論
1.?re: Form 表單域與 Java 對象屬性的自動裝配功能[未登錄]
沒排版,看著好累
--alex
2.?re: struts2實現登錄攔截器和驗證方法
嗯嗯
--嗯嗯
3.?re: 用struts2給的jar包解析json
阿斯頓發送到
--阿斯蒂芬
4.?re: ApplicationContextAware接口的作用
解決
--解決
5.?re: jfinal攔截器筆記5[未登錄]
2132
--123
閱讀排行榜
1.?ApplicationContextAware接口的作用(37201)
2.?svn使用(12309)
3.?git 解決 unable to get local issuer certificate 問題(10187)
4.?jfinal攔截器筆記5(8696)
5.?$.cookie的用法(8170)
評論排行榜
1.?struts2實現登錄攔截器和驗證方法(5)
2.?jquery阻止表單提交(3)
3.?jfinal筆記1(2)
4.?extjs和highcharts整合顯示數據曲線和打印圖片(2)
5.?XPath解析,寫入,修改xml文件(1)
Powered by:
BlogJava
Copyright © 楊軍威
主站蜘蛛池模板:
阜平县
|
平安县
|
五河县
|
曲水县
|
平阳县
|
同心县
|
鸡西市
|
桑日县
|
沽源县
|
湖北省
|
阿拉尔市
|
六安市
|
苗栗县
|
平和县
|
盘山县
|
松江区
|
通河县
|
抚宁县
|
闸北区
|
财经
|
西乌珠穆沁旗
|
宁蒗
|
沅陵县
|
麻城市
|
潜山县
|
托里县
|
连州市
|
迁西县
|
拜城县
|
盐边县
|
手机
|
温州市
|
花莲县
|
安陆市
|
读书
|
航空
|
来凤县
|
桃园市
|
太保市
|
深州市
|
延寿县
|