First, the work flow chart of MyBatis
(1), the principle see:
The MyBatis application is created from an XML configuration file based on the configuration, the configuration is from two places, one is the configuration file, one is the Java code annotation, gets a sqlsession. Sqlsession contains all the methods required to execute SQL, you can run the mapped SQL statements directly through the Sqlsession instance, complete the additions and deletions of the data and transaction commits, etc., and then close the sqlsession.
Ii. advantages and disadvantages of MyBatis
Advantages:
1. Easy and easy to learn
The mybatis itself is small and simple. No third-party dependencies, the simplest installation as long as two jar files + configuration of several SQL mapping files easy to learn, easy to use, through the documentation and source code, you can compare the complete grasp of its design ideas and implementation.
2. Flexible
MyBatis does not impose any impact on the existing design of an application or database. SQL is written in XML to facilitate unified management and optimization. With SQL you can basically implement all the features that we can do without using the data access framework, perhaps more.
3. Decoupling SQL from program code
By providing a DAL layer that separates business logic from data access logic, the system is designed to be clearer, easier to maintain, and easier to unit test. The separation of SQL and code improves maintainability.
4, provide mapping label, support ORM field relationship mapping between object and database
5, provide the object relation mapping tag, support the object relation establishment maintenance
6, provide XML tags, support to write dynamic SQL.
Disadvantages:
1, the work of writing SQL statement is very large, especially the field many, the association table A long time, more so.
2, SQL statements rely on the database, resulting in poor database portability, can not replace the database.
3, the framework is relatively humble, the function is still missing, although the data binding code is simplified, but the entire underlying database query is actually to write their own, the workload is relatively large, and it is not easy to adapt to rapid database modification.
4, level Two cache mechanism is poor
Third, Hibernate's working principle flowchart
Hibernate is a further encapsulation of JDBC
Originally did not use Hiberante to do persistent layer development, there are a lot of redundancy, such as: a variety of JDBC statements, connection management, so there has been hibernate to the JDBC encapsulation, we do not have to manipulate the data, directly manipulate it on the line.
Five, from the perspective of stratification
A typical three-tier architecture: The presentation layer, the business layer, and the persistence layer. Hiberante is also the framework of the persistence layer, and there are many frameworks for the persistence layer, such as IBATIS,NHIBERNATE,JDO,OJB,EJB and so on.
Hibernate is an ORM for open source (Object relational mapping) Framework .
ORM, or Object-relational Mapping, is a mapping between relational databases and objects. Maps from objects (object) to Relationships (Relation), and then from relationships to objects. In this way, when we operate the database, we do not need to deal with the complex SQL, as long as the operation of the object as it does it (the relational database of the field in memory mapped to the object's properties).
The core of Hibernate:
Hibernate six core interfaces, two main profiles
1. Configuration interface: Responsible for configuring and starting Hibernate
2, Sessionfactory interface: responsible for initializing hibernate
3. Session interface: CRUD Operations for persistent objects
4. Transaction interface: Responsible for business
5, query interface and the criteria interface: responsible for the implementation of various database queries
Note: The configuration instance is an object during startup, and once Sessionfactory is created it is discarded.
Hibernate operation process:
1. Through the configuration (). Configure (); Read and parse the Hibernate.cfg.xml config file
2. Read and parse mapping information by <mappingresource= "Com/xx/user.hbm.xml"/> in Hibernate.cfg.xml
3. Through Config.buildsessionfactory ();//Create Sessionfactory
4.sessionfactory.opensession ();//Open Sesssion
5.session.begintransaction ();//Create Transaction Transation
6.persistent operate persistent Operation // general means SAve This method
7.session.gettransaction (). commit ();//COMMIT Transaction
8. Close session
9. Close Sesstionfactory
Advantages/Disadvantages of Hibernate:
Advantages:
1. More Object
With the object-based thinking operation database, we only need to manipulate the object to be able, the development is more object-based.
2. Transplant Sex
Because Hibernate does a persistent layer of encapsulation, you don't know the database, and all of the code you write is reusable.
3. Hibernate is a non-intrusive framework that does not have an intrusive framework that we call a lightweight framework.
The action and actionform of struts need to be inherited, without struts. Hibernate does not need to inherit any classes and does not need to implement any interfaces. Such objects are called Pojo objects.
4, Hibernate code testing is convenient.
5, improve efficiency, improve productivity.
How hibernate and MyBatis work and how they differ