天天看點

mybatis-generator 代碼自動生成工具

今天來介紹下怎麼用mybatis-gennerator插件自動生成mybatis所需要的dao、bean、mapper xml檔案,這樣我們可以節省一部分精力,把精力放在業務邏輯上。

之前看過很多文章,都是借助eclipse或者IDEA等其他工具,今天講的這個方法是,直接在指令提示符下運作。

程式打包上傳:http://download.csdn.net/detail/tolcf/9456659

1、在D盤建立一個檔案夾,命名:generator(或者其他盤其他名字也可以,之是以用這個,是為了copy下面代碼後,不用再做修改路徑)

2、準備需要的jar包:mybatis-generator-core-1.3.2.jar、mysql-connector-java-5.1.34.jar(忽略版本号,這隻是我用的jar 版本)

3、建立一個檔案,命名:generator.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:\generator\mysql-connector-java-5.1.34.jar" />   
    <!-- <classPathEntry location="C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar" />-->  
    <context id="DB2Tables" targetRuntime="MyBatis3">  
        <commentGenerator>  
            <property name="suppressAllComments" value="true" />  
        </commentGenerator>  
        <!-- 資料庫連結URL、使用者名、密碼 -->  
         <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/my_db?characterEncoding=utf8" userId="root" password="123456">   
        <!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa">-->  
        </jdbcConnection>  
        <javaTypeResolver>  
            <property name="forceBigDecimals" value="false" />  
        </javaTypeResolver>  
        <!-- 生成模型的包名和位置 -->  
        <javaModelGenerator targetPackage="andy.model" targetProject="D:\generator\src">  
            <property name="enableSubPackages" value="true" />  
            <property name="trimStrings" value="true" />  
        </javaModelGenerator>  
        <!-- 生成的映射檔案包名和位置 -->  
        <sqlMapGenerator targetPackage="andy.mapping" targetProject="D:\generator\src">  
            <property name="enableSubPackages" value="true" />  
        </sqlMapGenerator>  
        <!-- 生成DAO的包名和位置 -->  
        <javaClientGenerator type="XMLMAPPER" targetPackage="andy.dao" targetProject="D:\generator\src">  
            <property name="enableSubPackages" value="true" />  
        </javaClientGenerator>  
        <!-- 要生成那些表(更改tableName和domainObjectName就可以) -->  
        <table tableName="kb_city" domainObjectName="KbCity" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />  
        <!-- <table tableName="course_info" domainObjectName="CourseInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />  
        <table tableName="course_user_info" domainObjectName="CourseUserInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> -->  
    </context>  
</generatorConfiguration>        

修改代碼中D:\generator 自己所放jar包路徑、資料庫連結位址,使用者名、密碼,以及需要生成檔案對應的表名,儲存即可

檔案目錄如下:

4、打開指令提示符,進入D:\generator,輸入指令:java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite

如圖:

5、這時,你可以看到,dao、model、mapper xml檔案已經生成了

繼續閱讀