Are you or mapping?

xiaoxiao2021-03-06  44

Are you o / r mapping?

In the software development, O / R mapping is increasingly lifted by people. What is o / r mapping? Simply, Object / Relation Mapping, object to the relationship mapping. What is this name and concept come out? Why do we need o / r mapping in the development? First we look at the development of the object to the object. Object-Oriented development has been widely popular in recent years and has become the most basic method of software development. The development language also has a programming language specifically for object-oriented development, like the mainstream Java, C # is based on programming languages ​​for face-to-face development design. Practice has proved object-oriented, an development method that effectively improves development quality, easy management and conforming to human thinking. Do not manage the development method for process / structures or object-oriented development methods, almost all projects and product software development must involve data access updates such as relational databases. And relational databases are the first need to face all project product development. The current development technology needs to develop a data access layer (Data Access Layer) for each project as the basis for commercial logic code.

In object-oriented development, the most basic database operation of the data access layer includes insertion objects, deleting objects, updating objects, and query objects. Almost all project development needs to first need to implement these atomic operations. These operations are similar in almost all project development, which transforms inserted objects, deleting objects, update objects, and query-level operations to underlying relational data supported by SQL operations such as INSERT, DELETE, UPDATE, and SELECT. . Look at a basic example insert object, here we add a user object Account.

Public void addaccount (Final Account Account) Throws DaoException,

AccountAlReadyexistexception {

Final connection conn;

CONN = getConnection ();

Final prepaaredStatement Stat

= conn.preparestatement ("INSERT INTO"

PersistentAccount.property_account_table

"(" "

PersistentAccount.property_user_name ","

PersistentAccount.property_password ","

PersistentAccount.property_Email ","

Persistentaccount.property_column_writer ")"

"VALUES"

Account.getusername () "','"

Account.getPassword () "" ""

Account.Getemail () "','"

(Account.iscolumnWriter ()? "y": "n") ")");

Stat.execute ();

Stat.close ();

CONN.CLOSE ();

Return;

}

This operation is simple, which is to convert an object logic of an Account object into an Insert action, which implements the simplest ACCOUNT object to the map of database table Account. In the example, it is implemented by developers manually writing JDBC code. If there is another object Person, we also want to write similar code to achieve a Person's operation. Similar examples and code exist in a large number of items. From 1996 JDBC, it is currently 8 years, a large number of project development is still repeated to write such code to implement basic fucks needed for each project. The development of data access layer (development and testing) is great, and the requirements for developers are also very high. The most unfortunate thing is to repeat the development of this complicated data access layer in a large number of project development, resulting in a large number of human labor financial resources to waste in this human intensive data access layer repeated development and database operations are precise. It is the most prone to errors and consume a lot of time in project development. If you need to face different databases in your project, you also need to develop a specialized data access layer for each database. Obviously, each item is repeated but has a code that has the same mode is a significant waste. This gave birth to the production of O / R mapping technology. ORM, Object-RelationL mapping, its role is to make an automatic mapping between the relational database and objects, so that we do not need to go and complicate SQL statements when we do not need to go to the complex SQL statement when specific operational databases. Objects can be, the O / R mapping tool automatically converts the object's operation to the SQL statement operation. This way we only need to pay attention to the object architecture in business logic, rather than the database SQL and JDBC code of the underlying repetitive. Let's take a look at what the original code is different after using O / R mapping technology. Here we use JDO. Public void addaccount (Final Account Account) Throws DaoException,

AccountAlReadyexistexception {

Final PersistenceManager PM = pmf.getPersistenceManager ();

PM.currentTransaction (). Begin ();

PM.makePersistent (Account);

Pm.currentTransaction (). commit ();

}

After contrast, we can understand the huge differences in the amount of code. And this is just a situation in which a single object is inserted. Insert multiple objects, especially inserting multiple associated objects, more advantages of O / R mapping can be seen. In the Liberator JDO frame, Persistence by Reachability is supported, and all objects referenced by the object can be automatically saved to the appropriate data table when saving objects, without having to save each object. After contrast, we can understand the huge differences in the amount of code. And this is just a situation in which a single object is inserted. Insert multiple objects, especially inserting multiple associated objects, more advantages of O / R mapping can be seen. In the Liberator JDO frame, Persistence by Reachability is supported, and all objects referenced by the object can be automatically saved to the appropriate data table when saving objects, without having to save each object. The following example saves a department, all of the student objects contained, and each student profile (Profiel). final PersistenceManager pm = pmf.getPersistenceManager (); final Profile myProfile1 = new Profile (180, "foo1", "fooLast1", 19); final Student student1 = new Student (1, myProfile1); final Profile myProfile2 = new Profile (181 , "foo2", "fooLast2", 19); final Student student2 = new Student (2, myProfile2); final Department department = new Department ( "Computer"); department.addStudent (student1); department.addStudent (student2); Pm.currentTransAction (). Begin (); PM.makePersistent (department); pm.currentTransAction (). Commit (); If you use traditional manual SQL, JDBC mode encoding, code amount can be nearly 5 times. In calculating the test saving time, you can use O / R mapping technology to greatly improve the development efficiency and development time, while the development quality is also easier to ensure. Here we only show a small part of the O / R mapping technology, and more very attractive features worthy of our trial. The benefits of using O / R mapping: 1) Improve learning and development efficiency, greatly reduce development costs. The use of ORMs can greatly reduce learning and development costs, the development of modern technology, so that we need to learn. We must not only learn knowledge-oriented, UML, design models, etc., but also need to learn SQL, JDBC, and even knowledge of various databases (DB2, Oracle, SQL Server, etc.). In actual development, it is really a unique business function for customers. The current status is that we spend a lot of time in writing data access, including BUG lookups, maintenance, etc. will cost more. Time is on data processing. That is to say, we have been wasted at all non-business incidents that do not create value in actual development.

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

New Post(0)