Differences between ibatis and hibernate

Source: Internet
Author: User

Hibernate introduction 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 application EJB to fulfill the task of data persistence. I. Basic functions Hibernate, as a data persistence middleware, is sufficient for databases to Hibernate during business logic layer development. It implements the ing between classes and data tables through the Extensible Markup Language (XML), so that programmers can change to object-oriented development in the development of business logic. This makes the entire project development division clearer and improves the efficiency of program development. 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 URL2. database user 3. database User Password 4. database JDBC driver Category 5. database dialect is used to provide support for a specific database, including the 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. Session Object: Session is the basis for persistent layer operations, which is equivalent to Connection in JDBC: The instance is built through the SessionFactory instance: Configuration config = new Configuration (). configure (); SessionFactory 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 operations on the database to implement Hibernate's database manipulation functions, such as adding and saving the Save () method, and deleting the data using the Delete () method; update () is used to Update and modify data. Find () is used to retrieve data. Hibernate automatically generates corresponding SQL statements based on different operations, this allows programmers to convert operations on PO objects to operations on Database Relational Tables. 2. Use Step 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. The PO and ing files use middlegen and hibernate-extensions to export the PO ing files from the database and declare them 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. For more details, refer to the hibernate website for detailed information. In addition, we will learn more from each other's practices and development. Ibatis is a "semi-automated" ORM implementation compared with "one-stop" ORM solutions such as Hibernate and Apache OJB. 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 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. 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 and facilitate query optimization and connection. I. Basic functions as a lightweight ORM middleware, ibatis provides connection management, cache support, and thread support in addition to adding, deleting, modifying, and querying databases, A set of (distributed) transaction management functions provides 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 delete () method maps the deleted SQL statements. The update () method reflects the updated SQL statements. queryForList () queryForMap (), queryForObject (), queryForPaginatedList (), and other methods provide a set of query SQL statements. 2. Use step 1. the ibatis SQL Map configuration file configures the connection of the database used, including the database driver type, user name, password, and management data of the connection pool. 2. the PO and hibernate ing files are the same as those of hibernate. PO is used as the shadow of the database relationship table and also the ing configuration file to be responded. You can write the PO manually or generate the PO 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. Choose either Hibernate or iBATIS. The features of Hibernate: Powerful Hibernate, good database independence, and strong O/R ing. If you are proficient in Hibernate, in addition, if Hibernate is properly encapsulated, the entire persistent layer code of your project will be quite simple, and few code needs to be written. The development speed will be fast and refreshing. The PO and Hibernte ing obtained by one-to-one ing of database fields are completely different. The essential difference is that the PO is flat, unlike the PO mapped by Hibernate, it can express the relationship between three-dimensional object inheritance, aggregation, and so on, which will directly affect the design idea of your entire software system. 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. 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 strong experience and capabilities, but Hibernate is now a mainstream O/R Mapping Framework, from the richness of documents, product perfection, both versions are faster than iBATIS. IBATIS features: iBATIS is easy to get started with and is ready for use. It provides the Automatic Object binding function for database queries and provides good experience in SQL, for projects that do not have such high requirements on object models, it is 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. When the system is under secondary development and cannot control or modify the database structure, iBATIS is more flexible than Hibernate. 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. Comparison of actual development: 1. iBATIS requires a handwritten SQL statement or a part of it. Hibernate can basically generate it automatically and occasionally write Hql. 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. 2. iBatis can be fine-grained. For example, 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, I need to list part of a table. When using iBatis, the advantage here is that I can read a lot of data FROM the database and save the flow of 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 the entire domain object needs to be loaded at one time. 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. 3. 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. 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 provides more free space for system design with the workload of SQL development and database portability concessions. 4. When the running efficiency is not considered as cache, iBatis should be faster or more efficient than hibernate.

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.