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

The concept of flying in the sky, it is necessary to achieve a landing

  • The concept ten times as good as the code again, and friends, I hope you put all the text code cases are knocking again

  • Like after the first look, habit

SpringBoot graphic tutorial series directory

  1. SpringBoot graphic tutorial 1 "concept, Case mind map" "on the Basics."
  2. Use SpringBoot graphic tutorial 2- log "logback" "log4j"
  3. SpringBoot graphic Lesson 3 '' first love 'complex' integrated Jsp
  4. SpringBoot graphic tutorial 4-SpringBoot for file uploading and downloading
  5. SpringBoot graphic tutorial using 5-SpringBoot Aop
  6. Use SpringBoot graphic tutorial 6-SpringBoot of the filter
  7. SpringBoot graphic tutorial 7-SpringBoot interceptor use gesture that has

Foreword

When using Mybatis project development, the most complicated thing is to write the entity class, dao interfaces, mapper.xml files, almost every table needs a written correspondence, and most of the increase in workload are essential check the deletion. If the fields in the table has been modified, then the entity class, mapper file even dao interfaces must be modified.

The world is suffering mapper file for some time now, so Mybatis official recommended a Mybatis code generator (MBG) to the country and people veering.

Mybatis code generator

MBG

Full name MBG MyBatis Generator, may be used to generate the code associated Mybatis development, including basic CRUD entity classes, interfaces and mapper DAO file. And MBG tool supports all versions of Mybatis.

Official documents Address: http://mybatis.org/generator/

SpringBoot integrated MBG

In this paper, the code will be a SpringBoot + empty project Mybatis in, if necessary, please go Git repository to download: https://gitee.com/bingqilinpeishenme/Java-Tutorials

1. Import dependence

SpringBoot used MBG need to import start the plug while introducing the MBG MBG dependent.
MBG dependence

<!--MybatisGenerator的依赖jar包-->
<dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.2</version>
        </dependency>

MBG's start the plug

<!--MybatisGenerator的启动插件-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <!--jar包去生成对应类 需要连接数据库 数据连接的版本和项目中的一致-->
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.38</version>
                    </dependency>
                </dependencies>
                
                <configuration>
                    <!--MBG配置文件的路径 -->
                    <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>

note:

  1. Consistent with the version number of the plug-in mysql-connector-java version and projects
  2. configurationFile configuration is the address of the configuration file src MBG / main / resources

2. Import and write MBG profile

After you import dependence, need to import MBG profile generatorConfig.xml in SpringBoot resources directory

Here are all the content and profile comments

<?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>
            <!-- 这个元素用来去除指定生成的注释中是否包含生成的日期 false:表示包含 -->
            <!-- 如果生成日期,会造成即使修改一个字段,整个实体类所有属性都会发生变化,不利于版本控制,所以设置为true -->
            <property name="suppressDate" value="true" />
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />

        </commentGenerator>

        <!--数据库链接URL,用户名、密码 -->
        <jdbcConnection
                driverClass="com.mysql.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost/demo"
                userId="root"
                password="123456">
        </jdbcConnection>

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

        <!-- 生成实体类的包名和位置 -->
        <javaModelGenerator targetPackage="com.lu.entity"
            targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!--生成映射文件的包名和位置  com/lu/mapper-->
        <sqlMapGenerator targetPackage="mapper"
            targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- 生成DAO的包名和位置 mybatis两种开发模式 xml 注解式-->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.lu.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        
        <!-- 要生成哪些表 -->
        <table tableName="user" domainObjectName="User"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>

     

    </context>
</generatorConfiguration>

Key configuration! ! !

There are some configuration requires the user to be configured according to their own situation in the configuration file

  1. Modify the database connection parameters for the database connection parameters of their own

  2. dao interface entity mapper class file generation packet structure arrangement position

    Note: generating a packet structure and location parameters can be modified according to their actual situation, no need to modify the content of other

  1. Configuration table corresponds to which entity classes and interfaces dao mapper to generate a document

Note: In addition to these three configurations, other configurations may need to modify

3. The plug boot may be generated by the code

Run plugins, to generate the following code:

  • Entity class

  • dao Interface

  • mapper file

to sum up

Tips: This article sample code project address: https://gitee.com/bingqilinpeishenme/Java-Tutorials

Congratulations, you have completed this chapter, and applaud you! If this article help you, please help thumbs up, comment, forwarding, which the author is very important, thank you.

Let's review the learning objectives of this article again

  • Mastering SpringBoot in the MBG

To grasp SpringBoot more usage, please stay tuned to this series of tutorials.

Seeking attention, seeking thumbs up, seeking forwards

I welcome public concern number: deer teacher's notes Java, Java technology will be a long-term update graphic tutorials and video tutorials, Java learning experience, Java interview experience and practical development experience in Java.

I welcome public concern number: deer teacher's notes Java, Java technology will be a long-term update graphic tutorials and video tutorials, Java learning experience, Java interview experience and practical development experience in Java.

Guess you like

Origin www.cnblogs.com/bingyang-py/p/12391544.html