mybatis generator maven-plugin 集成

Mybatis generator 工具使用

Mybatis 相對於 ibatis 多了很多功能,幫助我們在項目更加敏捷的開發程序,其中的Mybatis generator 代碼生成功能,解放了很多重複代碼的編寫 。今天學習一下這個插件的使用。


Maven插件方式

mybatis-generator-maven-plugin
該工具支持集成Maven,以Maven插件的方式運行,附上Maven插件的下載地址:

 <project ...>
     ...
     <build>
       ...
       <plugins>
        ...
        <plugin>
          <groupId>org.mybatis.generator</groupId>
          <artifactId>mybatis-generator-maven-plugin</artifactId>
          <version>1.3.0</version>
            <configuration>
                    <!--允許移動生成的文件-->
                    <verbose>true</verbose>
                    <!--允許覆蓋生成的文件-->
                    <overwrite>true</overwrite>
           </configuration>
        </plugin>
        ...
      </plugins>
      ...
    </build>
    ...
  </project>

添加至pom.xml 文件後,等待Maven插件安裝完成後,我們開始編輯插件的配置文件 generatorConfig.xml 。
注意!名字千萬不能錯,否則程序找不到配置文件。 配置文件放到Maven資源包下(resources)。

不論使用該工具的方式,配置文件都是通用的!

generatorConfig.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>
    <!-- 驅動包路徑! -->
    <classPathEntry location="D:\Repository\mysql\mysql-connector-java\5.1.38\mysql-connector-java-5.1.38.jar"/>
    <context id="MysqlTables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!-- jdbc url地址 ! -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/yourDatabase?generateSimpleParameterMetadata=true" userId="root" password="root">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- targetPackage 生成後的路徑 ,targetProject 絕對路徑  該配置生成java代碼  -->
        <javaModelGenerator targetPackage="com.xzxin.system.dao" targetProject="D:\eclipse\sourceCode\xzxin_server\src\main\java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
          <!-- 該配置生成 mybatis 映射文件   -->
        <sqlMapGenerator targetPackage="com.xzxin.system.dao" targetProject="D:\eclipse\sourceCode\xzxin_server\src\main\resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 生成 java 接口代碼  -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.xzxin.system.dao" targetProject="D:\eclipse\sourceCode\xzxin_server\src\main\java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--  表配置    tableName 表名      domainObjectName java類名,首字母必須大寫,否則報字符串越界錯誤 -->
        <table schema="xzxin" tableName="userinfo" domainObjectName="Userinfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
            <property name="useActualColumnNames" value="true"/>
        </table>

    </context>


</generatorConfiguration> 

生成代碼

如果是在eclipse 中,選擇pom.xml文件,擊右鍵先擇Run AS——>Maven Build… ——>在Goals框中輸入:mybatis-generator:generate

如果在命令行輸入Maven命令即可,注意:一定是當前項目目錄下運行該命令:

mvn mybatis-generator:generate
這裏寫圖片描述

代碼生成完,大功告別成! 刷新工程,能找到代碼代表成功!

如果使用 IDEA , 雙擊一下插件就可以了 。
這裏寫圖片描述

執行沒有錯誤,就OK 了
這裏寫圖片描述

普通java工程生成也很簡單,配置文件和文章中的沒有什麼分別,只是需用通過執行jar包的方式即可。
這個哥們已經寫的比較詳細了,可以參考一下 http://www.cnblogs.com/smileberry/p/4145872.html

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