mybatis-generator的使用

背景:如何使用mybatis-generator插件自动生成数据库中表的pojo类,mapper接口,mapper文件

第一步:

在src/main/resources下面创建jdbc.properties文件,内容如下:

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://ip:port/database?serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8
username=***
password=***

第二步:

在src/main/resources下面创建generator/generatorConfig.xml文件

<?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="jdbc.properties"/>
    <context id="testTables" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <!--注释生成器-->
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
<!--            <property name="suppressAllComments" value="true" />-->
            <property name="suppressDate" value="true"/>
        </commentGenerator>
        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
        <jdbcConnection driverClass="${driverClassName}"
                        connectionURL="${url}" userId="${username}"
                        password="${password}">
        </jdbcConnection>
        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
           NUMERIC 类型解析为java.math.BigDecimal -->
<!--        <javaTypeResolver type=""></javaTypeResolver>-->
<!--        <javaTypeResolver>-->
<!--            <property name="forceBigDecimals" value="false" />-->
<!--        </javaTypeResolver>-->

        <!-- targetProject:生成PO类的位置 -->
        <javaModelGenerator targetPackage="com.****.entity"
                            targetProject="src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
<!--            <property name="enableSubPackages" value="false" />-->
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置
           如果maven工程只是单独的一个工程,targetProject="src/main/java"
           若果maven工程是分模块的工程,targetProject="所属模块的名称",例如:
           targetProject="ecps-manager-mapper",下同-->
        <sqlMapGenerator targetPackage="mybatis.mapper"
                         targetProject="src/main/resources">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!-- targetPackage:mapper接口生成的位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.****.mapper"
                             targetProject="src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <table tableName="batch" domainObjectName="Batch">
                <generatedKey column="id" sqlStatement="Mysql"></generatedKey>
        </table>
        <table tableName="batch_record" domainObjectName="BatchRecord">
            <generatedKey column="id" sqlStatement="Mysql"></generatedKey>
    </context>

第三步:

在pom.xml文件中增加mybatis-generator插件和mysql-connector-java依赖

<?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.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.***</groupId>
    <artifactId>****</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>benchmark</name>
    <description>benchmark</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.3</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.26</version>
<!--            <scope>runtime</scope>-->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <!--custom-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.12</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.75</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-text</artifactId>
            <version>1.6</version>
        </dependency>
        <dependency>
            <groupId>com.arronlong</groupId>
            <artifactId>httpclientutil</artifactId>
            <version>1.0.4</version>
        </dependency>
        <dependency>
            <groupId>io.esastack</groupId>
            <artifactId>httpclient-core</artifactId>
            <version>0.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.10</version>
        </dependency>
        <dependency>
            <groupId>jakarta.validation</groupId>
            <artifactId>jakarta.validation-api</artifactId>
            <version>2.0.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>



    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <descriptor>src/main/assembly/assembly.xml</descriptor>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.2.1</version>
                <configuration>
                    <attach>true</attach>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
            </plugin>

            			<plugin>
            				<groupId>org.mybatis.generator</groupId>
            				<artifactId>mybatis-generator-maven-plugin</artifactId>
            				<version>1.3.2</version>
            				<configuration>
            					<configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
            					<verbose>true</verbose>
            					<overwrite>true</overwrite>
            				</configuration>
            				<executions>
            					<execution>
            						<id>Generate MyBatis Artifacts</id>
            						<goals>
            							<goal>generate</goal>
            						</goals>
            					</execution>
            				</executions>
            				<dependencies>
            					<dependency>
            						<groupId>org.mybatis.generator</groupId>
            						<artifactId>mybatis-generator-core</artifactId>
            						<version>1.3.2</version>
            					</dependency>
                                <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
                                <dependency>
                                    <groupId>mysql</groupId>
                                    <artifactId>mysql-connector-java</artifactId>
                                    <version>8.0.26</version>
                                </dependency>
            				</dependencies>
            			</plugin>
        </plugins>
    </build>


</project>

  第四步:

点击maven刷新按钮,下载相关依赖和安装相关插件

mybatis-generator的使用

 

 

 第五步:

双击plugins插件列表里面的mybatis-generator:generate生成pojo类,mapper接口,mapper文件

mybatis-generator的使用

 

关于mybatis-generator配置文件的更详细的讲解参考:https://liuzh.blog.csdn.net/article/details/42102297

上一篇:matlab程序的lte waveform generator的学习


下一篇:使用mybaitsplus---Generator 提高开发效率