為了生活而生活
BlogJava
首頁
新隨筆
聯系
聚合
管理
隨筆 - 3 文章 - 1 trackbacks - 0
<
2025年7月
>
日
一
二
三
四
五
六
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
8
9
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(2)
給我留言
查看公開留言
查看私人留言
隨筆檔案
2008年2月 (1)
2007年4月 (1)
2007年3月 (1)
文章分類
ActiveMQ(1)
ajax(1)
eclipse(1)
hibernate(3)
java(2)
spring(4)
struts(1)
文章檔案
2009年7月 (1)
2008年4月 (2)
2008年2月 (1)
2007年12月 (1)
2007年4月 (2)
2007年3月 (5)
搜索
最新評論
1.?re: 不好的項目演示
雖然項目演示失務,但我相信你的項目是最成功的,因為你的態度,你的聰明.
You are NO.1! my lover
--roy117
閱讀排行榜
1.?StringUtils (400)
2.?不好的項目演示(266)
3.?快速的一年(142)
評論排行榜
1.?不好的項目演示(1)
2.?StringUtils (0)
3.?快速的一年(0)
轉 Spring+Hibernate配置事務
<?
xml version="1.0" encoding="UTF-8"
?>
<
beans
xmlns
="http://www.springframework.org/schema/beans"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
>
<
bean
id
="dataSource"
class
="org.apache.commons.dbcp.BasicDataSource"
>
<
property
name
="driverClassName"
>
<
value
>
com.microsoft.jdbc.sqlserver.SQLServerDriver
</
value
>
</
property
>
<
property
name
="url"
>
<
value
>
jdbc:microsoft:sqlserver://localhost:1433
</
value
>
</
property
>
<
property
name
="username"
>
<
value
>
sa
</
value
>
</
property
>
</
bean
>
<
bean
id
="sessionFactory"
class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
>
<
property
name
="dataSource"
>
<
ref
bean
="dataSource"
/>
</
property
>
<
property
name
="hibernateProperties"
>
<
props
>
<
prop
key
="hibernate.dialect"
>
org.hibernate.dialect.SQLServerDialect
</
prop
>
<
prop
key
="hibernate.show_sql"
>
true
</
prop
>
</
props
>
</
property
>
<
property
name
="mappingResources"
>
<
list
>
<
value
>
beans/Users.hbm.xml
</
value
>
</
list
>
</
property
>
</
bean
>
<
bean
id
="transactionManager"
class
="org.springframework.orm.hibernate3.HibernateTransactionManager"
>
<
property
name
="sessionFactory"
>
<
ref
local
="sessionFactory"
/>
</
property
>
</
bean
>
<
bean
id
="UsersDAO"
class
="beans.UsersDAO"
>
<
property
name
="sessionFactory"
>
<
ref
bean
="sessionFactory"
/>
</
property
>
</
bean
>
<
bean
id
="userDAOProxy"
class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
>
<
property
name
="transactionManager"
>
<
ref
local
="transactionManager"
/>
</
property
>
<!--
指定直接對類進行代理,將屬性proxyTargetClass指定為true(默認是false)
-->
<
property
name
="proxyTargetClass"
value
="true"
/>
<
property
name
="target"
>
<
ref
local
="UsersDAO"
/>
</
property
>
<
property
name
="transactionAttributes"
>
<
props
>
<!--
key表示匹配DAO中的方法名,例如:save*表示所有前綴為save的方法
-->
<
prop
key
="save*"
>
PROPAGATION_REQUIRED
</
prop
>
<
prop
key
="find*"
>
PROPAGATION_REQUIRED,readOnly
</
prop
>
<
prop
key
="delete*"
>
PROPAGATION_REQUIRED
</
prop
>
<
prop
key
="update*"
>
PROPAGATION_REQUIRED
</
prop
>
</
props
>
</
property
>
</
bean
>
</
beans
>
<!--
PROPAGATION_REQUIRED 支持當前事務,如果當前沒有事務,就新建一個事務。這是最常見的選擇。
PROPAGATION_SUPPORTS 支持當前事務,如果當前沒有事務,就以非事務方式執行。
PROPAGATION_MANDATORY 支持當前事務,如果當前沒有事務,就拋出異常。
PROPAGATION_REQUIRES_NEW 新建事務,如果當前存在事務,把當前事務掛起。
PROPAGATION_NOT_SUPPORTED 以非事務方式執行操作,如果當前存在事務,就把當前事務掛起。
PROPAGATION_NEVER 以非事務方式執行,如果當前存在事務,則拋出異常。
PROPAGATION_NESTED 如果當前存在事務,則在嵌套事務內執行。如果當前沒有事務,則進行與PROPAGATION_REQUIRED類似的操作。
-->
UsersDAO
package
beans;
import
java.util.List;
import
org.apache.commons.logging.Log;
import
org.apache.commons.logging.LogFactory;
import
org.hibernate.LockMode;
import
org.marker.spring.MyHibernateDao;
import
org.springframework.context.ApplicationContext;
public
class
UsersDAO
extends
MyHibernateDao
implements
UsersInterface
...
{
private
static
final
Log log
=
LogFactory.getLog(UsersDAO.
class
);
public
static
final
String NAME
=
"
name
"
;
public
static
final
String PWD
=
"
pwd
"
;
public
void
save(Users transientInstance)
...
{
log.debug(
"
saving Users instance
"
);
try
...
{
getHibernateTemplate().save(transientInstance);
log.debug(
"
save successful
"
);
}
catch
(RuntimeException re)
...
{
log.error(
"
save failed
"
, re);
throw
re;
}
}
public
void
delete(Users persistentInstance)
...
{
log.debug(
"
deleting Users instance
"
);
try
...
{
getHibernateTemplate().delete(persistentInstance);
log.debug(
"
delete successful
"
);
}
catch
(RuntimeException re)
...
{
log.error(
"
delete failed
"
, re);
throw
re;
}
}
public
Users findById(java.lang.Integer id)
...
{
log.debug(
"
getting Users instance with id:
"
+
id);
try
...
{
Users instance
=
(Users) getHibernateTemplate().get(
"
beans.Users
"
,
id);
return
instance;
}
catch
(RuntimeException re)
...
{
log.error(
"
get failed
"
, re);
throw
re;
}
}
public
void
update(Users user)
...
{
try
...
{
getHibernateTemplate().update(user);
log.debug(
"
update successful
"
);
}
catch
(RuntimeException re)
...
{
log.error(
"
update failed
"
,re);
}
}
}
UsersInterface
package
beans;
public
interface
UsersInterface
...
{
void
update(Users user);
}
JUnit單元測試
package
test;
import
junit.framework.TestCase;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import
beans.Users;
import
beans.UsersDAO;
public
class
UsersDAOTest
extends
TestCase
...
{
private
ApplicationContext context;
private
UsersDAO dao;
protected
void
setUp()
throws
Exception
...
{
context
=
new
ClassPathXmlApplicationContext(
"
applicationContext.xml
"
);
dao
=
(UsersDAO) context.getBean(
"
userDAOProxy
"
);
System.out.println(
"
%%%%% 單元測試開始 %%%%%
"
);
}
protected
void
tearDown()
throws
Exception
...
{
System.out.println(
"
%%%%% 單元測試結束 %%%%%
"
);
}
public
void
testSave()
...
{
Users user
=
new
Users();
user.setName(
"
777
"
);
user.setPwd(
"
888
"
);
dao.save(user);
}
public
void
testDelete()
...
{
Users user
=
new
Users();
user.setId(
28
);
dao.delete(user);
}
public
void
testFindById()
...
{
Users user
=
dao.findById(
26
);
assertNull(user.getName(), user.getId());
}
public
void
testUpdate()
...
{
Users user
=
new
Users();
user.setId(
22
);
user.setName(
"
654321
"
);
user.setPwd(
"
999999
"
);
dao.update(user);
}
}
posted on 2008-04-01 17:36
terryliu
閱讀(277)
評論(0)
編輯
收藏
所屬分類:
spring
、
hibernate
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關文章:
轉 Spring提供的Hibernate申明式事務管理有兩種辦法
轉 Spring+Hibernate配置事務
Spring中bean的基本xml配置
Spring控制反轉(IoC)的理解
Copyright ©2025 terryliu Powered by:
博客園
模板提供:
滬江博客
主站蜘蛛池模板:
临武县
|
沽源县
|
湾仔区
|
荆州市
|
北安市
|
昂仁县
|
伊春市
|
韩城市
|
梅河口市
|
嘉义市
|
若羌县
|
安仁县
|
长宁县
|
兴安县
|
永州市
|
奉新县
|
于都县
|
南城县
|
吉安县
|
浦江县
|
迁安市
|
白朗县
|
济源市
|
乌审旗
|
舟曲县
|
衡东县
|
满洲里市
|
绥江县
|
依兰县
|
鲁甸县
|
铜山县
|
美姑县
|
栾城县
|
广德县
|
乃东县
|
阿巴嘎旗
|
西华县
|
亚东县
|
前郭尔
|
长子县
|
通河县
|