Use object-relational ing (ORM) system middleware to improve software development efficiency and quality)

Source: Internet
Author: User

1. What is object-relational ing (ORM )?

Object-link ing
Object/Relation Mapping (ORM) is developed with the development of object-oriented software development methods. The object-oriented development method is enterprise-level application development
Mainstream development methods in the environment. Relational databases are the mainstream data storage systems that permanently store data in enterprise application environments. Objects and relational data are two forms of business entities.
Object, expressed as relational data in the database. Objects in the memory have associations and inheritance relationships. In the database, relational data cannot directly express many-to-many associations and inheritance relationships. Therefore, object-link ing
(ORM) systems generally exist in the form of middleware, mainly to map program objects to relational database data.

2. Why should we introduce the object-link ing middleware?
When developing a relational database system, you can use SQL statements to read and operate relational database data. In the Java field, you can directly access the database through JDBC programming. JDBC can be said to be
The most primitive and direct method for Java to access relational databases. The advantage of this method is high running efficiency. The disadvantage is that a large number of SQL statements are embedded in Java program code. Redundancy is inevitable.
Developers often find that they write the same common code over and over again, such as getting connections, preparing statements, loop result sets, and other JDBC-specific elements, making the project difficult to maintain. Especially when
To a large number of relational data tables, it is more difficult to use JDBC development in programs when they need to be used in multiple different types of relational database systems.
In the development of data-based applications
The introduction of Object-relationship ing middleware in a given software system is a realistic need to improve development efficiency and software product maintenance and scalability. Practice shows that enterprise-level application development based on data processing
By introducing the object-relationship ing middleware, you can save about 35% of the programming workload related to object persistence, improve software product maintenance and scalability, and improve software product quality.
Therefore, when developing enterprise-level applications, it is necessary to introduce the object-relational ing System middleware to achieve rapid database development. Enterprises can develop separate persistence layers through JDBC programming, encapsulate database access operations, and provide concise APIs for unified calling at the business layer to implement their own ORM system middleware.
Of course, a mature object-link ing middleware product not only simply persists the objects in the memory to the database, but also loads the relational data in the database into the memory, and ensures frequent system access.
The performance of the database reduces the frequency of accessing the database. Many details such as multithreading, caching, and transaction management need to be introduced. The technology involved is complicated. Therefore, we use the excellent ORM systems on the market.
Device Products.

Iii. What are the mainstream object-link ing middleware Products in Java?
Object Relational mappers (ORM) has multiple forms. In the Java field, most popular ORM can implement full domain model ing. Its goal is to map the entire layer of objects and behaviors to database tables. Mainstream ORM middleware Products include:
Hibernate (recommended)
JDO
Ibatis
EJB entities 3
EJB entity beans 2.x
Toplink
Among the many ORM middleware Products, Hibernate is the key recommendation by the author. Hibernate is a Java-based open-source persistent middleware.
The lightweight encapsulation not only provides the ORM ing service, but also provides data query and data cache functions. Java developers can easily manipulate the database through the hibernate API. Now
More and more java developers use hibernate as the middleware between enterprise applications and relational databases.

4. Use easydbo to implement simple object-link ing
Easydbo is a simple Java framework Network (www.easyjf.com)
Developed a Java data persistence layer framework suitable for the development of small and medium-sized software databases. The system references hibernate, JDO, and so on, and implements a simple Java
Object-to-link data ing. Easydbo is relatively simple and suitable for development and use by small and medium-sized enterprises and individuals. easydbo is currently in the testing stage and is a simple but immature object.
-Link ing open-source Middleware.
Easydbo's source code is very simple and can be understood by anyone with a little java knowledge. Therefore, easydbo is chosen as an example in this article to help readers quickly enter the world of ORM systems, understand object-the essence and implementation principle of relational database.
Easydbo considers the simplest object-link ing. It is simple to realize object-link ing without using any configuration file. Easydbo source code mainly includes
Com. easyjf. DBO, Com. easyjf. DBO. config, Com. easyjf. DBO. SQL. Where
Com. easyjf. DBO is the core of the framework. It implements object-link conversion, jdbc api encapsulation, and user interface support,
Com. easyjf. DBO. config manages configuration files. com. easyjf. DBO. SQL generates simple SQL statements for databases and supports multiple databases.
.
In the current beta version, programmers use easydbo to operate databases. They only need to focus on the methods provided by the easyjdo class, the dbobject class of the original data object, and the iobject interface.

V. Example of developing database applications using easydbo
Next, let's take a look at how easydbo is used for database development.
Assume that the structure of a message table (Message) is as follows:
CID: varchar 16 primary key
Title: varchar 50
Content: Text
Inputuser: varchar 16
Inputtime: datetime
Status: int

The class message in the corresponding Java is defined
Import java. util. date;
Import java. util. List;
Import com. easyjf. DBO. easyjdb;
Import com. easyjf. DBO. iobject;
// Implement the com. easyjf. DBO. iobject interface so that your object can be mapped to a relational data table without a configuration file.
Public class message implements iobject {
Private string CID;
Private String title;
Private string content;
Private string inputuser;
Private date inputtime;
Private integer status;
Public String gettablename (){
Return "message ";
}
Public String getkeyfield (){

Return "CID ";
}
Public String getkeygenerator (){
Return "com. easyjf. DBO. idgenerator ";
}
Public String getcid (){
Return CID;
}
Public void setcid (string CID ){
This. cid = CID;
}
Public String getcontent (){
Return content;
}
Public void setcontent (string content ){
This. content = content;
}
Public java. util. Date getinputtime (){
Return inputtime;
}
Public void setinputtime (Java. util. Date inputtime ){
This. inputtime = inputtime;
}
Public String getinputuser (){
Return inputuser;
}
Public void setinputuser (string inputuser ){
This. inputuser = inputuser;
}
Public integer getstatus (){
Return status;
}
Public void setstatus (integer status ){
This. Status = status;
}
Public String gettitle (){
Return title;
}
Public void settitle (String title ){
This. Title = title;
}

// Persists (SAVE) the object to the relational database.
Public Boolean save ()
{
Easyjdb DB = new easyjdb ();
Return dB. saveorupdate (this );
}
// Permanently delete objects from the Database System of persistent storage devices
Public Boolean del ()
{
Easyjdb DB = easyjdb. getinstance ();
Return dB. Del (this );
}
// Read data from the database system using the primary key ID and return an object
Public static message read (string CID)
{
Easyjdb DB = easyjdb. getinstance ();
Return (Message) dB. Get (message. Class, CID );
}
// Query the qualified data from the database through SQL and return the Object List
Public static list query (string SQL)
{
Easyjdb DB = easyjdb. getinstance ();
Return dB. Query (message. Class, SQL );
}
// Application demo code
Public static void main (string [] ARGs ){
Message M = new message ();
M. settitle ("title ");
M. setcontent ("content ");
M. setinputtime (new date ());
M. setinputuser ("test ");
M. setstatus (New INTEGER (1 ));
If (M. Save ())
{
System. Out. Print ("the object is successfully saved to the relational database ");
}
Else
{
System. Out. println ("An error occurred while saving the data! ");
}
// Query data
List list = message. Query ("1 = 1 ");
If (list! = NULL ){
For (INT I = 0; I <list. Size (); I ++)
{
Message message = (Message) list. Get (I );
System. Out. println ("--------");
System. Out. println ("CID:" + message. getcid ());
System. Out. println ("title:" + message. gettitle ());
System. Out. println ("inputuser:" + message. getinputuser ());
System. Out. println ("inputtime:" + message. getinputtime ());
System. Out. println ("status:" + message. getstatus ());
}
}
}
}

As shown in the code above, the program only needs to get an instance through the getinstance () method of easyjdb, and then you can use the save,
Update, Del, get, query and other similar methods for database operations. Programmers do not have to worry about the specific database system type, the access to database connections, and the access to database resources.
Release the SQL statements, without the need to construct tedious and tasteless SQL statements, such as addition, deletion, modification, and query.
About the data source and configuration file: You can configure the database to connect to the data source through the configuration file easyjf-dbo.xml file of easydbo, you can also set the data source in the program, you can also set the data source through the IOC container. Next let's take a look at the content of the easydbo data source profile easyjf-dbo.xml.
<Easyjf-DBO>
<Datasource id = "BBS" type = "org. Apache. commons. DBCP. basicdatasource">
<Property name = "easydbo. Connection. driver_class"> org. gjt. Mm. MySQL. Driver </property>
<Property name = "easydbo. Connection. Password"> yourpassword </property>
<Property name = "easydbo. Connection. url"> JDBC: mysql: // 127.0.0.1: 3306/easyjf </property>
<Property name = "easydbo. Connection. username"> root </property>
<Property name = "easydbo. dialect"> com. easyjf. DBO. SQL. mysqlquery </property>
<Property name = "easydbo. Optimize"> true </property>
<Property name = "esyydbo. show_ SQL"> true </property>
</Datasource>
<Tables>
<Class>
</Class>
</Tables>
</Easyjf-DBO>

If you are using ms SQL Server data, modify the configuration file above to the following:
<Easyjf-DBO>
<Datasource id = "BBS" type = "org. Apache. commons. DBCP. basicdatasource">
<Property name = "easydbo. Connection. driver_class"> com. Microsoft. JDBC. sqlserver. sqlserverdriver </property>
<Property name = "easydbo. Connection. Password"> SA </property>
<Property name = "easydbo. Connection. url"> JDBC: Microsoft: sqlserver: // localhost: 1433; databasename = easyjf; selectmethod = cursor </property>
<Property name = "easydbo. Connection. username"> yourpassword </property>
<Property name = "easydbo. dialect"> com. easyjf. DBO. SQL. mssqlserverquery </property>
</Datasource>
<Tables>
<Class>
</Class>
</Tables>
</Easyjf-DBO>

6. Select the target object-link ing Middleware
Not the most comprehensive function, the most used middleware will certainly be suitable for you. Among the various popular ORM middleware Products, Hibernate is undoubtedly the most outstanding product, from the system architecture, performance and merit
Excellent performance. In the application field of cluster servers in large distributed cities, EJB entity beans is the first choice because of its extensive market and application base. Of course, in small database applications
Fields, such as a news website, a simple online store, and a forum system, I also recommend you try easydbo without defense. As a beginner, I recommend you read easydbo.
Easydbo source code allows you to quickly master and understand the essence and working principle of ORM middleware, thus laying the foundation for learning and using more powerful object-relational ing middleware Products.
For more complete easydbo application instance code, download it from the official website www.easyjf.com.

Source: http://blog.csdn.net/chensheng913/archive/2005/12/26/562700.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.