What is the difference between Hibernate and ibatis?

Source: Internet
Author: User

Hibernate

Hibernate is an open-source object relationship ing framework that encapsulates JDBC objects in a lightweight manner, so that Java programmers can use the object programming thinking to manipulate the database as they wish. Hibernate can be used in any situation where JDBC is used. It can be used in Java client programs or Servlet/JSP web applications. The most revolutionary is that, hibernate can replace CMP in the J2EE architecture of the application EJB [CMP (container-managed persistence) indicates that the EJB container manages the persistence of the entity EJB, the EJB container encapsulates object-link ing and data access details to complete data persistence.

I. Basic functions
As a data persistence middleware, Hibernate is sufficient for databases to hibernate at the business logic layer development.It implements ing between classes and data tables through Extensible Markup Language (XML), enabling programmers to change database-oriented development to object-oriented development in business logic development.. This makes the entire project development division clearer and improves the efficiency of program development.
1. Configuration object:
The configuration class manages the configuration information of hibernate. Some basic information about the underlying implementation needs to be obtained during hibernate runtime. Several key attributes include:
1) Database URL
2) database users
3) Database User Password
4) database JDBC driver
5) database dialect is used to provide support for specific databases, including implementation of specific database features, such as ing hibernate data types to specific database data types. The above information is generally configured in the hibernate. cfg. xml or hibernate. properties file to connect to different databases.
2. Session object:
Session is the basis of persistent layer operations, which is equivalent to connection in JDBC:
The instance is built through the sessionfactory instance:
Configuration Config = new configuration (). Configure ();
Sessionfactory = config. buildsessionfactory ();
Session session = sessionfactory. opensession ();
Then we can call the save, find, flush and other methods provided by the session to complete the persistent layer operations. Therefore, the session object encapsulates all database operations to implement hibernate's database operations, such:
Add and save the SAVE () method;
Delete () method to delete data;
Update () method to update and modify data;
Use the find () method to retrieve data ;......
Hibernate will automatically generate corresponding SQL statements based on different operations, so that the programmer's operations on the Po object are converted to operations on the database relational table..
Ii. Procedure
1. Compile the hibernate configuration file
There are two types of hibernate configuration files: hibernate. cfg. xml and hibernate. properties. We recommend that you use hibernate. cfg. xml.
2. Po and ing files
Use middlegen and hibernate-extensions to export the Po ing file from the database and declare it in hibernate. cfg. xml.
3. Write Dao to write a Dao for each relational table, and provide a set of adding, deleting, modifying, and querying methods for the business logic to operate on the database.

Ibatis
Compared with "one-stop" ORM solutions such as Hibernate and Apache OJB, ibatis is"Semi-automatic. The so-called "semi-automated" may be a little cool to understand. Throughout the mainstream Orm, both Hibernate and Apache OJB provide a complete encapsulation of the database structure and a full set of ing mechanisms from pojo to database tables. Programmers often only need to define the poing between pojo and database tables, and can complete the persistent layer operations through the methods provided by hibernate or OJB. Programmers do not even need
Familiar with, Hibernate/OJB will automatically generate corresponding SQL based on the developed storage logic and call the JDBC interface for execution.
The most direct advantage of ibatis is that it not only provides a ing between objects and relational databases for programmers, but also provides a direct ing between operation methods and SQL, the designer can directly specify an SQL statement for a method to obtain more accurate data. It also facilitates the optimization of queries and connection queries..
I. Basic functions
As another lightweight ORM middleware, ibatis provides connection management, cache support, thread support, and (distributed) in addition to adding, deleting, modifying, and querying databases) things management is a set of complete database management functions.
The sqlmapclient object is the basis for ibatis persistent layer operations. It is equivalent to the session in Hibernate and provides the SQL ing method.
The insert () method maps the inserted SQL statements;
The delele () method maps the deletion of SQL statements;
Update () method to update SQL statements;
Queryforlist (), queryformap (), queryforobject (), queryforpaginatedlist (), and other methods provide an example of a set of query SQL statements;
Ii. Procedure
1. ibatis SQL map configuration file
The file configures the connection of the database used, including the database driver type, user name, password, and management data of the connection pool.
2. Po and ing files
Like hibernate, Po is used as a reflection of database relationship tables. It also requires a ing configuration file to be responded. It can be handwritten or generated using hibernate tools, it does not affect the use of Po in ibatis. Different from hibernate, The ibatis ing file does not provide a response description for each attribute in the Po, but specifies a series of SQL-related operations related to the PO, it also reflects the good flexibility and scalability of ibatis.
3. Write Dao
In Dao, you can use the method provided by sqlmapclient to specify the SQL statement for Po operations, so that the development of the business logic layer is still object-oriented.
Compared with hibernate "O/R,Ibatis Is An ORM Implementation of "SQL mapping ".Now.
Hibernate provides a complete encapsulation of the database structure. hibernate's O/R Mapping implements the poing between pojo and database tables, as well as automatic SQL generation and execution. Programmers often only need to define the poing relationship between pojo and database tables to complete the persistent layer operations through the methods provided by hibernate. Programmers do not even need to be familiar with SQL. hibernate/OJB will automatically generate the corresponding SQL based on the developed storage logic and call the JDBC interface for execution.
Ibatis focuses on the poing between pojo and SQL. That is to say, ibatis does not automatically generate SQL Execution for programmers at runtime.

The specific SQL needs to be compiled by the programmer, and then map the parameters required by the SQL and the returned result fields to the specified pojo through the ing configuration file. Using the ORM mechanism provided by ibatis, for business logic implementers, they are faced with pure Java objects. This layer is basically consistent with the implementation of ORM through hibernate, while for specific data operations, hibernate automatically generates SQL statements, while ibatis requires developers to write specific SQL statements. Compared with hibernate, ibatis
With the workload of SQL development and database portability concessions, it provides more free space for system design.
.
Comparison between the two:
1. ibatis is very easy to learn, Hibernate is relatively complicated, and the threshold is high.
2. Both are excellent open-source products
3. When the system is under secondary development and cannot control or modify the database structure, ibatis is more flexible than hibernate.
4.System data processing capacity is huge and performance requirements are extremely demanding. This often means that we must use highly optimized SQL statements (or stored procedures) to achieve system performance design indicators. In this case, ibatis has better controllability and performance.
5.Ibatis requires a handwritten SQL statement or a part of it. hibernate can basically generate it automatically and occasionally write some hql statements.. For the same requirement, the workload of ibatis is much larger than that of hibernate. Similarly, if modification to database fields is involved, there are very few changes to hibernate, while ibatis should modify those SQL mapping areas one by one.

6.Ibatis supports fine-grained optimization.. For example, if I have a table with several or dozens of fields, I need to update one of the fields. ibatis is very simple, execute an SQL update table_a set column_1 = # column_1 # Where id = # ID # However, It is troublesome to use hibernate. By default, Hibernate updates all fields. Of course, I remember that hibernate has an option to control only the modified fields, but I am not sure about the negative effects of this function. For example, to list part of a table, use
In ibatis, the advantage is that you can read a lot of data from the database and save the select ID, name from table_with_a_lot_of_column where... generally, Hibernate Selects all fields. For example, there is a table with eight fields, two of which are relatively large, varchar (255)/text. In the above scenario, why should I select them? If Hibernate is used, you cannot set the two unnecessary fields as lazy load, because there are still many places to set the entire domain object
Load it. At this time, we can see the benefits of ibatis. If I want to update a record (an object), if Hibernate is used, select the object and update it again. This is two SQL statements for the database. Ibatis only needs an update SQL statement. Reducing one interaction with the database is very important for performance improvement.

7. Development: in terms of development efficiency, I think the two should be similar.In terms of maintainability, I think ibatis is better.. Because ibatis SQL is saved to a separate file. In some cases, Hibernate may protect SQL/hql in Java code. Compared with hibernate "O/R", ibatis is Is An ORM implementation of "SQL mapping. Ibatis focuses on the poing between pojo and SQL. That is to say, ibatis does not automatically generate SQL Execution for programmers at runtime. Specific SQL
You need to write the program, and then map the parameters required by the SQL statement and the returned result fields to the specified pojo through the ing configuration file. Using the ORM mechanism provided by ibatis, for business logic implementers, they are faced with pure Java objects. This layer is basically consistent with the implementation of ORM through hibernate, while for specific data operations, hibernate automatically generates SQL statements, while ibatis requires developers to write specific SQL statements. Compared with hibernate, ibatis provides more free space for system design based on the workload of SQL development and database portability concessions.
8. Hibernate is now a mainstream o/R Mapping Framework. From the richness of documents, product perfection, and version development speed, it is better than ibatis.

Choose hibernate or ibatis:
Hibernate has powerful functions, good database independence, and strong o/R ing capabilities. If you are proficient in Hibernate and have encapsulated hibernate properly, the code for the entire persistence layer of your project will be quite simple, with few code to be written, fast development, and great.
The disadvantage of Hibernate is that it has a low learning threshold and a higher level of proficiency. In addition, how can we design o/R ing and balance the performance and object models, and how to make good use of hibernate requires your experience and capabilities.
Ibatis is easy to learn and use. It provides the Automatic Object binding function for database queries and extends the good experience in SQL. For projects that do not have such high requirements on object models, perfect.
The disadvantage of ibatis is that the framework is still relatively simple and the functions are still missing. Although the data binding code is simplified, the entire underlying database query needs to be written by itself, and the workload is also large, it is not easy to adapt to quick database modification.
Suggestion:
If your team does not have a master of hibernate, use ibatis to make good use of hibernate, it is not easy; otherwise you should choose hibernate, so that your development speed and code simplicity are quite good!

References:

Http://hi.baidu.com/274084093/blog/item/5d15a56eb16276ce80cb4a1e.html

Http://zhidao.baidu.com/question/99674664.html? Fr = qrl & cid = 870 & Index = 4 & FR2 = Query

Http://developer.51cto.com/art/200907/138286.htm

Http://javapub.iteye.com/blog/751485

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.