Source :https://www.cnblogs.com/homejim/p/9782403.html
In the use of mybatis In the process , When handwriting JavaBean and XML When I write more and more , More and more agree to make mistakes . This repetitive work , We certainly don't want to do so much .
not so bad , mybatis Provides us with powerful code generation --MybatisGenerator.
Through simple configuration , We can generate various types of entity classes , Mapper Interface , MapperXML file , Example Object etc. . Through these generated files , We can easily add, delete, modify and query a single table .
The following tools use IDEA
1 Create a code generator
1.1 establish Maven project
1.1.1 Select new project from the menu
File | New | Project
1.1.2 Choose the one on the left Maven
Because we're just creating a normal project , Click here Next
that will do .
1.1.3 Input GroupId
and ArtifactId
In my project ,
GroupId fill com.homejim.mybatis
ArtifactId fill mybatis-generator
Click on Next
.
1.1.4 Finish
Go through the above steps , An ordinary Maven
The project is created .
1.2 To configure generator.xml
The name doesn't matter , Just follow the one below pom.xml The correspondence in the document is good .
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC
"-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration>
<!-- Local database driver jar Full path of package -->
<classPathEntry location="C:\Users\\Administrator\\.m2\repository\\mysql\\mysql-connector-java\\8.0.12\\mysql-connector-java-8.0.12.jar"/>
<context id="context" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="false"/>
<property name="suppressDate" value="true"/>
</commentGenerator>
<!-- Database configuration -->
<jdbcConnection
driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis"
userId="root"
password="jim777"/>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- Where entity classes are generated -->
<javaModelGenerator
targetPackage="com.homejim.mybatis.entity"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- *Mapper.xml The location of the file sqlMapGenerator-->
<sqlMapGenerator
targetPackage="mybatis/mapper"
targetProject=".\src\main\resources">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- Mapper Location of interface files -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.homejim.mybatis.mapper"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- Configuration of related tables -->
<table tableName="blog" />
</context>
</generatorConfiguration>
Something needs to be changed :
Local database driver jar Full path of package ( It has to be changed ).
Database configuration ( It has to be changed )
Configuration of related tables ( It has to be changed )
Location of entity class generation and storage .
MapperXML The location where the generated files are stored .
Mapper Where the interface is stored .
If you don't know how to change , Please look at the back Configuration details .
1.3 To configure pom.xml
Add something to the original .
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.homejim.mybatis</groupId>
<artifactId>mybatis-generator</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- Add... To the original That's it -->
<build>
<finalName>mybatis-generator</finalName>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<!-- Notice here , The file matches the file above -->
<configurationFile>src/main/resources/generator.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<!-- To this end -->
</project>
It should be noted that configurationFile The document in refers to generator.xml. So the path is the relative path of the file , The name is the same as the file .
Here we are , mybatis-generator You can use it .
1.4 Use and test
1.4.1 open Maven Projects View
stay IDEA On , open :
View | Tools | Windwos | Maven Projects

1.4.2 Maven Projects Middle double click mybatis-generator
On the right, you can see Maven Projects 了 . find mybatis-generator plug-in unit .
mybatis-generator | Plugins | mybatis-generator | mybatis-generator

1.4.3 Double-click on the run
After correct operation , The generated code , The following structure is obtained
2 XML Configuration details
It's not cool just to use it as simple as above . Then we can change generator.xml Configuration file to generate the configuration .
2.1 first
Official documents for reference .
Good English : Official website .
Chinese translation : Translated website
2.2 It's not on the official website
2.2.1 property label
The tag is only used to specify the attributes of elements in the official website , As for how to use it, there is no detailed explanation .
2.2.1.1 Separator related
<property name="autoDelimitKeywords" value="true"/>
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
The above configuration corresponds to mysql, When the fields in the database are the same as the keywords in the database , You'll use the separator .
For example, our data column is delete
, According to the above configuration , Where it appears , It becomes delete
.
2.2.1.2 code
The default is to use the encoding of the current system environment , Can be configured as GBK or UTF-8.
<property name="javaFileEncoding" value="UTF-8"/>
I think the project is UTF-8, If you specify a build GBK, Then automatically generated Chinese is garbled code .
2.2.1.3 format
<!-- Format generated Java Code -->
<property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
<!-- Format generated XML-->
<property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
These are obviously customizable .
2.2.2 plugins label
plugins Tags are used to extend or modify code generated by code generators .
In the generated XML in , It's not **** This label's . This tag is configured for caching .
If we want to generate this tag , Then you can. plugins To configure .
<plugin type="org.mybatis.generator.plugins.CachePlugin" >
<property name="cache_eviction" value="LRU"/>
</plugin>

Like what you want to generate JavaBean It can be realized by itself Serializable Interface .
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />

You can also customize plug-ins .
These plug-ins are very useful , I feel that I can open an article to explain .
2.2.3 commentGenerator label
Look at the name , It's used to generate comments .
The default configuration :
<commentGenerator >
<property name="suppressAllComments" value="false"/>
<property name="suppressDate" value="false"/>
<property name="addRemarkComments" value="false"/>
</commentGenerator>
suppressAllComments: Prevent comments from being generated , The default value is false.
suppressDate: Prevent generated comments from including timestamps , The default is false.
addRemarkComments: Add database comments to comments , The default is false.
And then there's us Can pass type Attribute specifies our custom annotation implementation class , Generate annotations we want . Custom implementation classes need to implement org.mybatis.generator.api.CommentGenerator
.
Recommended reading
Code comparison tool , I'll use this 6 individual
Share my favorite 5 A free online SQL Database environment , It's so convenient !
Spring Boot A combination of three moves , Hand in hand to teach you to play elegant back-end interface
MySQL 5.7 vs 8.0, You choose the one ? Net friend : I'm going to stay where I am ~
Last , I recommend you an interesting and interesting official account : The guy who wrote the code ,7 Old programmers teach you to write bug, reply interview | resources Send you a complete set of Development Notes There's a surprise