SpringBoot graphic tutorial 8 - SpringBoot integrated MBG "code generator"

If there is the concept of flying in the sky, there must be the realization of landing

  • Ten times of concept is not as good as one time of code. Friend, I hope you can type all the code cases in this article

  • Praise before you see, form a habit

Spring boot text tutorial series article directory

  1. Spring boot picture and text tutorial 1 "concept + case mind map" and "basic chapter I"
  2. Spring boot graphic tutorial 2 - use of log "logback" and "log4j"
  3. Spring boot graphic tutorial 3 - "first love complex" integration
  4. Spring boot picture and text tutorial 4 - spring boot implementation file upload and download
  5. Spring boot graphic tutorial 5 - using Aop in spring boot
  6. Spring boot picture and text tutorial 6 - use of filters in spring boot
  7. Spring boot graphic tutorial 7 - the usage posture of spring boot interceptor


When using Mybatis for project development, the most tedious thing is to write entity class, dao interface and mapper.xml file. Almost every table needs to write a corresponding set, and most of the workload is in the most basic addition, deletion, modification and query. If the fields in the table are modified, the entity class, mapper file and even dao interface will be modified.

The world has been struggling with mapper files for a long time, so the official recommendation of Mybatis is a Mybatis code generator (MBG) to save people in the water and fire.

Mybatis code generator


MBG, the full name of MyBatis Generator, can be used to generate Mybatis development related code, including basic add, delete, modify and query entity classes, dao interfaces and mapper files. And the MBG tool supports all versions of Mybatis.

Official document address: http://mybatis.org/generator/

SpringBoot integrated MBG

The code of this article will be implemented in an empty project of SpringBoot+Mybatis. Please go to Git warehouse to download if necessary: https://gitee.com/bingqilinpeishenme/Java-Tutorials

1. Import dependency

To use MBG in spring boot, you need to import the startup plug-ins of MBG while importing MBG dependencies. MBG dependence

<!--MybatisGenerator Dependence jar package-->

Start plug-in of MBG

<!--MybatisGenerator Start plug-in for-->
				<!--jar Package to generate corresponding class needs to connect to database data connection version consistent with that in project-->
					<!--MBG Path to profile -->

Be careful:

  1. The MySQL connector java version in the plug-in is the same as the version number in the project
  2. configurationFile configures the address of MBG configuration file src/main/resources

2. Import and write MBG configuration file

After importing dependencies, you need to import the configuration file generatorConfig.xml of MBG in the SpringBoot resources directory

Here are all the contents and comments of the configuration file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
    <context id="test" targetRuntime="MyBatis3">
        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>  
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin> 
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>

            <!-- This element is used to remove the generated date from the specified generated comment false:Representation includes -->
            <!-- If a date is generated, even if a field is modified, all properties of the entire entity class will change, which is not conducive to version control. Therefore, it is set to true -->
            <property name="suppressDate" value="true" />
            <!-- Remove automatically generated comments or not true: Yes, false:no -->
            <property name="suppressAllComments" value="true" />


        <!--Database links URL,User name, password -->

            <property name="forceBigDecimals" value="false" />

        <!-- Package name and location of the generated entity class -->
        <javaModelGenerator targetPackage="com.lu.entity"
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />

        <!--Package name and location of the build mapping file  com/lu/mapper-->
        <sqlMapGenerator targetPackage="mapper"
            <property name="enableSubPackages" value="true" />

        <!-- generate DAO Package name and location for mybatis Two development models xml Annotation-->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.lu.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        <!-- Which tables to generate -->
        <table tableName="user" domainObjectName="User"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"



Key configuration!!!

In the configuration file, users need to configure according to their own conditions

  1. Modify database connection parameters to their own database connection parameters

  2. Configuration of location package structure for generating mapper file of dao interface entity class

Note: the generation location and package structure parameters are modified according to the actual situation. Other contents do not need to be modified

  1. Configure the entity class dao interface and mapper file corresponding to which tables to generate

Note: in addition to the above three configurations, other configurations can be modified without modification

3. Code can be generated by plug-in startup

Run the plug-in and generate the code as follows:

  • Entity class

  • dao interface

  • mapper file


Tips: the example code project address of this article is: https://gitee.com/bingqilinpeishenme/Java-Tutorials

Congratulations on the completion of this chapter, applaud for you! If this article is helpful to you, please like it, comment and forward it. It's very important for the author. Thank you.

Let's review the learning objectives of this article again

  • Master the use of MBG in SpringBoot

To learn more about SpringBoot, stay tuned for this series of tutorials.

For attention, for approval, for forwarding

Welcome to my official account: Mr. Lu's Java notes will be updated in Java technology tutorials and video tutorials, Java learning experience, Java interview experience and Java practical development experience.

Welcome to my official account: Mr. Lu's Java notes will be updated in Java technology tutorials and video tutorials, Java learning experience, Java interview experience and Java practical development experience.

Keywords: Programming Mybatis Java Spring xml

Added by nonexistence on Sun, 01 Mar 2020 14:22:18 +0200