Use OJB as a storage layer in the application (2)

zhaozj2021-02-12  142

Use the OJB PersistenceBroker API to implement various functions:

The above code is very simple, because there is no storage operation, just the exit of the program. Let us let us

Look at a more specific example: UclistallProducts class. This feature must contain a Collection class to include

All products in the database are then enumerated and displayed. In order to get all the database

Products, we need to use a method in the OJB API.

OJB provides three main APIs:

Persistencebroker

ODMG implementation

JDO implementation

In guidance 1, we use the PersistenceBroker API to implement all three features. Practice 2 D - use

ODMG API, Guided 4 D - Use the JDO API to use different database access methods to achieve the same function.

You can find the source code of the PersistenceBroker API in the org.apache.ojb.broker package. The most close

One component of the key is the PersistenceBroker interface. He offers objects, storage objects, and delete objects.

Features. In the actual use process, you need to get a Broker instance, configure the relevant O / R mapping relationship,

Use the features provided.

Get an instance of a Broker:

How to get a Broker instance? Let's find answers from the constructor of the Application class:

Public application ()

{

PersistenceBroker Broker = NULL;

Try

{

Broker = persistencebrokerfactory.

DEFAULTPERSISISTENCEBROKER ();

}

Catch (throwable t)

{

T.PrintStackTrace ();

}

Usecases = new vector ();

Usecases.add (New UclistallProducts);

Usecase.Add (New ucenternewproduct (brokeer);

Usecases.add (New UcdeleteProduct (Broker));

Usecases.add (new ucquitapplication);

}

The PersistenceBrokerFactory class uses ./repositoty.xml Create a Pesistence as a mapping repository

Broker's instance, the created PesistenceBroker instance passed as a parameter to the construct of four Usecase classes

Go in the function.

Get collections and itemors:

Below we have to do is to use this Broker instance to store operations. In this feature, we need to be numbered

According to libraries, you get Collection containing all products list. In order to meet some of the COLLECTION, I

We can use the PersistenceBroker.getCollectionByQuery (Query Query) method. Among them, QUE

RY is a class that provides special conditions such as Price> 100 or userid = 3. In our case, we want

Get all records stored in the Product table, so we don't need filtration conditions.

Below is the code of the uclistallproducts.apply () method:

Public void apply ()

{

System.out.println ("The List of Available Products);

// Build a Query That Selects All Objects of Class Product,

// welch any further criteria aciding 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 Itereate over the result to print each product

Java.util.ITerator it = allproducts.ITerator ();

While (item.hasnext ())

{

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

}

}

Catch (throwable t)

{

T.PrintStackTrace ();

}

}

转载请注明原文地址:https://www.9cbs.com/read-6262.html

New Post(0)