Preface
In JavaWeb development, the most classic ones are the SSH framework combination and the SSM framework combination. Now many IT companies are willing to use SSM. For H and M here, Hibernate and MyBatis, let me briefly talk about it today.
Playing axe in the door
During class, I often tell students that you must understand any technique:
Knowledge explanation
Hibernate and MyBatis are both very popular ORM (object-relational mapping, to put it bluntly, a technology that maps to databases) persistence layer frameworks. In essence, it is still encapsulated JDBC to facilitate our use and simplify our development. Let’s figure out a few issues first:
Why can the introduction of a persistence layer solve this problem? Analysis: A mechanism can be introduced in the persistence layer, which can be used as a translation. For different databases at the bottom, HQL statements and dynamic query statements are translated into different SQL statements. This solves the problem that there is no need to change SQL statements after replacing the database, and the amount of code will be greatly reduced.
It's so good, so how did Hibernate and MyBatis implement the above solution, that is, how did their ORM be implemented?
Hibernate
In Hibernate, this mechanism is a configuration file hibernate.cfg.xml (placed in the src directory)
<hibernate-configuration> <session-factory> <property name="connection.url">jdbc:mysql://localhost/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">saber</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- ddl statement automatically creates tables --> <property name="hbm2ddl.auto">none</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <!-- Connection pool configuration--> <property name="hibernate.connection.provider_class"> org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider </property> <!-- Minimum number of JDBC connections in the connection pool. Hibernate defaults to 1 --> <property name="hibernate.c3p0.min_size">5</property> <!-- The maximum number of JDBC connections in the connection pool. Hibernate defaults to 100 --> <property name="hibernate.c3p0.max_size">20</property> <!-- When to remove an idle connection in seconds from the connection pool. The default is 0, never expires --> <property name="hibernate.c3p0.timeout">300</property> <!-- The number of cached precompiled statements. Used to improve performance. Hibernate defaults to 0, cache is not available --> <property name="hibernate.c3p0.max_statements">100</property> <!-- Idle time (in seconds) before a connection is automatically verified. Hibernate defaults to 0 --> <property name="hibernate.c3p0.idle_test_period">3000</property> <!-- Running independently. If you get the Session through getCurrentSession(), you need to set it as follows --> <property name="current_session_context_class">thread</property> <!-- Two ways to map persistent classes --> <mapping /> <mapping resource="com/serup/model/Teacher.hbm.xml" /> </session-factory></hibernate-configuration>
Once the project has changed the database, what needs to be modified is just several attributes in this file, and the logic code of the business layer does not need to be modified.
Using Hibernate for development is recommended:
** Domain Object ---> Mapping ---> DataBase In other words, objects and tables need to have a mapping, and this mapping has two configuration methods in Hibernate**
1. XML method (class name.hbm.xml)
Establish mapping relationship between table and class through hbm file
2. Annotation method
Later, with reverse engineering, it became much simpler, and you can directly generate the corresponding file through the database table.
MyBatis
In MyBatis, this mechanism also relies on a configuration file mybatis-config.xml (placed in the src directory)
<configuration> <properties resource="sqlserverinfo.properties"/><!--Configuration File of the Database--> <typeAliases> <package name="com.demo.domain"/> <!--Scan packages with type alias to scan a file separately--> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <!--Mapping files to complete specific crud operations--> <mapper resource="com/demo/domain/UserMapper.xml"/> </mappers></configuration> In this way, every time you change the database, you only need to modify the configuration file.
Using MyBatis for development recommends this:
** Create a database and create various tables ---> Reverse engineering automatically generates the code required for MyBatis execution (mapper.java,mapper.xml, po..) ---> Write the control layer and service layer according to business logic. **
Differences
There are various comparisons on the Internet, and I only talk about some different points I think
1. Hibernate has an object-oriented query language called HQL. This is very powerful. It allows apes who do not know SQL statements to add, delete, modify and search the database, but this is also its weakness. SQL statements cannot be optimized.
2. MyBatis requires SQL statement configuration, which means that it still requires basic knowledge of the database to get started. Moreover, beginners must have encountered many pitfalls in the result mapping.
3. I have never done a particularly large project. If there are no special performance requirements, HIbernate is still easier to implement functions.
System tuning
Hibernate's Tuning Solution
Mybatis Tuning Solution
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.