How to generate code using mybatis generator?

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:

[how to generate code using mybatis generator?]

[small java class of Xiuzhen academy]

MYBATIS GENERATOR

(1) Background:

MyBatis Generator

Mybatis generator (MBG) is a code generator for mybatis

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.

You still need to write SQL and objects for federated queries and stored procedures

(2) Knowledge analysis:

How to implement MBG

1. Use the command line to generate (understand)

2. Generate using Maven plug-in (common)

Generator used in two ways The configuration in the XML file is the same. The first one needs to use the command line to load the jar package, and the second one needs to use the editor to configure POM xml

Steps for code generation using mybatis generator

Step 1: prepare database and table

Step 2: 3 dependencies and 1 plug-in

mybatis. jar

mybatis-generator-core. jar

mysql-connector-java. jar

mybatis-generator-maven-plugin

Step 3: write the configuration file generatorconfig xml

a. Configure database connection address, account and password

b. Storage location of generated model class

c. Storage location of generated mapping file

d. Specify the table to build (naming convention)

The code generated mapping file provides several common CRUD operations

1.insert()

2.insertSelective()

3.updateByPrimaryKeySelective()

4.updateByPrimaryKey()

5.selectByPrimaryKey()

6.deleteByPrimaryKey()

Running process of mybatis generator

Connect to the database - get fields from the database table - generate mapper files, Dao, and entity classes based on the fields

Generation method

1. Use the command line to generate

2. Generate using Maven plug-in

(3) Frequently asked questions:

What if the database naming standard is different from the Java Naming standard?

Mapper can only be generated reversely XML file, can I generate an annotation mapper

(4) Solution:

1. If it is not set, mybatis generator will automatically convert the underline rules in the database into hump rules in Java and generate entity classes

2. You can use generatorconfig XML settings

Change the type value in the javaclientgenerator parameter from xmlmapper to annotatedmapper

(5) Coding practice:

(6) Expand thinking:

(7) References:

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

https://gitee.com/free/Mybatis_Utils/blob/master/MybatisGeneator

(8) More discussion:

1. What is example and criteria?

A: the example class is used to generate any where clause. It contains an internal static class, criteria. The criteriz maintains a list of conditions that are ANDed to the where clause. 2. What is the difference between the createcriteria () method and the or () method?

A: both methods can be used to create criteria objects. The difference is that the first criteriar object created by the critercriteria method will be automatically added to the list of criteria. If it is created by the or () method, it is added to the list in all instance.

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.

(9) Thanks:

Thanks to elder martial brothers XX and XX. This tutorial is based on their previous technology sharing.

(10) Conclusion:

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
分享
二维码
< <上一篇
下一篇>>