Brief Analysis J2EE Application Database Class Design Mode

zhaozj2021-02-08  209

This article mainly introduces a database-related design pattern. And introduce specific implementations in the J2EE framework, as well as some considerations in transaction processing.

I. Introduction to the design mode

When developing J2EE applications, you are usually to find various information involved in the application, such as a company's product catalog, or a website user information, we will put this information in the database.

In the usual design, we want to analyze the properties and relationships of this data, then perform the logical design of the database, and store various information in different forms. For example, to develop a book information query system. You can create the following two tables to represent books and publishers, respectively.

Table Book (ID, Name, ISBN, AUTHOR, PUBLISHERID, Price, VOLUME)

Table Publisher (ID, Name, Televhone, Address, Postcode)

Table Book contains ID, title, book number, author, publishing agency ID, price, page number. Table Publisher contains ID, commodity name, phone, address, zip code. These two tables are related to the publisher ID.

Let's introduce a pattern of a database-related design.

Database-related classes can be divided into entity class and session classes.

The entity class corresponds to the encapsulation of a table, that is, one example of such a class corresponds to a record in the table. Moreover, the properties in this class and fields in the record are one or one.

The session class corresponds to the operation of all records in a table. For example, add a record, delete a record, find a record, and update a record.

By using this design pattern, make the program more modular and easy to develop and maintain. Of course, other design patterns can also be used.

Second, the program is realized

When implementing the above mode, different techniques are often used in accordance with specific applications. After seeing the above description mode, we can easily find that you can implement (EJB division, entity EJB and session EJB) with EJB.

We know that the purpose of EJB is used to provide a distributed component system development. If our app is a distributed application, there is no doubt that use EJB to achieve great reduction of programming workload. At the same time, by using some of the advanced features of the EJB container, the application can make the application more reliable and scalable. In this way, developers don't have to care about some of the underlying technologies, such as transaction, security, etc., but put the focus on how to implement business logic. But we should pay attention to that if the development of the application is not distributed, it is possible to reduce the performance of the system using EJB. Because the cost of the EJB call is very large.

This article will explore how this mode is implemented above without using EJB technology.

The following is an example of developing book information query systems.

Entity class

As mentioned earlier, each instance of the entity class corresponds to a record in the table. Thus, the attributes of the entity class should correspond to each field of the table. It must be noted that the instance of the entity class is the corresponding corresponding to each record, so the operation of the instance is not immediately reacted in the record of the database in the program.

In this class, it is only a package of data, so this class only requires some basic methods, namely setxx (), and getxx () methods.

Below is an entity class, which is a package for the BOOK table.

Class book {

Protected Int ID;

Protected string name;

Protected string ISBN;

Protected string author;

Protected int publisherid; protected double price;

Protected int volume;

Public void setid (INT IID);

Public int getId ();

Public void setname (String sname);

Public string getname ();

Public void setisbn; SISBN;

Public String Getisbn ();

Public void setauthor (string sauthor);

Public string getAuthor ();

Public void setpublisherid (INT IID);

Public int getPublisherid ();

Public Void SetPrice (double dprice);

Public double getprice ();

Public void setVolume (int tent ivolume);

Public int getVolume ();

Public Book (int IID, String Sname, String Sisbn, INT IPUBLISHERID, DOUBLE DPRICE, IVOLUME);

}

The table Publisher can be packaged in the same manner.

2. Session class

The session class is mainly processed for a table. These operations can be created in the table, delete a record, update a record and find a record. The result of these operations corresponds to the instances of records in the tables and the physical classes in the memory, or corresponding to the records in the table.

We can take a look at the package of the BOOK table.

Class boxtable {

Void Add (Book Book);

Void Delete (Book Book;

Void Update (Book Book);

Collection FindbyId (INT IID);

Collection FindByxxxx (XX, XX);

Collection FindBypulishername (String Spublishername);

}

ADD () is used to map a BOOK instance in memory to the database. DELETE () is used to delete a record in the database. Update () is used to update one of the records in the table. FindbyXXXX () corresponds to the SELECT statement.

There are two ways to be in multiple tables. One is like BookTable, specializes in encapsulating a class. Another method is to write a FindByPublisherName () directly in BoolTable. This method is designed to return a collection of BOOK.

The above is just a simple introduction how to implement physical classes and session classes. In a specific application, considering the consistency of database operations. Let's introduce the relevant content of the transaction.

Third, transaction processing

In order to ensure the integrity and consistency of data operations, you should fully take into account the problem of transaction processing in programming.

1. How many SQL statements are combined into a transaction in JDBC.

In JDBC, when you open a connection object Connection, the default is Auto-Commit mode, each SQL statement is used as a transaction, that is, each time you execute a statement, you will automatically get a transaction confirmation. In order to combine multiple SQL statements into one transaction, you should block the auto-commit mode.

After the auto-commit mode is broken, if a commit () method is not called, the SQL statement does not get a transaction confirmation. All SQLs after the last commit () method call will be confirmed when the method commit () is called.

The following code is a demonstration:

Con.SetAutocommit (False);

PREPAREDSTATEMENT UPDATESALES = Con. PrepareStatement ("Update Coffes Set Sales =? Where COF_NAME LIKE?"); UpdateSaS.setint (1, 50);

UpdatesaS.setstring (2, "colorbian");

Updates.executeUpdate ();

PREPAREDSTATEMENT UpdateTotal = Con. PrepareStatement ("Update Coffee Set Total = Total ? WHERE COD_NAME LIKE?");

UpdateTotal.setint (1, 50);

UpdateTotal.SetString (2, "colorbian");

UpdateTotal.executeUpdate ();

C.Commit (0;

Con.SetAutocommit (TRUE);

2.J2ee distributed transaction processing

In J2EE, JTA can be used to call the underlying JTS (Java Transaction Service Provider Services) to handle distributed transaction processing. In addition, if eJB can be used, it can be implemented by specifying the property of the transaction in the description file.

For details on distributed transaction processing, see the J2EE specification.

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

New Post(0)