Brief introduction
What is MyBatis?
MyBatis is an excellent persistence framework that supports custom SQL, stored procedures and advanced mappings. MyBatis avoids almost all JDBC code and manual setting parameters and obtaining the result set. MyBatis can use simple XML configuration and mapping annotations or native types, interfaces and Java POJO (Plain Old Java Objects, plain old Java object) is recorded in the database.
Use
- Programming the formula: injection configuration via code manually create SqlSessionFactory, SqlSession, Mapper other objects.
- Integrated: a container such as integrated Spring
Mapper XML and Annotation form
Mapper is used to form a unified, do not use both the form and the use of XML Annotation form, prone to error. XML format used in the proposed project, the project will certainly appear complex SQL, XML and complex SQL better readability.
Compatible form
Mapper XML and Annotation is compatible with complementary forms, the same method name can not exist in the presence of both XML and Annotation.
Advantages and disadvantages
Core components
Outline
- The SqlSessionFactoryBuilder (constructor) : it generates a SqlSessionFactory (factory interface) according to configuration information, or codes.
- SqlSessionFactory : relying on plants to generate SqlSession (session).
- SqlSession : either send a SQL to execute and return the results, you can also get Mapper interface.
- Mapper SQL : it is a component MyBatis new design, which consists of a Java interface and an XML file (or notes) made of, and the need to give the corresponding SQL mapping rules. It is responsible for sending SQL statements, and return the results.
The life cycle
SqlSessionFactoryBuilder
SqlSessionFactoryBuilder is the use of XML or Java coding access to resources to build SqlSessionFactory, through which you can build multiple SessionFactory. It is the role of a builder, once we build a SqlSessionFactory, its role has come to an end, he lost the meaning of existence. It exists only in the life cycle of internal method.
SqlSessionFactory
SqlSessionFactory role is to create SqlSession, and SqlSession is a conversation, the equivalent of the JDBC Connection object. Each time the application access to the database, we need to create SqlSessionFactory SqlSession, so should the entire life cycle SqlSessionFactory MyBatis applications. And if we create multiple SqlSessionFactory the same database, then each time you create SqlSessionFactory will open more database connection resources, then the connection will soon be depleted resources. So SqlSessionFactory responsibility is unique, its responsibility is to create SqlSession, so it should adopt simple interest mode. The correct approach is that each database corresponds to only a SqlSessionFactory, manage database resources allocation, to avoid excessive Connection is consumed.
SqlSession
SqlSession is a conversation, the equivalent of a JDBC Connection object, its life cycle should be in the process of handling requests to the database transaction. It is a thread object insecure when it comes multithreaded We need to be careful, pay attention to the need to operate the database isolation level, the database locks and other advanced effects. In addition, SqlSession each time you create must be promptly closed it and reduce its long-term presence makes the database connection pool resources activities, the impact on system performance too much. We often by finally statement to ensure that our right to close SqlSession. It survived to request an application and operation, can execute multiple SQL, ensure transactional consistency.
Mapper
Mapper is an interface, but no specific implementation class, its role is to send SQL, and return the results we need, or execute SQL to modify data in the database, so it should be within a SqlSession transaction method of, is a method level s things.
FIG described by a life cycle MyBatis components:
MyBatis Practical Tips
MyBatis Example - Introduction
Example MyBatis - processor type
MyBatis example - a plurality of transmission parameters
Example MyBatis - primary key backfilling