In the first article of the MyBatis series, it is mentioned that there are many problems in the actual development of JDBC programming, and then there is no analysis after introducing MyBatis, mybatis How to solve the problem of JDBC. At the beginning of this blog, let's start with a look at how MyBatis solves the problem of JDBC programming. Of course, all ORM frameworks are similar, both encapsulation and optimization of JDBC.
1, the database link creation, the release frequently causes the system resources to waste thus affects the system performance, if uses the database link pool solves this problem.
Workaround: Configure the data link pool in Sqlmapconfig.xml and use connection pooling to manage database links.
2, SQL statements written in the code cause code is not easy to maintain, the actual application of SQL changes may be large, SQL changes need to change the Java code.
WORKAROUND: Configure the SQL statement to be detached from the Java code in the Xxxxmapper.xml file.
3. It is troublesome to pass arguments to SQL statements because the WHERE condition of the SQL statement is not necessarily, possibly more or less, and the placeholder needs to correspond to parameter one by one.
Resolution: MyBatis Automatically maps Java objects to SQL statements, defining the type of input parameters through ParameterType in statement.
4, the result set parsing trouble, SQL changes lead to parsing code changes, and before parsing need to traverse
Resolution: MyBatis automatically maps SQL execution results to Java objects, defining the type of output results through Resulttype in statement.
Many people like to MyBatis and hibernate the two frameworks together to compare, perhaps these two are more mainstream persistence layer framework, or perhaps in the actual project development with more, the problem is more, so there is the need to choose.
1 from the ORM definition, MyBatis and hibernate are different, it is not a complete ORM framework, if Hibernate is an automated ORM, then MyBatis is a semi-automated framework, Because MyBatis requires programmers to write SQL statements themselves, because of this, MyBatis introduces XML configuration files or annotations to manage and configure SQL statements to run, and to map Java objects and SQL statements into the final executed SQL. Finally, the results of SQL execution are then mapped to Java objects, so flexibility is greatly improved.
2 in terms of learning costs, MyBatis is clearly more simple and easier to get started with. Programmers can quickly develop the business code of a system as long as they understand the process developed using the MyBatis framework and are proficient in writing SQL statements. In view of hibernate, the high threshold of learning, I believe that many of the friends who have studied hibernate have deep experience, to be proficient in hibernate and to be able to do some flexible processing and optimization hibernate, will be more difficult, and how to design O/R mapping, The tradeoffs between performance and the object model and how to use hibernate require strong experience and ability.
3 from the development efficiency, mybatis development efficiency is lower than hibernate, for advanced queries, a large number of mapping files to write work, people are very crazy. In contrast, hibernate encapsulation is very good, without the programmer to write SQL statements, as long as the operation of the object to manipulate the database data, developers do not care about SQL generation and result mapping, really do the ORM relationship and object mapping, more object-oriented flavor.
4 with regard to Tuning scheme, MyBatis can perform detailed SQL optimization design, can use level two caching mechanism, and reasonable session management mechanism. But its most important optimization point is SQL optimization, because MyBatis's SQL is written manually, so we can redesign it to the effect we want. and Hibernate HQL statement tuning needs us to print out the SQL statements, to analyze to adjust the HQL statement, to achieve the goal of optimization. In addition, hibernate tuning can also use a reasonable caching strategy, using lazy loading features, using batch fetching, and designing a reasonable O/R mapping.
5 in terms of extensibility, it is clear that hibernate is a better one, and Hibernate's association with a specific database can only be configured in an XML file, and all HQL statements are not related to the database in use, so portability is good. All SQL statements in the MyBatis project are dependent on the database used, so different databases need to write different mapping files, and the portability is not good.
6 in terms of performance, Hibernate has a single encapsulation on JDBC, and MyBatis is based on native JDBC, so mybatis inherently has the advantage of running speed from the original framework design. In addition, Hibernate, in order to ensure the data integrity of Pojo, need to load the associated data all, need additional query more data, and mybatis loaded field is clean, not too many redundant fields, directly mapped into the association, from this point of view, MyBatis is also superior to hibernate.
7 about Hibernate and MyBatis comparative analysis, said a lot of, actually use which to see the specific use of the scene, for the management platform, need to show a large amount of information, concurrent requirements are not high, use hibernate better, and for some small internet sites, Business requirements change more frequently, concurrency is also relatively high, it is appropriate to use MyBatis.
In short, in accordance with the needs of users in a limited resource environment as long as the maintenance and extensibility of a good software architecture is a good architecture, so the framework only suitable is the best.