Use Websharpdao to achieve OR mapping and persistence layer

xiaoxiao2021-03-06  41

1. What can WebSharpdao do?

Websharpdao encapsulates interactions between objects with databases that can easily perform some commonly used databases and object interactions. Websharpdao is a very simple framework, his purpose is not to design a complete solution as JDO, Hibernate, but design a usable solution to solve the general problem during the development process. He is much simpler than JDO and Hibernate, which is convenient to use. In the interface design, also refer to JDO standard, or you can see him as a MINI version of JDO.

You can download all source code and sample works from the following URL:

http://www.uml.org.cn/opensource/websharp/

2. Main interface

The main interface of the PersistenceManager interface, manipulating the object with the database:

Public Interface PersistenceManager

{

Void Close () throws SqlexCeption;

Boolean isclosed () throws sqlexception;

Transaction CurrentTransaction ();

Void PersistneWobject (PersistenceCapable PC).

Void UpdateObject (PersistenceCapable PC).

Void deleteObject (PersistenceCapable PC).

Void Reload (PersistenceCapable PC) THROWS SQLEXCEPTION;

PersistenceCapable FindObjectByPrimaryKey (Object ID, PersistenceCapable PC).

Query newquery ();

Query NewQuery (Class EntityType);

Query NewQuery (Class EntityType, String Filter);

Query NewQuery (Class EntityType, String Filter, ArrayList Paramcollet);

CONNECTION getConnection ();

}

Transaction interface, manipulating transaction processing:

Public Interface Transaction

{

Void Begin () THROWS SQLEXCEPTION;

Void commit () throws sqlexception;

Void rollback () throws SqlexCeption;

PersistenceManager getPersistenceManager ();

}

Query interface, query object:

Public Interface Query

{

Class getentityType ();

Void SeTentityType (Class EntityType);

String getfilter ();

Void setfilter;

ArrayList getParameters ();

Void setParameters (arraylist array);

Void AddParameter (Object Param);

String getordering ();

void setORDERING (String Ordering);

Public boolean ignorecache = true;

Persistencecapable [] queryObject () throws sqlexception; persistenceManager getPersistenceManager ();

Boolean isclosed ();

Void close ();

Sqloperator getsqloperty ();

Void Setsqloperator (Sqloperator CONN);

}

Sqloperator interface, packaged some commonly used database manipulation functions:

Public Interface Sqloperator

{

ResultSet ExecuteQuery (String SQL) THROWS SQLEXCEPTION;

ResultSet ExecuteQuery (String SQL, ArrayList Parameters).

RowSet ExecuterowSet (String SQL) THROWS SQLEXCEPTION;

RowSet Executerowset (String SQL, ArrayList Parameters) Throws SqlexCeption;

INT ExecuteUpdate (String SQL) THROWS SQLEXCEPTION;

Int ExecuteUpdate (String SQL, ArrayList Parameters) Throws SqlexCeption;

Void Close () throws SqlexCeption;

Boolean isclosed () throws sqlexception;

CONNECTION getConnection ();

}

PersistenceCapable interface, all entity classes must implement this interface:

Public Interface Persistencecapable

{

Public String [] getfields (); // field list

Public string [] getKeyfields (); // keyword

Public string gettablename (); // Corresponding table name

}

3. Preparation specification of the entity class:

a) In order to facilitate it, the physical class with the database table is one or one.

b) All physical classes must implement the PersistenceCapable interface. For example, the Product class can be represented as follows:

Public Class Product Implements Persistencecapable

{

Public product () PUBLIC Product ()

{

Super ();

}

Public String [] getfields ()

{

Return New

String [] {"Productid", "Name", "UnitName", "Description", "Price", "CurrentCount"}

}

Public String [] getKeyfields ()

{

Return new string [] {"productID"};

}

Public string gettablename ()

{

Return "Product";

}

Private string products = ""

Public string getProductId ()

{

Return ProductId;

}

Public void setProductId (String ProductID)

{

This.ProductId = ProductId;

}

Private string name = ""; public string getname ()

{

Return Name;

}

Public void setname (String name)

{

THIS.NAME = Name;

}

Private string unitname = ""

Public String getUnitname ()

{

Return UnitName;

}

Public void setUnitName (String UnitName)

{

THIS.UnitName = UnitName;

}

Private string description = ""

Public string getdescription ()

{

Return description;

}

Public void setdescription (String Description)

{

THIS.DESCRIPTION = DESCRIPTION;

}

PRIVATE DOUBLE = 0;

Public Double getPrice ()

{

Return Price;

}

Public void setprice (double price)

{

this.price = price;

}

Private double currentcount = 0;

Public double getcurrentcount ()

{

Return CurrentCount;

}

Public void setcurrentcount (double currentcount)

{

THIS.CURRENTCOUNT = CURRENTCOUNT;

}

}

c) The attribute must correspond to the same field. For example, there is a ProductName field, you must have the corresponding GET and SET methods, pay attention to the case of the GET and SET methods and the case in the getFields () method.

4. Using the PersistenceManager access object

The process of accessing objects using the PersistenceManager is:

1) Instantiate a PersistenceManager object. To instantiate a PersistenceManager object, please pass the CreatePersistenceManager method for PersistenceManagerFactory. Below is an example:

PersistenceManager PM = NULL;

Try

{

PM = persistenceManagerFactory.instance (). CREATEPERSISISTENCEMANAGER ();

...;

}

2) Instantiate an entity object:

Product p = new product ();

P.ProductId = ....

3) Call the PersistenceManager corresponding method:

PM.PersistneWobject (p);

or:

PM.UPDateObject (dept);

or

Pm.deleteObject (dept);

4) Close PersistenceManager

PM.Close ();

Here is a complete example:

ProductForm Productform = (Productform) Form;

Product p = new product ();

P.SetProductId (Productform.getProductId ());

P.setname (Productform.getName ()); P.SetUnitName (Productform.getUnitName ());

P.SETPRICE (Productform.getPrice (). DoubleValue ());

P.SetCurrentcount (Productform.getCurrent (). DoubleValue ());

P.SetDescription (Productform.getDescription ());

Try

{

PersistenceManager PM =

PersistenceManagerFactory.Instance (). CreatePersistenceManager ();

PM.PersistneWobject (p);

PM.Close ();

}

Catch (Exception E)

{

Errors.Add ("Name", New ActionError ("ID"));

}

5. Use transaction processing

Transaction processing is performed by Transaction interface.

Try

{

PersistenceManager PM =

PersistenceManagerFactory.Instance (). CreatePersistenceManager ();

Transaction trans = PM.currentTransaction ();

Trans.begin ();

PM.PersistneWobject (p);

TRANS.COMMIT ();

}

Catch (Exception E)

{

TRANS. ROLLBACK ();

}

Finally

{

PM.Close ();

}

6. Inquiries for objects

The query object can be conducted in two ways:

1) query a single object according to the primary keyword

You can do it through the FindObjectByPrimaryKey method of the PersistenceManager. Below is an example:

DEPT Dept = New DEPT ();

PersistenceManager PM = NULL;

Try

{

PM = persistenceManagerFactory.instance (). CREATEPERSISISTENCEMANAGER ();

PM.FindObjectByPrimaryKey (New Integer (90), DEPT);

PM.Close ();

System.out.println (dept.getdeptno ());

System.out.println (dept.getdname ());

System.out.println (dept.getloc ());

}

Catch (Exception E)

{

System.out.println (E.TOString ());

}

2) Query a set of objects according to the conditions

This function can be done through the Query interface. Below is an example:

PersistenceManager PM = NULL;

Try

{

PM = persistenceManagerFactory.instance (). CREATEPERSISISTENCEMANAGER ();

Query Q = PM.NewQuery (de PEPT.CLASS);

Q. setfilter ("deptno> 10");

Persistencecapable [] depts = Q.QueryObject ();

For (int i = 0; i

{

System.out.print (i);

System.out.print ("/ t");

System.out.print ((dept) dePTS [i]); getDeptno ()); system.out.print ("/ t");

System.out.print ((DEPT) DEPTS [I]). GetDName ());

System.out.print ("/ t");

System.out.println ((DEPT) DEPTS [I]). Getloc ());

}

}

Catch (Exception E)

{

System.out.println (E.TOString ());

}

7. Get of database connections

Sometimes, we also need to use database connections to make some operations for the database. In a project, you should always get a connection via ConnectionFactory.

You can use the following statement to get a default database connection:

Connection conn = connectionFactory.getConnection ()

You can also get a specific connection by indicating a DataSource name:

Connection conn = connectionsFactory.getConnection (DataSourceName)

Once you have been connected, you can use the obtained connection like using the usual database connection.

Some parameters connected to the Connection must be set in the ApplicationResources.properties file. The format is:

DB_FROMDATASOURCE = FALSE Whether to use DataSource

DB_DataSourceName = DataSource name

DB_URL = JDBC: Oracle: Thin: @newte database connection URL

DB_Driver = Oracle.jdbc.driver.Oracledriver JDBC driver

DB_USER = User Database User Name

DB_Password = PWD database password

8. Use of Sqloperator

You can also get a SQLOPERATOR with ConnectionFactory. Sqloperator definition See the previous content. Here are two ways:

Sqloperator Operator = ConnectionFactory. Getsqloperty ()

Sqloperator Operator = ConnectionFactory. Getsqloperty (DataSourceName)

Let's execute a query by SQLOPERATOR, returns a resultset.

String SQL = "SELECT * from Product Where Price";

ArrayList parameters = new arraylist (1);

Parameters.Add (100);

SQLOPERATOR Operator = connectionFactory.getsqloperty ()

ResultSet Rst = Operator.executeQuery (SQL, Params);

......

Operator.close ();

You can also return a rowset with SQLOPERATOR. Returns the use of RowSet as the result.

RowSet is the same as ResultSet, but it is a dataset, but it can be disconnected and can scroll backward data sets. When the data is transmitted between the layers, please try to use RowSet so that we can get a dataset at the business logic layer, then disconnect the database connection immediately, then transfer the data set to the interface layer. For other methods of SQLOPERATOR, please refer to the front interface description.

9. Tomcat database connection pool configuration

First, add a environment variable named "Websharp / DataSourceName" in the web.xml file of the project to specify the name of the DataSource used. The format is as follows:

......

ORG / Websharp / DAO / DATASOURCENAME

JDBC / NewTest

java.lang.String

Then add the corresponding connection pool settings in the server.xml file of Tomcat:

factory

org.apache.commons.dbcp.basicDataSourceFactory

username scott

Password Tiger

driverclassname

Oracle.jdbc.driver.Oracledriver

URL

JDBC: Oracle: Thin: @newtest

maxactive 20

maxidle 10 maxwait -1

Once you have configured, you can use the ConnectionFactory's getConnection method to get the database connection from the connection pool.

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

New Post(0)