推薦一款mybatis插件-反向生成mapper和實體類

添加maven依賴

pom.xml添加插件依賴


    <build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <configurationFile>src/main/resources/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>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.42</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

創建maven依賴中<configurationFile>對應的配置文件

<?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>  
    <!-- <classPathEntry location="E:\repo\mysql\mysql-connector-java\5.1.26\mysql-connector-java-5.1.26.jar" /> -->  
    <context id="generatorTables" targetRuntime="MyBatis3">
    <!-- sqlMapconfig 默認不生成 -->
<!--         <plugin type="org.mybatis.generator.plugins.MapperConfigPlugin">  
            <property name="fileName" value="sqlMapConfig.xml" />  
            <property name="targetPackage" value="/" />  
            <property name="targetProject" value="src/main/resources" />  
        </plugin>   -->
  
        <!-- 此處是將 Example 改名爲 Criteria -->  
        <plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin">  
            <property name="searchString" value="Example" />  
            <property name="replaceString" value="Criteria" />  
        </plugin>  
  
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />  
  
        <!-- commentGenerator 去除自動生成的註釋 -->  
        <!--<commentGenerator type="com.youli.prod.util.CustomCommentGenerator">-->
            <!--<property name="suppressAllComments" value="false" />-->
            <!--<property name="suppressDate" value="true" />  -->
        <!--</commentGenerator>-->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
            <property name="suppressDate" value="true" />
        </commentGenerator>
  
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"  
            connectionURL="jdbc:mysql://mysql_db_host:3306/ltn_product" userId="yluser"
            password="Yl#sit&amp;2018">
        </jdbcConnection> 
  
        <!-- 默認false,把JDBC DECIMAL 和 NUMERIC 類型解析爲 Integer true, 把 JDBC DECIMAL 和 NUMERIC 類型解析爲java.math.BigDecimal -->  
        <javaTypeResolver>  
            <property name="forceBigDecimals" value="false" />  
        </javaTypeResolver>  
  
        <!-- javaModelGenerator是模型的生成信息,這裏將指定這些Java model類的生成路徑; -->  
        <javaModelGenerator targetPackage="test"
            targetProject="src/main/java">  
            <property name="enableSubPackages" value="true" />  
            <property name="trimStrings" value="true" />  
        </javaModelGenerator>  
  
        <!-- sqlMapGenerator是mybatis 的sqlMapper XML文件的生成信息,包括生成路徑等; 先生成xml,在生成java -->  
        <sqlMapGenerator targetPackage="test"
            targetProject="src/main/java">  
            <property name="enableSubPackages" value="true" />  
        </sqlMapGenerator>  
  
        <!-- javaClientGenerator是應用接口的生成信息; -->  
        <javaClientGenerator type="XMLMAPPER"  
            targetPackage="test" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />  
        </javaClientGenerator>  
  
        <!-- table是用戶指定的被生成相關信息的表,它必須在指定的jdbc連接中已經被建立。 -->  
        <table tableName="yl_auction_goods" domainObjectName="AuctionGoods"
             enableCountByExample="false" enableUpdateByExample="false"
             enableDeleteByExample="false" enableSelectByExample="false"
             selectByExampleQueryId="false">
        </table>
  
    </context>  
</generatorConfiguration>   

雙擊執行插件
雙擊執行插件

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章