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.

MyBatis knowledge points summed up a mind map to share with you

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.

Tips: Follow wechat public account: Programmer Bai Nannan to get daily push.

The following tools use IDEA

1.1 Creating a Maven project

1.1.1 Select New project from the menu

File | New | Project

1.1.2 Select Maven on the left

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

1.1.3 Enter GroupId and ArtifactId

  • In my project,

GroupId fill in com.homejim.mybatis ArtifactId fill in 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> <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> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" password="jim777"/> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <javaModelGenerator targetPackage="com.homejim.mybatis.entity" targetProject=".\src\main\java"> <property name="enableSubPackages" value="false"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <sqlMapGenerator targetPackage="mybatis/mapper" targetProject=".\src\main\resources"> <property name="enableSubPackages"  value="false"/> </sqlMapGenerator> <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 of the jar package of the local database driver (must be changed).

2. Database configuration (must be changed)

3. Configuration of related tables (must be changed)

4. Location where the entity class is generated and stored.

5. Store the generated MapperXML file.

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-SNAPSHOT</version> <build> <finalName> Mybatis - Generator </finalName> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> < version > 1.3.7 < / version > < configuration > < 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

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

2.1 document

It is recommended to check the official documentation.

Good in English: www.mybatis.org/generator/c…

Mbg.cndocs. Ml /index.html

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 will become ‘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

<property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>

<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.

This tag is not present in the generated XML. 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.

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.

conclusion

The 2020 interview questions are divided into 19 modules: Java Basics, Containers, multithreading, reflection, object copy, Java Web, exceptions, Networking, Design patterns, Spring/Spring MVC, Spring Boot/Spring Cloud, Hibernate, MyBatis, RabbitMQ, Kafka, Zookeeper, MySQL, Redis, JVM.

Pay attention to my public number: programmer Bai Nannan, access to the above information.