mybatis-generator

mybatis 自動生成工具java

  最近在工做中使用到了mybatis,對於數據庫的javabean,orm映射配置文件,手動編寫有點繁瑣,同時又容易出錯,這裏就介紹一下mybatis-generator反向生成工具。mysql

1)所需文件:sql

  generatorConfig.xml數據庫

  mybatis-generator-core-1.3.4.jarapi

  mysql-connector-java-5.1.28.jarmybatis

2)修改配置文件app

<?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="mysql-connector-java-5.1.28.jar"/>
    <context id="DB2Tables"    targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--數據庫連接地址帳號密碼-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://10.107.134.26:8306/niux" userId="nuomi" password="nuomisc">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--生成Model類存放位置-->
        <javaModelGenerator targetPackage="com.baidu.api.gateway.model" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--生成映射文件存放位置-->
        <sqlMapGenerator targetPackage="com.baidu.api.gateway.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--生成Dao類存放位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.baidu.api.gateway.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--生成對應表及類名-->
        <table tableName="gw_service_param_mapping" domainObjectName="GWServiceParamMapping" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

 

 3)在該目錄下,執行以下命令:dom

java -jar mybatis-generator-core-1.3.4.jar -configfile generatorConfig.xml -overwrite

 

注意:若是使用該工具修改配置文件,xml不會覆蓋,只會追加,而java代碼會徹底覆蓋。 工具

相關文章
相關標籤/搜索