In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you the "what is the difference between choosing Ibatis and Hibernate", the content is simple and clear, and I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "what is the difference between choosing Ibatis and Hibernate".
Introduction to hibernate
Hibernate is an open source object-relational mapping framework, which encapsulates jdbc with very lightweight objects, so that java programmers can use object programming thinking to manipulate the database at will. Hibernate can be used in any situation where jdbc is used, not only in java client programs, but also in servlet/jsp web applications. * Revolutionary significance is that hibernate can replace cmp in the j2ee architecture of ejb to complete the important task of data persistence.
I. basic functions
As the middleware of data persistence, hibernate is enough to make the database hibernating in the development of business logic layer. It realizes the mapping between classes and data tables through Extensible markup language (xml), which makes programmers change from database-oriented to object-oriented in the development of business logic. It makes the division of labor of the whole project development more clear, and improves the efficiency of program development.
Configuration object:
The configuration class is responsible for managing the configuration information for hibernate. Required by the hibernate runtime
Get some basic information about the underlying implementation, several of which are key attributes:
Database url
Database user
Database user password
Database jdbc driver class
Database dialect, which is used to provide support for specific databases, including the implementation of specific database characteristics, such as the mapping of hibernate data types to specific database data types.
The above information is generally configured by hibernate.cfg.xml or hibernate.properties files to connect with different databases.
Session object:
Session is the basis for persistence layer operations, which is equivalent to connection in jdbc:
The instance is built through 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 session to complete the persistence layer operation. Therefore, the session object also encapsulates all the operations on the database to achieve the database manipulation functions of hibernate, such as:
The save () method implements adding and saving
Delete () method to delete data
Update () method to update and modify data
Find () method to realize data retrieval
Hibernate will automatically generate corresponding sql statements according to different operations, thus realizing the transformation of programmers' operations from po objects to database relational tables.
Second, use steps
1. Write hibernate configuration file
There are two types of hibernate configuration files, which are hibernate.cfg.xml files and hibernate.properties, and hibernate.cfg.xml is recommended.
2.po and Mapping Fil
Use middlegen and hibernate-extensions to export the mapping file for po 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 add, delete, change and query methods for business logic to operate on the database.
For more details, please refer to hibernate's website for more information. And deepen the experience in their respective practice and development.
Introduction to ibatis
Compared to "one-stop" orm solutions such as hibernate and apache ojb, ibatis is a "semi-automated" orm implementation. The so-called "semi-automatic" may be a little astringent in understanding. Throughout the current mainstream orm, both hibernate and apache ojb provide a relatively complete package of database structure, and provide a full set of mapping mechanism from pojo to database tables. Programmers often only need to define the mapping relationship between pojo and database tables, and can complete the persistence layer operation through the methods provided by hibernate or ojb. Programmers do not even need to be proficient in sql, hibernate/ojb will automatically generate the corresponding sql according to the storage logic and call the jdbc interface to execute it.
The most direct advantage of ibatis is that it not only provides programmers with mapping between objects and relational databases, but also provides direct mapping between operation methods and sql. Designers can directly specify a sql statement for a method to obtain more accurate data, and at the same time provides convenience for optimizing and connecting queries.
I. basic functions
As another lightweight orm middleware, ibatis not only provides basic database addition, deletion, modification and lookup, but also provides connection management, cache support, thread support, (distributed) transaction management and other complete database management functions.
The sqlmapclient object is the basis for the operation of the ibatis persistence layer, which is equivalent to session in hibernate and provides a method for mapping to sql.
The insert () method implements the mapping of inserting sql statements
The delete () method implements the mapping of deleting sql statements
The update () method implements the mapping of updated sql statements.
Queryforlist (), queryformap (), queryforobject (), queryforpaginatedlist () and other methods provide a set of mapping of query sql statements.
Second, use steps
1.ibatis sql map profile
The basic configuration of the connection to the database is made in the file, including the database driver type, user name, password, and the relevant management data of the connection pool.
2.po and Mapping Fil
Like hibernate, po, as a mapping of database relational tables, also needs a response mapping configuration file, which can be handwritten or generate po with the help of related tools of hibernate, which will not affect the use of po in ibatis. Different from hibernate, the mapping file of ibatis does not describe the response of each attribute in po, but specifies a series of sql-related operations related to po, which also reflects the good flexibility and expansibility of ibatis.
3. Write dao
In dao, you can use the methods provided by sqlmapclient to specify sql statements for po operations, so that the development of the business logic layer is still an object-oriented operation.
There is a reason to choose hibernate or ibatis:
Characteristic comparison
Characteristics of hibernate:
Hibernate is powerful, database-independent, and the mapping ability of hibernate r is strong. If you are quite proficient in hibernate and encapsulate hibernate properly, then the whole persistence layer code of your project will be quite simple, with very little code to write, and the development speed is very fast and very cool. The po obtained by the object mapping of po and hibernte obtained by the one-to-one mapping of database fields is completely different, the essential difference is that this kind of po is flattened, unlike the po of hibernate mapping, which can express the relationship between three-dimensional object inheritance, aggregation and so on, which will directly affect the design idea of your whole software system. Hibernate provides a relatively complete encapsulation of the database structure, and hibernate's hibernate mapping realizes the mapping between pojo and database tables, as well as the automatic generation and execution of sql. Programmers often only need to define the mapping relationship between pojo and database tables, and can complete the persistence layer operation through the methods provided by hibernate. Programmers do not even need to be proficient in sql, hibernate/ojb will automatically generate the corresponding sql according to the storage logic and call the jdbc interface to execute it. The disadvantage of hibernate is that the threshold for learning is not low, and the threshold to be proficient is higher, and how to design the ibatis r mapping, how to strike a balance between performance and the object model, and how to make good use of hibernate requires your strong experience and ability, but hibernate is now the mainstream oibatis framework, from the richness of the document, product perfection, version development speed should be stronger than ibatis.
Characteristics of ibatis:
Ibatis is easy to get started, provides automatic object binding for database queries, and continues a good experience in using sql, which is quite * * for projects that do not have such high object model requirements. The disadvantage of ibatis is that the framework is still relatively simple, the function is still missing, although it simplifies the data binding code, but the entire underlying database query actually has to be written by itself, the workload is also relatively large, and it is not easy to adapt to rapid database modification. When the system belongs to secondary development and can not control and modify the database structure, then the flexibility of ibatis will be more suitable than hibernate. The system has a huge amount of data processing and extremely stringent performance requirements, which often means that we must use highly optimized sql statements (or stored procedures) to achieve the system performance design indicators. In this case, ibatis will have better controllability and performance.
A comparison of actual development:
1. Ibatis requires handwritten sql statements, or you can generate some of them, while hibernate can basically be generated automatically, and occasionally write some hql. For the same requirement, the workload of ibatis is much larger than that of hibernate. Similarly, when it comes to database field changes, hibernate changes very little, while ibatis modifies those sql mapping areas one by one.
2. Ibatis can optimize fine granularity.
For example, I have a table that has several or dozens of fields. I need to update one of the fields. Ibatis is very simple. It is troublesome to execute a sql update table_a set column_1=#column_1# where id=#id# but use hibernate. By default, hibernate updates all the fields. Of course I remember that hibernate has an option to save only modified fields, but I'm not sure about the negative effects of this feature.
For example: I need to list some of the contents of a table, when using ibatis, the advantage is that it can read a lot of data from the database, saving traffic select id, name from table_with_a_lot_of_column where. Normally, hibernate will select all the fields. For example, there is a table above that has eight fields, including one or two larger fields, varchar / text. Why did I pick them out in the above scene? With hibernate, you can't set these two unwanted fields to lazy load, because there are a lot of places where you need to load the entire domain object at once. At this time, the benefits of ibatis can be shown. If I need to update a record (an object), if I use hibernate, I need to select the object now and then update it. These are two sql for the database. Ibatis only needs a sql of update. 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 the sql of ibatis is saved in a separate file. In some cases, hibernate may protect sql/hql in java code. Ibatis is an orm implementation of "sql mapping" as opposed to hibernate "obind r". The focus of ibatis lies in the mapping relationship between pojo and sql. That is, ibatis does not automatically generate sql execution at run time for programmers. The specific sql needs to be written by the programmer, and then the parameters required by the sql and the returned result fields are mapped to the specified pojo by mapping the configuration file. Using the orm mechanism provided by ibatis, business logic implementers are faced with pure java objects, which is basically the same as implementing orm through hibernate. 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 concession of sql development workload and database portability.
4. Operation efficiency
Regardless of cache, ibatis should be faster or much faster than hibernate.
The above is all the contents of this article entitled "what are the differences between Ibatis and Hibernate?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.