Mybatis generator code generation

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 generator code generation]

[small java class of Xiuzhen academy] code generation of mybatis generator

Hello, I'm wang Tianchi, the 17th student of Wuhan Branch of it Academy. I'm an honest, pure and kind java programmer. Today, I'd like to share with you the knowledge point in deep thinking - code generation of mybatis generator, Java task 3 on the official website of the Academy

(1) Background:

Mybatis code generator - mybatis generator

Mybatis generator can generate different types of code according to the configuration. The code includes the entity class corresponding to the database table, mapper interface class, mapper XML file and example object.

These codes contain almost all the form operation methods, which can greatly facilitate us to use mybatis.

Automatically generate POJO and mapper XML file, improve development efficiency, as an excellent programmer, "lazy" is a very important advantage.

Of course, the automatic generation is fixed, which is easy for crud operation, but it still needs to be written for some special business and table operations.

He can introspect the tables (or tables) of the database and then generate the underlying objects that can be used to access the tables (or tables).

This eliminates the need to create objects and configuration files when interacting with database tables.

MBG solves some simple CRUD (insert, query, update, delete) operations that have the greatest impact on database operations.

(2) Knowledge analysis:

How to implement MBG

1. Make the database and tables you need

2. Add plug-ins

mybatis. jar

mybatis-generator-core. jar

mysql-connector-java. jar

mybatis-generator-maven-plugin

3. Write the configuration file generatorconfig xml

Advantages and disadvantages of MBG plug-in

advantage

1. It is convenient and fast to save code repetition time

2. Easy to use

shortcoming

Only simple crud can be generated, and some special businesses and tables need to be modified

(3) Frequently asked questions:

How to perform associated table query

After generating a simple crud, you can create it yourself in mapper XML supplement means self supplement

Is this the only way

MYBATISGENERATOR+TKMAPPER

On the basis of mybatis code generator (MBG for short), if the database fields change frequently, the code needs to be regenerated repeatedly, and the generated code and XML are generated by MBG overlay and addition

As a result, a large number of comparisons and modifications are required for each regeneration.

In addition, only basic methods such as addition, deletion, modification and query have produced a large amount 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.

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.

(4) Coding practice:

(5) Expand thinking:

(6) References:

http://mbg.cndocs.ml/quickstart.html

(7) More discussion:

1. Does the MBG plug-in need some kind of configuration environment?

Answer: this plug-in adds elements to the generated SQL map. This plug-in is only available for mybatis3 directed runtime

2. What does the SQL statement include?

A: insert update by primary key update by example (using dynamic where clause) delete by primary key delete by example delete (using dynamic where clause) select by primary key select example (using dynamic where clause) by example these statements have different variants according to the structure of the table (for example, if the table has no primary key, MBG will not generate updates through the primary key function)

3. Is there any problem with dynamic SQL?

A: here is only a simple example. Because of the method name, it is difficult to use, but if the company does not generate code in the future, you can learn.

That's all for today's sharing. You are welcome to like, forward, leave messages and make bricks~

Ppt link video link

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>