java mybatis插件生成代码

2023-03-09  本文已影响0人  放鸽子_高手

成果

成果

步骤:

1.生成数据库表

库结构

2.maven项目中配置:

a.src/resources目录下新增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>
    <!-- mysql-connector-java 版本需要与pom中引得版本一致 -->
    <classPathEntry location="/Users/hedonghui/Documents/17usoft/repository/mysql/mysql-connector-java/8.0.28/mysql-connector-java-8.0.28.jar" />

    <context id="DB2Tables" targetRuntime="MyBatis3">

        <!-- 不加注释,因为默认注释是英文的 -->
        <commentGenerator>
            <property name="addRemarkComments" value="true"/>
            <property name="suppressDate" value="true"/>
        </commentGenerator>

        <!-- 数据库连接信息 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://ip:端口/tablename?characterEncoding=utf-8&amp;useSSL=false&amp;serverTimezone=UTC"
                        userId="username"
                        password="password">
        </jdbcConnection>

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

        <!-- targetPackage 文件生成在指定包下,targetProject 路径-->
        <javaModelGenerator targetPackage="com.ly.flight.mock.entity" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="mappers"  targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER" targetPackage="com.ly.flight.mock.dao"  targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- domainObjectName 即生成后entity的名字 -->
        <table tableName = "tablename1" domainObjectName="tablename1" />
        <table tableName = "tablename2" domainObjectName="tablename2" />
        <table tableName = "tablename3" domainObjectName="tablename3" />
        <table tableName = "tablename4" domainObjectName="tablename4" />
        <table tableName = "tablename5" domainObjectName="tablename5" />
        <table tableName = "tablename6" domainObjectName="tablename6" />

    </context>
</generatorConfiguration>

b.创建对应的targetPackage包

代码结构

3.在pom.xml中引入jar包

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>
        <dependency>
              <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.28</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <overwrite>true</overwrite>
                    <configurationFile>真实路径/generatorConfig.xml</configurationFile>
                </configuration>
            </plugin>

4.运行插件生成代码

插件入口
上一篇下一篇

猜你喜欢

热点阅读