All in all you're just another brick in the wall.

0%

Springboot+MybatisGenerator

When developing my Java course project, I learned to deploy Mybatis generator on springboot framework to automatically generate java classes from database(mysql). I find it may be useful in the future so I record it here.

1

Project Initialization

I use the spring initializer provided by IntelliJ IDEA IDE to initialize my springboot project. Watch out for this, you need to select those dependency settings to get full functions of the initializer. JDBC API and MyBatis are universally needed, and the database driver should be decided by your database type.(Mine is MySQL so I select MySQL driver)

2

After initialization done, you should be able to find such dependencies in the image below in pom.xml of your project.

3

Introduce MyBatis Generator Plug-in

What’s coming next is introducing the plug-in to your project. Simply adding those codes below in to pom.xml will do all the magic.

1
2
3
4
5
6
7
8
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
</plugin>
<plugins>
</build>

But introducing the plug-in won’t be enough, we need to further deploy a config file to direct the generator to work.

Deploy MyBatis Generator Plug-in

Before we actually deploy the config file, we need to first build a new mybatis-generator-config.xml and then introduce this file to the pom.xml.

Deploy the File Path of Your Config File

Add those codes into pom.xml.

1
2
3
4
5
6
7
8
9
10
11
12
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<configuration>
<!--mybatis config file path-->
<configurationFile>src/main/resources/mybatis-generator-config.xml</configurationFile>
</configuration>
</plugin>
<plugins>
</build>

Pay attention here, the path should be the path of the config file relative to the pom.xml.

4

Allow Overwriting Generated Files

Sometimes our database is updated, and we need to generate new flies accordingly. The regular way to do this is deleting old files manually, and then using MyBatis Generator to generate new files. But you can also allow the generator to overwrite the old files, so you can update your code without deleting the old files manually.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<!--mybatis config file path-->
<configurationFile>src/main/resources/mybatis-generator-config.xml</configurationFile>
<!--allow overwriting files-->
<overwrite>true</overwrite>
</configuration>
</plugin>
<plugins>
</build>

However, MyBatis Generator only overwrites the old classes and mappers’ interfaces, and leaves the old mapper.xml unchanged, only adds new codes. This ensures the sql codes wrote by users will not be altered by the generator.

Add JDBC Drvier for Database

MyBatis generator needs to connect to database, so the according JDBC driver is necessary. We can add the JDBC driver to the generator manually like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<!--mybatis config file path-->
<configurationFile>src/main/resources/mybatis-generator-config.xml</configurationFile>
<!--allow overwriting files-->
<overwrite>true</overwrite>
</configuration>
<dependencies>
<!-- JDBC for mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
</plugin>
<plugins>
</build>

But most of the time, our project has been deployed with JDBC, so there will be redundancy if we deploy it again.

5

Maven provided includeCompileDependencies attribute to allow plug-in to use dependencies, so we don’t need to deploy it repeatedly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<!--mybatis config file path-->
<configurationFile>src/main/resources/mybatis-generator-config.xml</configurationFile>
<!--allow overwriting files-->
<overwrite>true</overwrite>
<!--add the dependecies of pom into generator-->
<includeCompileDependencies>true</includeCompileDependencies>
</configuration>
</plugin>
<plugins>
</build>

Deploy MyBatis Generator Config

Now it’s time for us to deploy the config file for real. When MyBatis Generator starts, it will access the config file according to the deployment in the pom.xml. And this config is the real director of the generator and the most important part is the context part. The config file needs at least one context to function correctly.

Introduce Outer Configuration File

MyBatis Generator Config allows introducing outer configuration files, and the path should be relative to the current config file.

6

Content in the application.properties:

1
2
3
4
5
# mysql
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ice?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=*****
spring.datasource.password=*****

Pay attention here. The url defines the schema of MySQL that is accessed by MyBatis Generator. Username and password should be your information to connect the database.

Deploy Context

It’s deployed in .

1
2
3
<context id="myContext" targetRuntime="MyBatis3" defaultModelType="flat">

</context>
  • id: whatever you like. Just make sure the ids of multiple contexts are different.
  • defaultModelType: Default value is conditional, and value flat means every table has its own folder.
  • targetRuntime: Default values is MyBatis3. Another option is MyBatis3Simple, this will alter content of the generated mapper interfaces and mapper.xml.

targetRuntime = MyBatis3, the generated codes:

7

targetRuntime = Mybatis3Simple, the generated mappers interfaces and mapper.xml will contain less functions:

8

Sub-elements of Context

MyBatis provides some other sub-elements for users to deploy. Those elements should be given by the strict rules of amount and order:

  1. property (0..N)

  2. plugin (0..N)

  3. commentGenerator (0 or 1)

  4. jdbcConnection (needs connectionFactory or jdbcConnection)

  5. javaTypeResolver (0 or 1)

  6. javaModelGenerator (At least one)

  7. sqlMapGenerator (0 or 1)

  8. javaClientGenerator (0 or 1)

  9. table (1..N)

Plug-in

You can deploy a plug-in like this:

1
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/>

This plug-in add functions of equals and hashCode to the Java classes.

CommentGenerator

This one is used to generate comments of the codes. By default MyBatis generator will generate comments, so you can leave this alone if you want comments.

You can remove comments by deploying like this:

1
2
3
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>

javaTypeResolver

javaTyprResolver defines the transforming rules between JDBC and java data types. You can leave it undeployed to utilize the default rules.

Actually we can only changed the rules of bigDecimal and date types.

javaModelGenerator

This one defines the paths of the generated packages and codes:

1
2
3
4
5
6
<javaModelGenerator targetPackage="com.example.demo.entity" targetProject="src/main/java">
<!-- Enable schema as subpackages, default = false -->
<!--<property name="enableSubPackages" value="false"/>-->
<!-- Enable altering string in set functions, default = false -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>

10

sqlMapGenerator

Defines the paths of mapper.xml

1
2
3
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<!--<property name="enableSubPackages" value="false"/>-->
</sqlMapGenerator>

9

javaClientGenerator

Deploy the path of XXXMapper.java(interfaces), similar to those two generators above.

1
2
<javaClientGenerator targetPackage="com.example.demo.dao" targetProject="src/main/java" type="XMLMAPPER">
<!--<property name="enableSubPackages" value="false"/--></javaClientGenerator>

table

schema is the name of the database, which is unnecessary to deploy when using MySQL, but necessary for oracle.

tableName is the name of table in the database.

domainObjectName is the name of classes to be generated. When it undeployed, Mybatis generator will set the names by PascalCase.

enableXXXByExample is by default true but only functions when targetRuntime = “MyBatis3”. And it will generate a corresponding Example class to help users to do Query by different conditions. You can set it as false if you don’t need it.

1
2
3
4
<table schema="" tableName="user" domainObjectName="User"
enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false" selectByExampleQueryId="false">
</table>

Use MyBatis Generator

Now all deployment done! Just double click on MyBatis Generator in Maven and you’ll get all you need.

11