IDEA經過MyBatis generator生成數據庫表的對象

  1.  File-->New-->Module-->Maven-->Next-->輸入GroupId、ArtifactId和Version-->Next-->輸入Module name、Content root和Module file location-->Finish,完成建立。java

  2. Run/Debug Configurations --> "+"--->Maven--->輸入Name、Working directory和Command line(mybatis-generator:generate)--->"OK"mysql

  3. 在resource目錄下編輯generatorConfig.xml文件以下:sql

<!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="C:\Users\xxx\.m2\repository\mysql\mysql-connector-java\5.1.22\mysql-connector-java-5.1.22.jar"/>
    <!--導入屬性配置 -->
    <context id="default" targetRuntime="MyBatis3">

        <property name="javaFileEncoding" value="UTF-8"/>

        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:8080/union?useUnicode=true&amp;characterEncoding=UTF-8" userId="root" password="root"
        />

        <!-- Model模型生成器,用來生成含有主鍵key的類,記錄類 以及查詢Example類 targetPackage 指定生成的model生成所在的包名 targetProject 指定在該項目下所在的路徑 -->
        <javaModelGenerator targetPackage="com.guwz.union.entity" targetProject="src/main/java">
            <!-- 是否對model添加 構造函數 -->
            <property name="enableSubPackages" value="true" />
            <!-- 給Model添加一個父類 -->
            <!--<property name="rootClass" value="com.itfsw.base"/>-->
        </javaModelGenerator>
        <!--Mapper映射文件生成所在的目錄 爲每個數據庫的表生成對應的SqlMap文件 -->
        <sqlMapGenerator targetPackage="mappings" targetProject="src/main/resources" />
        <!-- 客戶端代碼,生成易於使用的針對Model對象和XML配置文件 的代碼 type="ANNOTATEDMAPPER",生成Java Model 和基於註解的Mapper對象 type="MIXEDMAPPER",生成基於註解的Java Model 和相應的Mapper對象 type="XMLMAPPER",生成SQLMap XML文件和獨立的Mapper接口 -->
        <javaClientGenerator targetPackage="com.guwz.union.dao" targetProject="src/main/java" type="XMLMAPPER"/>


        <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自動生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
        <table tableName="table">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
   
    </context>
</generatorConfiguration>
View Code

  4. run generator配置便可數據庫

相關文章
相關標籤/搜索