mybatis逆向工程生成相關文件,解決Oracle多個表空間相同表名生成bean錯誤問題

1.添加maven依賴。

<dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <!-- 添加oracle驅動依賴 -->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.1.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!--配置Mybatis反向代理的插件-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <!--配置文件的位置-->
                    <configurationFile>src/main/resources/mybatis/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
            <!-- 設置編譯源代碼JDK的版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

2.配置MySQL或者Oracle的驅動、連接、密碼等,文件命名爲 generatorinit.properties

jdbc_driver=oracle.jdbc.OracleDriver
jdbc_url=jdbc:oracle:thin:@192.168.100.51:1521:ORCL
jdbc_user=LM
jdbc_password=123456

3.配置mybatis逆向工程的一些配置,命名文件爲:mybatis-config.xml

<configuration>
    <typeAliases>
        <typeAlias alias="Integer" type="java.lang.Integer" />
        <typeAlias alias="Long" type="java.lang.Long" />
        <typeAlias alias="HashMap" type="java.util.HashMap" />
        <typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
        <typeAlias alias="ArrayList" type="java.util.ArrayList" />
        <typeAlias alias="LinkedList" type="java.util.LinkedList" />
    </typeAliases>
    <!--生成JavaBean爲駝峯命名方式-->
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>

4.添加逆向工程的生成器配置,命名爲:generatorConfig.xml

<!-- 配置生成器 -->
<generatorConfiguration>
    <!--執行generator插件生成文件的命令: call mvn mybatis-generator:generate -e -->
    <!-- 引入配置文件 -->
    <properties resource="mybatis/generatorinit.properties"/>
    <classPathEntry location="E:\maven_lib\com\oracle\ojdbc6\11.2.0.1.0\ojdbc6-11.2.0.1.0.jar"/>

    <!-- 一個數據庫一個context -->
    <!--defaultModelType="flat" 大數據字段,不分表 -->
    <context id="MysqlTables" targetRuntime="MyBatis3" defaultModelType="flat">
        <!-- 自動識別數據庫關鍵字,默認false,如果設置爲true,根據SqlReservedWords中定義的關鍵字列表; 一般保留默認值,遇到數據庫關鍵字(Java關鍵字),使用columnOverride覆蓋 -->
        <property name="autoDelimitKeywords" value="true" />
        <!-- 生成的Java文件的編碼 -->
        <property name="javaFileEncoding" value="utf-8" />
        <!-- beginningDelimiter和endingDelimiter:指明數據庫的用於標記數據庫對象名的符號,比如ORACLE就是雙引號,MYSQL默認是`反引號; -->
        <property name="beginningDelimiter" value="&quot;" />
        <property name="endingDelimiter" value="&quot;" />

        <!-- 格式化java代碼 -->
        <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
        <!-- 格式化XML代碼 -->
        <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />

        <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />

        <!-- 註釋 -->
        <commentGenerator >
            <property name="suppressAllComments" value="true"/><!-- 是否取消註釋 -->
            <property name="suppressDate" value="true" /> <!-- 是否生成註釋代時間戳-->
        </commentGenerator>
        <!-- jdbc連接 -->
        <jdbcConnection driverClass="${jdbc_driver}" connectionURL="${jdbc_url}" userId="${jdbc_user}" password="${jdbc_password}" />
        <!-- 類型轉換 -->
        <javaTypeResolver>
            <!-- 是否使用bigDecimal, false可自動轉化以下類型(Long, Integer, Short, etc.) -->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 生成模型的包名和位置 -->
        <javaModelGenerator targetPackage="com.test.model" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 生成映射文件的包名和位置 -->
        <sqlMapGenerator targetPackage="com.test.mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.test.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- table可以有多個,每個數據庫中的表都可以寫一個table,tableName表示要匹配的數據庫表,
        也可以在tableName屬性中通過使用%通配符來匹配所有數據庫表,只有匹配的表纔會自動生成文件 -->
        <table tableName="表名" domainObjectName="" schema="Oracle登錄名" catalog="當前表空間"
               enableCountByExample="false"
               enableUpdateByExample="true"
               enableDeleteByExample="true"
               enableSelectByExample="true"
               selectByExampleQueryId="true">
            <property name="useActualColumnNames" value="false" />
            <property name="ignoreQualifiersAtRuntime" value="true" />
        </table>
    </context>
</generatorConfiguration>

因爲我使用的是Oracle數據庫,所有在上面table的配置中添加了Oracle的登錄名以及表空間配置,使用該配置是爲了防止Oracle在不用表空間使用相同表名。

使用MySQL數據庫時scheme以及catalog可以不用填寫。

5.在idea的plugins中尋找該插件雙擊即可

 

假如在生成時報如下錯誤:

Table configuration with catalog USERS, schema lm, and table PERSON did not resolve to any tables

請查看Oracle的表名,必須是大寫,才能生成。

 

碼雲demo地址

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