溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

在應(yīng)用程序中將OJB作為一個(gè)存儲(chǔ)層使用(二) (轉(zhuǎn))

發(fā)布時(shí)間:2020-08-05 02:15:22 來源:ITPUB博客 閱讀:185 作者:gugu99 欄目:編程語(yǔ)言
在應(yīng)用程序中將OJB作為一個(gè)存儲(chǔ)層使用(二) (轉(zhuǎn))[@more@]

用OJB PersistenceBroker api實(shí)現(xiàn)各種功能:

上面的一段代碼很簡(jiǎn)單,因?yàn)闆]有涉及到存儲(chǔ)操作,僅僅是程序的退出。下面讓我們來

看一個(gè)更具體的例子:UCListAllProducts類。該功能必須含有一個(gè)Collection類來包含

數(shù)據(jù)庫(kù)中的所有產(chǎn)品,然后將所有產(chǎn)品一一枚舉并顯示出來。為了得到數(shù)據(jù)庫(kù)中的所有

產(chǎn)品,我們需要使用OJB API中的一個(gè)方法。

OJB提供三個(gè)主要的API:

PersistenceBroker

ODMG實(shí)現(xiàn)

JDO實(shí)現(xiàn)

在導(dǎo)學(xué)1中,我們使用PersistenceBroker API來實(shí)現(xiàn)所有的三個(gè)功能。導(dǎo)學(xué)2 D――使用

ODMG API,導(dǎo)學(xué)4 D――使用JDO API將使用不同的數(shù)據(jù)庫(kù)訪問方法來實(shí)現(xiàn)同樣的功能。

 

你可以在org.apache.ojb.broker包中找到PersistenceBroker API的源碼。該包中最關(guān)

鍵的一個(gè)組件就是PersistenceBroker接口。他提供了獲得對(duì)象,存儲(chǔ)對(duì)象,刪除對(duì)象的

功能。在實(shí)際使用過程中,你需要獲得一個(gè)Broker實(shí)例,配置相關(guān)的O/R映射關(guān)系,才能

使用其提供的功能。

獲得一個(gè)Broker實(shí)例:

怎樣獲得一個(gè)Broker實(shí)例?讓我們來從Application類的構(gòu)造函數(shù)中找答案:

public Application()

{

  PersistenceBroker broker = null;

  try

  {

  broker = PersistenceBrokerFactory.

  defaultPersistenceBroker();

  }

  catch (Throwable t)

  {

  t.printStackTrace();

  }

  useCases = new Vector();

  useCases.add(new UCListAllProducts(broker));

  useCases.add(new UCEnterNewProduct(broker));

  useCases.add(new UCDeleteProduct(broker));

  useCases.add(new UCQuitApplication(broker));

}

PersistenceBrokerFactory類使用./repositoty.XML作為映射倉(cāng)庫(kù)創(chuàng)建一個(gè)Pesistence

Broker的實(shí)例,被創(chuàng)建的PesistenceBroker實(shí)例作為一個(gè)參數(shù)傳到四個(gè)UseCase類的構(gòu)造

函數(shù)中去。

獲得Collections和Iterators:

下面我們要做的就是用這個(gè)broker實(shí)例來進(jìn)行存儲(chǔ)操作。在這個(gè)功能中,我們需要從數(shù)

據(jù)庫(kù)中獲得包含所有產(chǎn)品列表的collection。為了獲得滿足一些條件的collection,我

們可以使用PersistenceBroker.getCollectionByQuery(Query query)方法。其中,Que

ry是一個(gè)類,它提供特殊的條件如price>100或者userId=3.在我們的案例中,我們想要

獲得存儲(chǔ)在Product表中的所有記錄,所以我們不需要過濾條件。

下面是UCListAllProducts.apply()方法的代碼:

public void apply()

{

  System.out.println("The list of available products:");

  // build a query that selects all objects of Class Product,

  // without any further criteria according to ODMG the

  // Collection containing all instances of a

  // persistent class is called "Extent"

  Query query = new QueryByCriteria(Product.class, null);

  try

  {

  // ask the broker to retrieve the Extent collection

  Collection allProducts = broker.getCollectionByQuery(query);

  // now iterate over the result to print each product

  Java.util.Iterator iter = allProducts.iterator();

  while (iter.hasNext())

  {

  System.out.println(iter.next());

  }

  }

  catch (Throwable t)

  {

  t.printStackTrace();

  }

}


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI