Spring boot (VII) mybatis code automatic generation and auxiliary plug-ins

1、 Introduction

1.1 introduction to mybatis generator

Mybatis generator is an official product of mybatis. It is used to automatically generate the framework of mapper, Dao and entity of mybatis, so that we can save the most regular part and the most basic code writing.

1.2 use of mybatis generator

There are four ways to use mybatis generator:

Maven method is recommended for code generation, because integration and use are relatively simple.

1.3 development environment

MysqL:8.0. twelve

MyBatis Generator:1.3. seven

Maven:4.0

IDEA:2018.2

2、 Automatic code generation configuration

The above describes several ways to use mybatis generator, of which Maven is the most recommended. Therefore, let's take a look at the mybatis code generation in maven, which is divided into four steps:

Step 1: add dependency

Configure POM XML file, add the path of dependency and configuration generation file ("generatorconfig. XML"):

<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.7</version>
    <dependencies>
        <dependency>
            <groupId>MysqL</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.12</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.7</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <id>Generate MyBatis Artifacts</id>
            <phase>package</phase>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <!--允许移动生成的文件 -->
        <verbose>true</verbose>
        <!-- 是否覆盖 -->
        <overwrite>true</overwrite>
        <!-- 自动生成的配置 -->
        <configurationFile>generatorConfig.xml</configurationFile>
    </configuration>
</plugin>

Step 2: add profile

According to the above configuration in POM, we need to add generatorconfig XML in the root directory of the project:

<?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>
    <!--加载配置文件,为下面读取数据库信息准备-->
    <properties resource="application.properties"/>

    <!--defaultModelType="flat" 大数据字段,不分表 -->
    <context id="MysqL" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <property name="autoDelimitKeywords" value="true" />
        <property name="beginningDelimiter" value="`" />
        <property name="endingDelimiter" value="`" />
        <property name="javaFileEncoding" value="utf-8" />
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />

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

        <!-- 注释 -->
        <commentGenerator >
            <property name="suppressAllComments" value="true"/><!-- 是否取消注释 -->
            <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳-->
        </commentGenerator>
        
        <!--数据库链接地址账号密码-->
        <jdbcConnection driverClass="${spring.datasource.driver-class-name}"
                        connectionURL="${spring.datasource.url}"
                        userId="${spring.datasource.username}"
                        password="${spring.datasource.password}">
        </jdbcConnection>
        
        <!-- 类型转换 -->
        <javaTypeResolver>
            <!-- 是否使用bigDecimal, false可自动转化以下类型(Long,Integer,Short,etc.) -->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!--生成Model类存放位置-->
        <javaModelGenerator targetPackage="com.hello.springboot.entity" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- 生成mapxml文件 -->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources/mybatis" >
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>

        <!-- 生成mapxml对应client,也就是接口dao -->
        <javaClientGenerator targetPackage="com.hello.springboot.dao" targetProject="src/main/java" type="XMLMAPPER" >
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <table tableName="article" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">
            <generatedKey column="id" sqlStatement="MysqL" identity="true" />
        </table>

        <table tableName="user_log" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">
            <generatedKey column="id" sqlStatement="MysqL" identity="true" />
        </table>

    </context>
</generatorConfiguration>

The configuration of database connection is from application Properties are read directly.

Step 3: configure the global properties file

Global properties file application The configuration of properties is the same as that of adding mybatis to spring boot. If mybatis support has been configured in your spring boot project, please ignore this step.

# MyBatis 配置
spring.datasource.url=jdbc:MysqL://172.16.10.79:3306/mytestdb?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.MysqL.cj.jdbc.Driver
mybatis.type-aliases-package=com.hello.springboot.mapper
mybatis.config-locations=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

Note: the JDBC configuration is different after MySQL 6. Refer to the configuration of MySQL 8 above.

Step 4: click Maven to generate code

If you are using idea, click Maven projects on the far right = > Click mybatis generator = > right click mybatis generator: generate = > Run Maven build, as shown in the following figure:

The normal console outputs "build success" to indicate that the generation has been successful. If an error occurs, eliminate the processing error according to the error prompt.

Mybatis generator sample source code: https://github.com/vipstone/springboot-example/tree/master/springboot-mybatis-xml

3、 Install the idea plug-in

If you are using idea, it is strongly recommended that you install a free idea plug-in "free mybatis plugin", which can quickly map Dao to the corresponding method of mapper XML. Click any one to quickly adjust to the corresponding method to improve work efficiency. The effect is shown in the figure below:

Click the green arrow to directly jump to the corresponding method of mapper XML, as shown in the following figure:

You can click each other for corresponding jump.

Installation steps

Screenshots of key steps are as follows:

4、 Summary

The use of mybatis generator can help us automatically generate entity classes and five most basic methods, which greatly improves our work efficiency. Users only need to write their own unique businesses on demand. At the same time, adding the "free mybatis plugin" plug-in can easily help us develop and debug code, which is a real benefit.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>