Universal mapper plug-in code generator usage


1. Add dependencies

Add a general mapper dependency in the project's pom.xml to replace the mybatis dependency.

<!--        <dependency>-->
<!--            <groupId>org.mybatis.spring.boot</groupId>-->
<!--            <artifactId>mybatis-spring-boot-starter</artifactId>-->
<!--            <version>2.1.4</version>-->
<!--        </dependency>-->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper</artifactId>
            <version>4.1.5</version>
        </dependency>

Add other code generator dependencies to the build


            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
                	<!--生成器配置文件地址-->
                	<!--${basedir}指项目根目录 -->
                    <configurationFile>
                        ${basedir}/src/main/resources/generator/generatorConfig.xml
                    </configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <!--这边用的是mysql,要对应mysql驱动-->
                        <version>8.0.23</version>
                    </dependency>
                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>4.1.5</version>
                    </dependency>
                </dependencies>
            </plugin>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.allone8</groupId>
    <artifactId>user-center</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>user-center</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>



    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.mybatis.spring.boot</groupId>-->
<!--            <artifactId>mybatis-spring-boot-starter</artifactId>-->
<!--            <version>2.1.4</version>-->
<!--        </dependency>-->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper</artifactId>
            <!-- 建议使用最新版本,最新版本请从项目首页查找 -->
            <version>4.1.5</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>



    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
                    <configurationFile>
                        ${basedir}/src/main/resources/generator/generatorConfig.xml
                    </configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.23</version>
                    </dependency>
                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>4.1.5</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

</project>

2. Write the generator configuration file

Insert image description here
According to the address filled in the pom.xml above, create a file in the corresponding directory and write the generator configuration generatorConfig.xml.

generatorConfig.xml

<!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="generator/config.properties"/>

    <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
            <property name="caseSensitive" value="true"/>
            <property name="lombok" value="Getter,Setter,ToString"/>
        </plugin>

        <jdbcConnection driverClass="${jdbc.driverClass}"
                        connectionURL="${jdbc.url}"
                        userId="${jdbc.user}"
                        password="${jdbc.password}">
        </jdbcConnection>

        <!--生成实体-->
        <javaModelGenerator targetPackage="com.allone8.usercenter.domain.entity.${moduleName}"
                            targetProject="src/main/java"/>

        <!--生成mapper.xml-->
        <sqlMapGenerator targetPackage="mapper.${moduleName}"
                         targetProject="src/main/resources"/>

        <!--生成mapper接口-->
        <javaClientGenerator targetPackage="com.allone8.usercenter.dao.${moduleName}"
                             targetProject="src/main/java"
                             type="XMLMAPPER"/>

        <!--为哪张表生成代码-->
        <table tableName="${tableName}">
            <generatedKey column="id" sqlStatement="JDBC"/>
        </table>

    </context>
</generatorConfiguration>

<properties resource="generator/config.properties"/>${xxx}Represents the source of what this form of placeholder reads appears in the file below .
Insert image description here


Below are the fields corresponding to the generated code, targetPackageindicating the name to be generated. targetProjectRepresents the generated path.

        <!--生成实体-->
        <javaModelGenerator targetPackage="com.allone8.usercenter.domain.entity.${moduleName}"
                            targetProject="src/main/java"/>

        <!--生成mapper.xml-->
        <sqlMapGenerator targetPackage="mapper.${moduleName}"
                         targetProject="src/main/resources"/>

        <!--生成mapper接口-->
        <javaClientGenerator targetPackage="com.allone8.usercenter.dao.${moduleName}"
                             targetProject="src/main/java"
                             type="XMLMAPPER"/>

        <!--为哪张表生成代码-->
        <table tableName="${tableName}">
            <generatedKey column="id" sqlStatement="JDBC"/>
        </table>

config.properties

jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3308/user_center?nullCatalogMeansCurrent=true
jdbc.user=root
jdbc.password=123456

# java模块名称
moduleName=user
# 数据库表名
tableName=user

The jdbc path here must be added at the end nullCatalogMeansCurrent=true, otherwise the generated code may not be correct.
moduleNameThe parameters here can be written according to your own requirements.
tableNameThe parameters here must be consistent with the table names in the database.
Insert image description here

3. Use

  1. After the above code generator is configured, find maven on the right side of the idea.
    Insert image description here

  2. Find the project in mavenplugins
    Insert image description here

  3. turn upmybatis-generator
    Insert image description here

  4. Select it mybatis-generator:generateand double-click.
    Insert image description here

  5. The following picture appears indicating that the code is generated successfully. At this point the project code is automatically generated.
    Insert image description here

Guess you like

Origin blog.csdn.net/u013611126/article/details/115481031