What is Orm? Why use Orm?

Source: Internet
Author: User
To understand Orm, first understand the following concepts: What is "persistence"

Persistence: saves data (such as objects in memory) to a permanently retained storage device (such as a disk ). Persistence is mainly used to store data in the memory in a relational database. Of course, it can also be stored in disk files or XML data files. What is
"Persistent layer"

Persistence Layer (persistence layer) is a logical layer that focuses on a specific system in the field of data persistence application. It associates data users with data entities. What is Orm? Object-relationl mapping is used to map between a relational database and an object. In this way, when we operate a database, you don't need to deal with complex SQL statements, as long as you operate on objects as usual. Why persistence and ORM design (important)

In the current enterprise application system design, MVC (model)-View-control) is the main system architecture mode. In MVC
Model includes complex business logic and data logic, as well as data access mechanisms (such
JDBC connection, SQL generation, statement creation, and reading of the resultset result set. Separates these complex business logic from data logic to tightly couple the system
Converting a link to a loosely coupled Link (I .e., decoupling) is an urgent task to reduce the Coupling Degree of the system and also a task of persistence.The MVC mode implements the decoupling between the presentation layer (View) and the data processing layer (model) in the architecture, the persistence design implements the decoupling of business logic and data logic separation in the Data Processing Layer.
As the most important and complex technology in persistence design, Orm is also a hot technology in the industry.

To put it simply, the business processing logic and data access logic are mixed in the general system design and JDBC operations on the database.
Generally, the steps are as follows:
1. Create a database connection to obtain the connection object.
2. Assemble and query SQL statements based on user input.
3. Create a statement object or preparedstatement object based on the SQL statement.
4. Run the SQL statement with the connection object to obtain the result set resultset object.
5. Read the data in the result set resultset object one by one.
6. Calculate the data according to the specific business logic.
7. Assemble and update SQL statements based on the calculated results.
8. Use the connection object to execute the update SQL statement to update data in the database.
7. Close each statement object and connection object in sequence.

it can be seen that the Code logic is very complex, which does not include the processing logic of a statement execution failure. The business processing logic and data access logic are completely mixed. A complete system should include thousands of such repetitive and mixed processing processes, if you want to modify some of the business logic or related business processes, the amount of code to be modified will be unimaginable. On the other hand, if you want to change the database
the product or running environment may also be an impossible task. However, the user's operating environment and requirements vary widely. We cannot design the same system for every user's running environment.
the same processing code, namely, business logic, and data access logic, must be separated. On the other hand, the data in relational databases is basically accessed by a row of data, while the Program
runs but processes objects one by one, currently, most database-driven technologies (such as ADO. net, JDBC, ODBC, and so on) are all processed in a row set result set. To solve the
difficulty, the orm object and data ing technology emerged.

for example, if you want to complete a program for shopping discounts and promotions, you can use the ORM idea to implement the following (derived from Hibernate):
the business logic is as follows:
Public double calcamount (string customerid, double amount)
{< br> // obtain the customer record based on the customer ID
customer = customermanager. getcustomer (custmerid);
// get discount Rules Based on Customer Grade
promotion Promotion = promotionmanager. getpromotion (customer. getlevel ();
// accumulate the total customer consumption and save the cumulative result
customer. setsumamount (customer. getsumamount (). add (Mount);
customermanager. save (customer);
// return the discounted amount
return amount. multiply (protomtion. getratio ();
}< br> the
sample code is very clear and completely isolated from the data access logic. When designing the business logic code, you do not need to consider the same operations of database JDBC, but hand it over to the
customermanager and promotionmanager classes. This is a simple ORM design. The actual ORM
Implementation Framework is much more complicated than this.

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.