There are a lot of comments on these two Persistence frameworks on the Internet. In any case, "the public opinions are justified." Some people say that ibatis is a "semi-automated" o/R Mapping, and some people say, it is SQL mapping. Here, I will extract several versions to share with you:
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
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;
The find () method is used 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.
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 Introduction
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.
Choose hibernate or ibatis:
Hibernate features:
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 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.
Mapping implements the poing between pojo and database tables, as well as automatic generation and execution of SQL. 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 experience and capabilities, but Hibernate is now a mainstream o/R
The Mapping Framework is faster than ibatis in terms of document richness, product perfection, and version development.
Ibatis features:
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. 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:
In my initial selection, considering the differences between ibatis and hibernate, I planned to select hibernate. During the study, I found the characteristics of ibatis. After analysis and comparison, I chose ibatis. Now I have used ibatis to complete a small and medium-sized project. This project is very satisfactory in terms of performance, maintainability, and scalability.
In this process, I also constantly discuss with people who have used or are using hibernate. In addition, I am constantly following up on the development of hibernate.
In the end, I came to the conclusion that ibatis was the correct choice, and the more I liked it.
Of course, my understanding of ibatis and Hibernate is still very limited, so here some of the ideas about hibernate are incorrect, I hope to be able to get the correct correction from the hibernate experts.
1. ibatis features are easy to master
The ibatis feature can be viewed in the document for half a day or two days. Hibernate may take more than three times to master.
2. ibatis makes it easier to optimize SQL.
Everyone should have a consensus on this. In addition, the SQL statement generated by Hibernate is too ugly. Some friends mentioned that SQL is not very important. I want to emphasize my experience here. Generally, the bottleneck of system performance lies in the database. Therefore, this is an important advantage after comparison between ibatis and hibernate.
3. ibatis Features fine-grained optimization.
3.1 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 sqlupdate table_a set column_1 = # column_1 # Where id = # ID # But 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 fields that have been modified, but I am not sure about the negative effects of this function. Therefore, ibatis has obvious advantages over hibernate.
3.2 I need to list part of the content 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
3.2.1 In general, 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?
3.2.2 If Hibernate is used, you cannot set the two unnecessary fields as lazy load, because there are still many places to load the entire domain object at a time. At this time, we can see the benefits of ibatis.
3.2.3hibernate Another solution is to generate JavaBean/MAP/object [] (thanks to leelun/cjmm), but this may produce a large number of redundant classes. The MAP/object [] method should be good. I prefer this method.
3.3 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.
4. Development
4.1 development efficiency, I think ibatis and hibernate should be similar
4.2 In terms of maintainability, I think ibatis is better. Because ibatis SQL is saved to a separate file. In some cases, Hibernate may save SQL/hql in Java code.
5. Operation Efficiency
5.1 without considering cache, ibatis should be faster or much faster than hibernate (depending on the actual situation ).
Of course, ibatis and hibernate also have relatively large disadvantages.
1. The support for different database types is not good. If the system you want to develop is to be transplanted between data pairs, it may be better to use hibernate.
2. The default cache support is not good, but the hibernate cache support is not very good and complicated. Especially for applications with large concurrency. Therefore, I prefer to manage the cache myself.
Here, let's also talk about the seven features of ibatis:
What are ibatis features? As you know, ibatis can be used in any system. Why does a framework like ibatis benefit your application from the architecture level? Let's start our introduction.
Ibatis feature 1 simplicity
Ibatis is widely regarded as one of the simplest Persistence frameworks available today. The concept of simplicity is rooted in the ibatis Development Team, which ranks first among all ibatis development goals. This simplicity is achieved because ibatis is directly built on JDBC and SQL. Ibatis is very simple for Java developers, because it is very similar to the working mechanism of JDBC in addition to not writing so much code. Almost everything you know about JDBC is also applicable to ibatis. You can almost think that ibatis is a JDBC code described in XML format. Some people say that ibatis has many architecture-level advantages that JDBC does not have. These advantages will be discussed later. Ibatis is also very easy to understand for database administrators and SQL programmers. Almost everyone can understand the ibatis configuration file, as long as he has experience in SQL programming.
Ibatis Feature 2 production efficiency
Any excellent framework aims to enable users of the framework to achieve higher production efficiency. In general, the framework is responsible for processing public tasks, reducing the compilation of repetitive code like a sample, and solving complex architecture-level problems. Ibatis has been very successful in bringing higher development efficiency to developers. In a case study conducted by the Italian Java User Group (Java Users Group) (see www.jugsardegna.org/vqwiki/jsp/wiki? Ibatiscasestudy), Fabrizio gianneschi found that ibatis reduces the code volume of the persistent layer by about 62%. The reason for reducing the amount of code is that developers no longer need to write tedious JDBC code. SQL statements are still hard-coded, but as you can see before this chapter, SQL is not a problem-the problem lies in JDBC
For ADO. net.
Ibatis feature 3 Performance
This topic of performance will undoubtedly cause a heated debate between framework developers, framework users, and commercial software developers. The fact is that at a lower level, all the frameworks will undoubtedly cause performance loss. In general, if you use hard-coded JDBC code and ibatis code for a 1,000,000 for loop, you will find that JDBC has some advantages in performance. Fortunately, in modern application development, the performance loss caused by such a for loop is not important. What really matters is how often you get data from the database and when you get it. For example, the reason why paging query can greatly improve application performance is that it will not retrieve thousands of records from the database once. Similarly, features such as lazy loading can avoid loading data that is not needed in the current situation. On the other hand, if you are sure you want to load a complex object graph that involves a large amount of data from multiple tables, if only one SQL statement can be used to load all objects, the efficiency can be greatly improved. ibatis supports such SQL statements. Ibatis supports many performance optimization measures, which will be discussed in detail later in this chapter. At present, the most important thing is to know that ibatis can always be configured and used in a simple way, and its performance is equivalent to or even better than that of JDBC. Another issue that needs to be considered is that not all JDBC code is well written. JDBC
The API is very complex and you need to be very careful when writing the correct code. Unfortunately, a lot of JDBC code is poorly written, so at a lower level, ibatis is not even working well.
Ibatis Feature 4 separation of concerns
In a typical JDBC code, the code related to the database (for example, getting a connection or traversing a result set) can be found at all layers of the application. This is not uncommon. I believe everyone has seen or even developed such a bad application-all logic is completed on a JSP page, connected to the database, executed SQL statements, and traversed the result set, of course, there are also HTML code scattered in it. Such code is a nightmare. In the first chapter, we have discussed the importance of application layering. We have seen how to layer applications from a higher perspective, and how to further layer the persistence layer. Ibatis supports layering by helping to manage all these persistence-related resources, including database connections
Connection), prepared statement, and result set ). Ibatis provides a set of database-independent interfaces and APIs, so that other parts of the application can maintain the independence of database-related resources by using these interfaces and APIs. With ibatis, your code is always dealing with strict objects directly, and you no longer have to worry about random result sets. Ibatis actually makes it difficult for you to violate the best practices of application layering.
Ibatis feature 5 clear division of labor
Some database administrators love their databases so much that they do not want anyone else to write SQL statements for the database. Some people are so good at writing SQL that others want them to do the job. For whatever reason, it is always advantageous to achieve human attention in your development team. If someone in your team is especially good at writing SQL, but is not so good at Java or C #, they can write SQL specifically. Ibatis makes this division of labor possible. In ibatis, SQL statements are largely separated from the source code of the application. SQL programmers can write SQL statements in the original way, you don't have to worry about SQL string connection. Even if the same developer writes Java code and SQL
Tune) also has a common request from DBA, that is, "Show SQL ". It is not easy to use JDBC to accomplish this, because SQL is hidden deeply and obtained by connecting a bunch of scattered strings. It may even be dynamically created in recursive statements and condition statements. If you use object relationship ing, the situation is more complicated, because you usually have to run the application before you can log out these SQL statements. Even if you find them, you cannot change them at all. Ibatis provides sufficient freedom for anyone to develop, observe, and modify SQL statements executed in the database.
Ibatis feature 6 portability
Ibatis is portable. Because of its relatively simple design, it can be implemented in almost any language on any platform. Ibatis supports three most popular Development Platforms: Java, Ruby, and C # of Microsoft. NET #.
Currently, configuration files are not fully cross-platform compatible, but we are planning to make them more compatible. It is important that concepts and methods are portable. This allows you to ensure that all your applications are consistent in design. Compared with other frameworks, ibatis can process more languages and more types of applications without considering the differences in application design. If application consistency is important to you, using ibatis can help you achieve consistency.
Ibatis features 7 Open Source and honest
At the beginning, we mentioned that this section is "self-defeating. In fact, ibatis is a free open-source software. No matter whether you use it or not, we will not get the benefit of even a penny. You have bought a book, so we have gained the benefits we deserve. One of the biggest advantages of open-source software is honesty. We have no reason to lie to you. Therefore, what we will do next is very rare in the commercial software documentation: we will discuss some situations where you may not use ibatis and propose some suitable alternatives.
Here we will introduce the ibatis feature, and hope that you can better understand ibatis through this explanation.