Mybatis generation strategy
This is the back-end small class of the monastery. Each article is shared from
[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]
Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:
[mybatis generation strategy]
[java small class of Xiuzhen academy] mybatis generation strategy
Hello, I'm tiemu'er, the 35th student of Beijing Branch of it Academy. I'm an honest, pure and kind java programmer. Today, I'd like to share with you the Java task expansion on the official website of the Academy
(1) Background:
MYBATIS
Mybatis is an excellent persistence layer framework, which supports customized SQL, stored procedures and advanced mapping. Mybatis avoids almost all JDBC code and manually setting parameters and getting result sets. Mybatis can use simple XML or annotations to configure and map native information, and map interfaces and Java POJOs (plain old Java objects) to records in the database.
(2) Knowledge analysis:
Phase I: JDBC template
Spring provides a database operation technology, jdbctemplate, which encapsulates JDBC. The syntax style is very close to dbutils. The jdbctemplate can directly operate the database to speed up efficiency, and learning this jdbctemplate is also a preparation for declarative transactions. After all, it is necessary to manipulate the data in the database!
Phase II: mybatis
Each mybatis based application is centered on an instance of sqlsessionfactory. Instances of sqlsessionfactory can be obtained through sqlsessionfactorybuilder. Sqlsessionfactory builder can build an instance of sqlsessionfactory from an XML configuration file or a pre customized configuration instance.
Phase III: mybatisgenerator
Mybatis generator (MBG) is a code generator of mybatis. It can help us generate corresponding entity classes, XML mapper files, interfaces and help classes according to the design of tables in the database (that is, we can use this class for simple CRUD operations). In this way, we can avoid manually creating corresponding classes and XML files every time we use the data of a table, This saves us a lot of time to develop functions related to business logic, but you still need to write SQL and objects for federated queries and stored procedures.
Stage 4: mybatisgenerator + tkmapper
Mybatis code generator (MBG for short), if the database fields change frequently, it needs to regenerate the code repeatedly, and because MBG overwrites the generated code and generates XML by appending, it needs a lot of comparison and modification every time. In addition, only the basic methods such as adding, deleting, modifying and querying have produced a lot of XML content, which seems to be a hindrance 。 In order to solve the problems mentioned above and avoid too many project refactoring in a MBG compatible way, a general mapper is generated by combining some JPA annotations on the basis of MBG. General mapper can simply let you obtain the basic single table method, and it is also convenient to expand the general method. Using general mapper can greatly improve your work efficiency.
Phase V: mybatisplus
Mybatis plus (MP for short) is an enhancement tool for mybatis. On the basis of mybatis, it only makes enhancements and does not change. It is born to simplify development and improve efficiency.
(3) Frequently asked questions:
1. Tools are becoming more and more powerful. Is it necessary for me to learn the previous content
2. How to handle linked table queries
3. Which scheme is better
(4) Solution:
1:
2:
The generated code is based on the table, and the table connection part needs to be supplemented in the configuration file of mybatis
3:
There is no absolutely good solution. In this content, there are two general solutions: "mybatisgenerator + tkmapper + PageHelper" or using mybatisplus
(5) Coding practice:
(6) Expand thinking:
(7) References:
http://mbg.cndocs.ml/
https://blog.csdn.net/isea533/article/details/83045335
http://mp.baomidou.com/
That's all for today's sharing. You are welcome to like, forward, leave messages and make bricks~
Ppt link video link