mybatis-generator-maven-plugin(MyBatis 逆向工程 插件)

管玉堂
2023-12-01

这里介绍一种与eclipse ibator 逆向工程插件功能类似的maven插件,即 mybatis-generator-maven-plugin,它可逆向快速生成基础代码,提高开发效率。eclipse ibator插件

  1. maven 父工程dependency版本控制
    <properties>
        <mysql-connector-java.version>5.1.41</mysql-connector-java.version>
        <mybatis-spring-boot-starter.version>1.3.2</mybatis-spring-boot-starter.version>
        <mybatis-generator-maven-plugin.version>1.3.5</mybatis-generator-maven-plugin.version>
        <druid-starter.version>1.1.6</druid-starter.version>
    </properties>
    <dependencyManagement>
        <dependency>
    	<groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
    	<version>${mysql-connector-java.version}</version>
        </dependency>
        <dependency>
    	<groupId>org.mybatis.spring.boot</groupId>
    	<artifactId>mybatis-spring-boot-starter</artifactId>
    	<version>${mybatis-spring-boot-starter.version}</version>
        </dependency>
        <dependency>
    	<groupId>org.mybatis.generator</groupId>
    	<artifactId>mybatis-generator-maven-plugin</artifactId>
    	<version>${mybatis-generator-maven-plugin.version}</version>
        </dependency>
        <dependency>
    	<groupId>com.alibaba</groupId>
    	<artifactId>druid-spring-boot-starter</artifactId>
    	<version>${druid-starter.version}</version>
        </dependency>
    </dependencyManagement>

     

  2. 子工程引用maven插件
    <build>
        <plugins>
            <!-- MyBatis 逆向工程 插件 -->
    	<plugin>
    	    <groupId>org.mybatis.generator</groupId>
    	    <artifactId>mybatis-generator-maven-plugin</artifactId>
    	    <dependencies>
    		<dependency>
    		    <groupId>mysql</groupId>
    		    <artifactId>mysql-connector-java</artifactId>
    		    <version>${mysql-connector-java.version}</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>
    		    src/main/resources/config/generator-config.xml
    		</configurationFile>
    	    </configuration>
    	</plugin>
        </plugins>
    </build>

     

  3. classpath:resources下config文件夹中创建generator-config.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="config/application.properties"/>
        <!-- mysql驱动的位置 这个是MySQL连接的jar包,你需要指定你自己计算机上的jar包的位置-->
        <classPathEntry location="D:\apache-maven-3.3.3\repo\mysql\mysql-connector-                        java\5.1.41\mysql-connector-java-5.1.41.jar" />
    
        <context id="Tables" targetRuntime="MyBatis3">
            <!-- 是否生成注释 -->
            <commentGenerator>
                <!-- 是否生成注释代时间戳 -->
                <property name="suppressDate" value="true"/>
                <!-- 是否去除自动生成的注释 true:是 : false:否 -->
                <property name="suppressAllComments" value="true"/>
            </commentGenerator>
    
            <!-- JDBC连接 其中connectionURL后面的newtest改为你创建的数据库,紧跟在后面是数据库连接的账户和密码-->
            <jdbcConnection
                    driverClass="${spring.datasource.driver-class-name}"
                    connectionURL="${spring.datasource.url}"
                    userId="${spring.datasource.username}"
                    password="${spring.datasource.password}">
            </jdbcConnection>
    
            <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
            <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
             NUMERIC 类型解析为java.math.BigDecimal -->
            <javaTypeResolver>
                <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
                <property name="forceBigDecimals" value="false" />
                <!-- 从数据库返回的值被清理前后的空格 -->
                <property name="trimStrings" value="true"/>
            </javaTypeResolver>
    
            <!-- 生成实体类地址 这里需要你改动,其中targetPackage需要根据你自己创建的目录进行改动 -->
            <javaModelGenerator targetPackage="${generator.javaModel-targetPackage}" targetProject="${generator.targetProject}">
                <!-- 从数据库返回的值被清理前后的空格 -->
                <property name="trimStrings" value="true" />
                <!-- enableSubPackages:是否让schema作为包的后缀 -->
                <property name="enableSubPackages" value="false" />
            </javaModelGenerator>
    
            <!-- 生成mapper xml文件 这里不需要改动 -->
            <sqlMapGenerator targetPackage="${generator.sqlMap-targetPackage}"  targetProject="${generator.targetProject}">
                <!-- enableSubPackages:是否让schema作为包的后缀 -->
                <property name="enableSubPackages" value="false" />
            </sqlMapGenerator>
    
            <!-- 生成mapper xml对应Client   这里需要改动targetPackage,依据你自己的工程-->
            <javaClientGenerator targetPackage="${generator.mappers}" targetProject="${generator.targetProject}" type="XMLMAPPER">
                <!-- enableSubPackages:是否让schema作为包的后缀 -->
                <property name="enableSubPackages" value="false" />
            </javaClientGenerator>
    
            <!-- 配置表信息 -->
            <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample
                是否生成 example类 -->
            <table schema="manage_center_menus" tableName="manage_center_menus" domainObjectName="ManageCenterMenus" enableCountByExample="true"
                   enableDeleteByExample="true" enableSelectByExample="true" enableUpdateByExample="true">
                <generatedKey column="id" sqlStatement="MySql" identity="true"></generatedKey>
            </table>
        </context>
    </generatorConfiguration>

     

  4. classpath:resources下config文件夹中创建application.properties
    #mysql配置
    spring.datasource.url=jdbc:mysql://****:3306/basic_server?useUnicode=true&characterEncoding=utf-8&useSSL=false&rewriteBatchedStatements=true
    spring.datasource.username=root
    spring.datasource.password=****
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
    
    #alibaba DruidDataSource 配置
    spring.datasource.druid.initialSize=5
    spring.datasource.druid.minIdle=5
    spring.datasource.druid.maxActive=30
    # 配置获取连接等待超时的时间
    spring.datasource.maxWait=60000
    #Druid监控配置
    spring.datasource.druid.WebStatFilter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
    spring.datasource.druid.StatViewServlet.loginUsername=druid
    spring.datasource.druid.StatViewServlet.loginPassword=druid
    
    #mybatis配置
    mapper.mappers=com.spring_cloud.base_server.repository
    
    # Generator配置
    generator.targetProject=src/main/java
    #生成接口位置
    generator.mappers=com.spring_cloud.base_server.repository
    #生成pojo位置
    generator.javaModel-targetPackage=com.spring_cloud.base_server.bean
    #生成mapper位置
    generator.sqlMap-targetPackage=com.spring_cloud.base_server.repository

     

  5. maven install即可生成pojo、pojoExample、pojoMapper接口(缺少@mapper注释,手动添加)、pojoMapper.xml,Ipojoservice与pojoserverImpl手动添加,到这里就结束了。

 类似资料: