Kira-2006
-僅僅是一陣風也罷了,偏偏是這樣永恒, 僅僅是一場夢也罷了,偏偏是如此的真實,
BlogJava
首頁
新隨筆
新文章
聯(lián)系
聚合
管理
posts - 4,comments - 7,trackbacks - 0
<
2025年5月
>
日
一
二
三
四
五
六
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
常用鏈接
我的隨筆
我的文章
我的評論
我的參與
最新評論
留言簿
(2)
給我留言
查看公開留言
查看私人留言
隨筆檔案
(3)
2008年4月 (2)
2006年3月 (1)
文章分類
(8)
design pattern
hibernate(6)
hsql(2)
java
文章檔案
(10)
2008年5月 (4)
2008年4月 (4)
2006年9月 (1)
2006年3月 (1)
中國通信建設(shè)集團設(shè)計院有限公司第三分公司
中國通信建設(shè)集團設(shè)計院有限公司第三分公司
搜索
最新評論
1.?re: Hibernate---SQL中datetime的映射[未登錄]
aaa
--s
2.?re: hsqldb編寫批處理文件啟動自己創(chuàng)建的數(shù)據(jù)庫
評論內(nèi)容較長,點擊標題查看
--懸殊
3.?re: Hibernate---SQL中datetime的映射
type="timestamp"
--cwjcsu@126.com
4.?re: myeclipse自帶Struts缺少jar文件,datasource配置
@隔葉黃鶯
容器的數(shù)據(jù)源是什么意思呀?如果在struts1.3中手工加入數(shù)據(jù)源,會出錯嗎?
--tayoto
5.?re: myeclipse自帶Struts缺少jar文件,datasource配置
Struts 不建議在 struts-config.xml 中配置數(shù)據(jù)源,用容器的數(shù)據(jù)源吧。
所以 Struts1.3開始廢除了在struts-config.xml中配置數(shù)據(jù)源。
--隔葉黃鶯
閱讀排行榜
1.?Hibernate---SQL中datetime的映射(2754)
2.?myeclipse自帶Struts缺少jar文件,datasource配置(1263)
3.?一個計算機系學生的困惑?(344)
評論排行榜
1.?Hibernate---SQL中datetime的映射(2)
2.?myeclipse自帶Struts缺少jar文件,datasource配置(2)
3.?一個計算機系學生的困惑?(2)
深入淺出Hibernate筆記--1.2.1持久化設(shè)計與解耦
解耦合的設(shè)計目標:
1. 應(yīng)用層解耦合--應(yīng)用邏輯與數(shù)據(jù)邏輯相分離。
2. 資源層解耦合--邏輯結(jié)構(gòu)與物理結(jié)構(gòu)相分離。
DAO模式:
即Data Accessor模式和Active Domain Object模式。
Data Accessor模式:實現(xiàn)數(shù)據(jù)訪問和業(yè)務(wù)邏輯的分離。
Active Domain Object:實現(xiàn)了業(yè)務(wù)數(shù)據(jù)的對象化封裝。
Domain Object:簡單來講就是對領(lǐng)域內(nèi)涉及的各個數(shù)據(jù)對象,反映到代碼,就是一個擁有相關(guān)屬性的getter,setter方法的java Bean。
DAO模式通過對業(yè)務(wù)邏輯蹭提供數(shù)據(jù)抽象層接口,實現(xiàn)了以下目標:
1. 數(shù)據(jù)存儲邏輯的分離:通過對數(shù)據(jù)訪問邏輯進行抽象,為上層結(jié)構(gòu)提供抽象化的數(shù)據(jù)訪問接口。
2. 數(shù)據(jù)訪問底層實現(xiàn)的分離:數(shù)據(jù)訪問劃分為抽象層和實現(xiàn)層,從而分離了數(shù)據(jù)使用和數(shù)據(jù)訪問的底層實現(xiàn)細節(jié)。
3. 資源管理和調(diào)用的分離。
4.數(shù)據(jù)抽象:DAO模式通過對底層數(shù)據(jù)的封裝,為業(yè)務(wù)層提供了一個面向?qū)ο蟮慕涌?,使得業(yè)務(wù)邏輯開發(fā)人員可以面向業(yè)務(wù)中的實體進行編程。
DAO = Data+Accessor+Domain Object
DAO模式的進一步改良
Factory模式的引入:
由于需要針對不同的數(shù)據(jù)庫訪問機制分別提供各個版本的Data Accessor實現(xiàn),自然我們會想到通過java interface定義一個調(diào)用接口,然后針對這個調(diào)用接口實現(xiàn)不同數(shù)據(jù)庫的Data Accessor。通過接口作為調(diào)用界面和實現(xiàn)規(guī)范,可以避免對具體實現(xiàn)的依賴。
public
interface
CustomerDAO
{
public
Customer getCustomer(String custID);
public
void
save(Customer customer);
}
作為最常見的創(chuàng)建模式,F(xiàn)actory模式在這里起到連接接口和實現(xiàn)的橋梁作用,通過Factory模式,我們可以根據(jù)具體的需要加載相應(yīng)的實現(xiàn),并將此實現(xiàn)作為所對應(yīng)接口的一個實例提供給業(yè)務(wù)層使用。
CustomerDAO custDAO
=
(CustomerDAO)DAOFactory.getDAO(CustomerDAO.
class
);
Customer customer
=
custDAO.getCustomer(custoemrID);
業(yè)務(wù)邏輯層通過接口調(diào)用底層實現(xiàn),具體的DAO實現(xiàn)類不會出現(xiàn)在我們的業(yè)務(wù)代碼中。而具體實現(xiàn)類在配置文件中加以配置,之后DAOFactory.getDAO方法通過讀取配置文件獲得當前我們期望使用的實現(xiàn)類的類名,再通過java Class動態(tài)加載機制加載返回。
從而我們的代碼不依賴于某個特定的實現(xiàn)類,只需要在部署的時候在配置文件中指定當前采用的實現(xiàn)類即可。
public
class
DAOFactory
{
private
static
HashMap daoMap
=
null
;
//
根據(jù)指定的Class來獲取DAO實例
public
static
Object getDAO(Class daoInterface)
{
initial();
Object dao
=
daoMap.get(daoInterface);
if
(
null
==
dao)
{
throw
new
DAOException(
"
No implementation found of DAO interface =>
"
+
daoInterface.getName());
}
return
dao;
}
//
初始化DAOFactory,加載DAO interface和
//
implementation到daoMap中
public
static
sychronized
void
initial()
{
if
(
null
==
daoMap)
{
//
根據(jù)配置文件中加載DAO實現(xiàn)配置
daoMap
=
DAOConfig.load();
}
}
}
//
DAOConfig類實現(xiàn)了配置文件的讀取功能,并根據(jù)配文
//
件中的內(nèi)容加載指定的接口和實現(xiàn)
public
class
DAOConfig
{
private
static
Logger logger
=
LogManager.getLogger(DAOConfig.
class
);
private
static
final
String DAO_CONFIG_FILE
=
"
dao.xml
"
;
private
static
final
String DAO_CONFIG_SECTION
=
"
DAO
"
;
public
static
synchronized
HashMap load()
{
HashMap map
=
new
HashMap();
JFigLocator jfigLocator
=
JFig.getInstance(jfigLocator);
Properties prop
=
daoConfig.getSectionAsProperties(DAO_CONFIG_SECTION);
Enumeration enumSection
=
prop.Keys();
while
(enumSection.hasMoreElements())
{
String daoIface
=
(String)enumSectioni.nextElement();
String daoImpl
=
(String)prop.getProperty(daoIface);
try
{
Class iface
=
ClassToolKit.loadClass(daoIface);
Class impl
=
ClassToolKit.loadClass(daoImpl);
//
將接口作為索引,實現(xiàn)作為值。
map.put(iface,impl);
}
catch
(ClassNotFoundException e)
{
logger.debug(
"
No Class Found =>
"
+
e);
}
}
return
map;
}
}
ClassToolKit.loadClass方法實現(xiàn)了類文件的動態(tài)加載:
public
class
ClassToolKit
{
public
static
Class loadClass(String className)
throws
ClassNotFoundException
{
Class cls
=
null
;
try
{
//
首先嘗試用當前ClassLoader加載
cls
=
Thread.currentThread().getContextClassLoader().loadClass(className);
}
catch
(Exception e)
{
e.printStackTrace();
}
if
(cls
==
null
)
{
//
如果通過當前ClassLoader加載失敗,使
//
用系統(tǒng)ClassLoader加載
cls
=
Class.forName(className);
}
return
cls;
}
}
這樣,通過接口與實現(xiàn)的分離,并結(jié)合DAOFactory動態(tài)加載實現(xiàn)類,我們就實現(xiàn)了底層訪問實現(xiàn)的參數(shù)化配置功能。從而為增強產(chǎn)品的部署能力提供了強有力的支持。
經(jīng)過Factory模式的改造,業(yè)務(wù)層代碼進行相應(yīng)的修改:
public
BigDecimal calcAmount(String customerID,BigDecimal amount)
{
//
根據(jù)客戶ID獲得客戶記錄
CustomerDAO customerDAO
=
(CustomerDAO)DAOFactory.getDAO(CustomerDAO.
class
);
Customercustomer
=
customerDAO.getCustomer(customerID);
//
根據(jù)客戶等級獲得打折比率
PromotionDAO promoDAO
=
(PromotionDAO)DAOFactory.getDAO(PromotionDAO.
class
);
Promotion promotion
=
promoDAO.getPromotion(customer.getLevel());
//
累計客戶總消費額,并更新數(shù)據(jù)庫
customer.setSumAmount(customer.getSumAmount().add(amount));
customerDAO.save(customer);
//
返回打折后金額
return
amount.multiply(promotion.getRatio());
}
這段代碼中混雜了數(shù)據(jù)訪問層的內(nèi)容,如DAOFactory.getDAO方法的調(diào)用。
Proxy模式的引入
為了保持業(yè)務(wù)邏輯代碼的簡潔,將Factory模式帶來的Bad Smell排除在系統(tǒng)外,引入Proxy模式。
Proxy模式的作用:通過提供一個中間層(Proxy),將上層調(diào)用接口與下層實現(xiàn)相銜接。
經(jīng)過Proxy模式改進后的業(yè)務(wù)層代碼:
public
BigDecimal calcAmount(String customerID,BigDecimal amount)
{
Customer customer
=
CustomerProxy.getCustomer(customerID);
Promotion promotion
=
PromotionProxy.getPromotion(customer.getLevel());
customer.setSumAmount(customer.getSumAmount.add(amount));
CustomerProxy.save(customer);
return
amount.multiply(promotion.getRatio());
}
public
class
CustomerProxy
{
//
get Customer Object by CustomerID
public
static
Customer getCustomer(String customerID)
{
customerDAO custDAO
=
(CustomerDAO)DAOFactory.getDAO(CustomerDAO.
class
);
return
custDAO.getCustomer(customerID);
}
//
Save Customer Object to DataBase
public
static
void
save(Customer customer)
{
CustomerDAO custDAO
=
(CUstomerDAO)DAOFactory.getDAO(CustomerDAO.
class
);
custDAO.save(customer);
}
}
posted on 2008-05-02 19:33
Kira-2006
閱讀(401)
評論(0)
編輯
收藏
所屬分類:
hibernate
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發(fā)表評論。
網(wǎng)站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關(guān)文章:
深入淺出Hibernate讀書筆記--Hibernate常見優(yōu)化策略
深入淺出Hibernate學習筆記--Criteria Query
深入淺出Hibernate學習筆記--數(shù)據(jù)關(guān)聯(lián)
深入淺出Hibernate筆記--1.2.1持久化設(shè)計與解耦
Hibernate生成器
Copyright ©2025 Kira-2006 Powered By
博客園
模板提供:
滬江博客
主站蜘蛛池模板:
亚洲中文字幕不卡无码
|
成人黄色免费网址
|
四虎影库久免费视频
|
亚洲精品自偷自拍无码
|
3d成人免费动漫在线观看
|
亚洲av无码一区二区三区乱子伦
|
男女免费观看在线爽爽爽视频
|
亚洲黄色免费观看
|
99久久免费中文字幕精品
|
亚洲高清在线mv
|
国产人成免费视频网站
|
亚洲综合校园春色
|
午夜免费福利在线观看
|
91av免费观看
|
亚洲一区二区三区国产精品无码
|
成年女人午夜毛片免费视频
|
亚洲日本在线电影
|
日本免费福利视频
|
伊人久久国产免费观看视频
|
自拍偷自拍亚洲精品第1页
|
成人久久免费网站
|
免费国产成人高清视频网站
|
ww在线观视频免费观看w
|
久久精品国产亚洲一区二区
|
0588影视手机免费看片
|
亚洲欧美日韩综合久久久
|
日韩亚洲国产二区
|
亚洲一卡2卡4卡5卡6卡在线99
|
国产成人A在线观看视频免费
|
自拍偷自拍亚洲精品播放
|
ww4545四虎永久免费地址
|
亚洲性无码AV中文字幕
|
免费夜色污私人影院在线观看
|
少妇性饥渴无码A区免费
|
亚洲国产精品久久网午夜
|
久久国产精品免费专区
|
亚洲精品成人无码中文毛片不卡
|
最近新韩国日本免费观看
|
亚洲日日做天天做日日谢
|
国产国拍精品亚洲AV片
|
国产免费不卡视频
|