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

Preface

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

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-->
<dependency>
			<groupId>org.mybatis.generator</groupId>
			<artifactId>mybatis-generator-core</artifactId>
			<version>1.3.2</version>
		</dependency>

Start plug-in of MBG

<!--MybatisGenerator Start plug-in for-->
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.2</version>
				<!--jar Package to generate corresponding class needs to connect to database data connection version consistent with that in project-->
				<dependencies>
					<dependency>
						<groupId>mysql</groupId>
						<artifactId>mysql-connector-java</artifactId>
						<version>5.1.38</version>
					</dependency>
				</dependencies>
				
				<configuration>
					<!--MBG Path to profile -->
					<configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
					<overwrite>true</overwrite>
				</configuration>
			</plugin>

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"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <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>

        <commentGenerator>
            <!-- 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" />

        </commentGenerator>

        <!--Database links URL,User name, password -->
        <jdbcConnection
                driverClass="com.mysql.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost/demo"
                userId="root"
                password="123456">
        </jdbcConnection>

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

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

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

        <!-- 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" />
        </javaClientGenerator>
        
        <!-- Which tables to generate -->
        <table tableName="user" domainObjectName="User"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>

     

    </context>
</generatorConfiguration>

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

summary

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