Biography: Swing unruly, love life. Java Cultivator (wechat official ID: Java Cultivator), welcome to follow. Access to 2000G of detailed information on the 2020 interview questions

In the process of using MyBatis, as you write more and more handwritten Javabeans and XML, you will more and more agree to error. We certainly don’t want to do that much repetitive work.

Fortunately, Mybatis provides us with a powerful code generator –MybatisGenerator.

With simple configuration, we can generate various types of entity classes, Mapper interfaces, MapperXML files, Example objects, and so on. Through these generated files, we can easily add, delete, change and check the operation of a single table.

The following tools use IDEA

Create a code generator

1.1 Creating a Maven project

1.1.1 Select New project from the menu

File | New | Project

1.1.2 Select the one on the leftMaven

Since we are just creating a normal project, click Next here.

1.1.3 inputGroupIdandArtifactId

  • In my project,

Fill the GroupId com. Homejim. Mybatis

ArtifactId fill mybatis generator

Click Next.

1.1.4 Finish

With the above steps, a normal Maven project is created.

1.2 configure the generator. The XML

It doesn’t matter what the name is, as long as it matches the name in the pom.xml file below.

<? The 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> <! -- Full path to the jar package of the local database driver --> <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> <! - a database of relevant configuration - > < jdbcConnection driverClass = ". Com. Mysql. JDBC Driver "connectionURL =" JDBC: mysql: / / localhost: 3306 / mybatis" userId="root" password="jim777"/> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <! - the position of the entity class generation - > < javaModelGenerator targetPackage = "com. Homejim. Mybatis. Entity" targetProject = ". \ SRC/main/Java "> <property name="enableSubPackages" value="false"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <! SqlMapGenerator --> <sqlMapGenerator targetPackage="mybatis/ Mapper" targetProject=".\src\main\resources"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <! - the location of the Mapper interfaces file - > < javaClientGenerator type = "XMLMAPPER targetPackage" = ". Com. Homejim. Mybatis Mapper" targetProject=".\src\main\java"> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <! <table tableName="blog" /> </context></generatorConfiguration>Copy the code

Some things need to be changed:

  1. The full path to the jar package for the local database driver (which must be changed).

  2. Database configuration (must change)

  3. Configuration of related tables (must change)

  4. The location where the entity class generation is stored.

  5. MapperXML is the location where the generated file is stored.

  6. Location of the Mapper interface.

If you do not know how to change, see the following configuration details.

1.3 configuration pom. XML

Add something to the original.

<? The 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" > The < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Homejim. Mybatis < / groupId > < artifactId > mybatis generator - < / artifactId > < version > 1.0 - the SNAPSHOT < / version > <! Mybatis -generator</finalName> <plugins> <plugins> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> The < version > 1.3.7 < / version > < configuration > <! -- Notice here, File with the above matching - > < configurationFile > SRC/main/resources/generator. The 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> < the groupId > org. Mybatis. The generator < / groupId > < artifactId > mybatis generator - core < / artifactId > < version > 1.3.7 < / version > </dependency> </dependencies> </plugin> </plugins> </build> <! --></project>Copy the code

Note that the file in the configurationFile refers to generator.xml. Therefore, the path writes the relative path of the file and has the same name as the file.

At this point, mybatis- Generator is ready to use.

1.4 Use and test

1.4.1 Opening the Maven Projects view

On IDEA, open:View | Tools | Windwos | Maven Projects

1.4.2 Maven Projects double-click Mybatis -Generator

You can now see Maven Projects on the right. Find the Mybatis – Generator plug-in.

mybatis-generator | Plugins | mybatis-generator | mybatis-generator

1.4.3 Double-click to run

After running correctly, the code is generated and the following structure is obtained

2 XML Configuration Details

The above simple use is not enough. The generated configuration can then be made by changing the generator.xml configuration file.

2.1 the priority

It is recommended to check the official documentation.

English good: official website.

Translated version: translated version of the website

2.2 There is no official website

2.2.1 the property tag

This tag is used to specify the attributes of an element, but it is not explained in detail.

2.2.1.1 Delimiter correlation

<property name="autoDelimitKeywords" value="true"/><property name="beginningDelimiter" value="`"/><property name="endingDelimiter" value="`"/>
Copy the code

The above configuration corresponds to mysql, and delimiters are used when the fields in the database are the same as the keywords in the database.

For example, if our data column is DELETE, after the above configuration, where it appears, it becomes DELETE.

2.2.1.2 coding

The default is to use the encoding of the current system environment, which can be configured as GBK or UTF-8.

<property name="javaFileEncoding" value="UTF-8"/>
Copy the code

I think the project is UTF-8. If you specify GBK generation, the automatically generated Chinese is garbled.

2.2.1.3 formatting

<! - the formatting of the generated Java code - > < property name = "javaFormatter" value = "org. Mybatis. Generator. API. Dom. DefaultJavaFormatter" / > <! - the formatting of the generated XML - > < property name = "xmlFormatter" value = "org. Mybatis. Generator. API. Dom. DefaultXmlFormatter" / >Copy the code

These are obviously customizable implementations.

2.2.2 the plugins TAB

Plugins tags are used to extend or modify the code generated by the code generator.

In the generated XML, there is no **** tag. This label is configured for caching.

If we want to generate this tag, we can configure it in plugins.

<plugin type="org.mybatis.generator.plugins.CachePlugin" >            <property name="cache_eviction" value="LRU"/></plugin>
Copy the code

For example, you might want to implement Serializable interface in your generated JavaBean.

<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
Copy the code

You can also customize plug-ins.

These plug-ins are quite useful, feel the follow-up can be dedicated to open an article to explain.

2.2.3 commentGenerator label

You can see from the name that it is used to generate comments.

Default configuration:

    <commentGenerator >        <property name="suppressAllComments" value="false"/>        <property name="suppressDate" value="false"/>        <property name="addRemarkComments" value="false"/>    </commentGenerator>
Copy the code

SuppressAllComments: Prevents comments from being generated. Default is false.

SuppressDate: Prevents generated comments from including time stamps. Default is false.

AddRemarkComments: Adds comments for the database to comments. Default is false.

Another is that we can specify our custom annotation implementation class through the Type attribute to generate our own annotations. The realization of the custom class needs to implement mentGenerator at org.mybatis.generator.api.Com.