Some time ago, I learned about Hibernate and ibatis, and made a few demos. I have some experience and want to talk with you. Of course, as a beginner, it is inevitable to describe or understand mistakes. I also hope that the readers can correct them. Thank you.
After summing up, I found many comparisons on the Internet. I think there is an article that is similar to my understanding, so I will list it.
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 application EJB to fulfill the task of 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 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. Required during hibernate Runtime
Obtain basic information about the underlying implementation, including the following key attributes:
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.
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 the 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
Write a Dao for each relational table and provide a set of addition, deletion, modification, and query 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 a "semi-automated" ORM implementation. 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 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 Delete () method maps the delete SQL statement;
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 Is An ORM implementation of "SQL mapping.
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 provides more free space for system design with the workload of SQL development and database portability concessions.
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. The system has a huge amount of data processing capacity and extremely demanding performance requirements. 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 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.
6. 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.
7. 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.
8. The most important sentence is what ibatis author says:
If you are starting a new project and you're in full control of your object model and database design, Hibernate is a good choice of O/R tool.
If you are accessing any 3rd party databases (e.g. vendor supplied), or you're working with a legacy database, or even just a really poorly designed database, then an O/R mapper might not be capable of handling the situation. that's were an SQL mapper comes in handy.
9. ibatis 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, 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.
10. 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.
11. Operation Efficiency
Without considering the cache, ibatis should be faster or much faster than hibernate.
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 and fast development speed.
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.
BTW:
Rails activerecord is the best balance, avoiding the complexity of Hibernate and the cost of hql learning. It also has the simplicity of ibatis out-of-the-box learning.