generator自動生成mybatis配置和類信息


generator自動生成mybatis的xml配置、model、map等信息:
1、下載mybatis-generator-core-1.3.2.jar包。
       網址:http://code.google.com/p/mybatis/downloads/list?can=3&q=Product%3DGenerator,下載mybatis-generator-core-1.3.2-bundle.zip,解壓
       找到lib下的需要jar包。
2、編寫genertor的xml文件,名下: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:數據庫的JDBC驅動的jar包地址-->  
      <classPathEntry location="E:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar" />  
    <context id="DB2Tables" targetRuntime="MyBatis3">  
      <commentGenerator>  
        <!-- 是否去除自動生成的注釋 true:是 : false:否 -->  
        <property name="suppressAllComments" value="true" />  
        <!--數據庫連接的信息:驅動類、連接地址、用戶名、密碼 -->  
      </commentGenerator>  
      <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"  
              connectionURL="jdbc:oracle:thin:@198.17.1.1:1521:ORCL"  
              userId="unuser"  
              password="password">  
      </jdbcConnection>  
        <!--  默認false,把JDBC DECIMAL 和 NUMERIC 類型解析為 Integer   
             true,把JDBC DECIMAL 和 NUMERIC 類型解析為java.math.BigDecimal   
         -->   
      <javaTypeResolver >  
         <property name="forceBigDecimals" value="false" />  
      </javaTypeResolver>  
      <!-- targetProject:自動生成代碼的位置 -->  
      <javaModelGenerator targetPackage="com.soft.model" targetProject="E:\WebWorkSpace\workspace_js\downAttachdemo\src">  
          <!-- enableSubPackages:是否讓schema作為包的后綴 -->       
          <property name="enableSubPackages" value="true" />  
        <!-- 從數據庫返回的值被清理前后的空格  -->   
          <property name="trimStrings" value="true" />  
      </javaModelGenerator>  
        
      <sqlMapGenerator targetPackage="sqlmap"  targetProject="E:\WebWorkSpace\workspace_js\downAttachdemo\conf">  
           <property name="enableSubPackages" value="false" />  
      </sqlMapGenerator>  
        
      <javaClientGenerator type="XMLMAPPER" targetPackage="com.soft.mapping"  targetProject="E:\WebWorkSpace\workspace_js\downAttachdemo\src">  
        <property name="enableSubPackages" value="true" />  
      </javaClientGenerator>  
      <!-- tableName:用於自動生成代碼的數據庫表;domainObjectName:對應於數據庫表的javaBean類名 -->  
      <table schema="untodo" tableName="mocha_t_app" domainObjectName="MochaTodoApp" >  
        
      </table>  
    </context>  
       
    </generatorConfiguration>  

  table其他屬性:
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"
schema即為數據庫名, tableName為對應的數據庫表, domainObjectName是要生成的實體類,
如果想要mapper配置文件加入sql的where條件查詢, 可以將enableCountByExample等設為true,
這樣就會生成一個對應domainObjectName的Example類, enableCountByExample等設為false時,
就不會生成對應的Example類了.

如果table里邊不配置property,默認字段都生成為類屬性。
<ignoreColumn column="FRED" />//忽略字段
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />//無論字段是什么類型,生成的類屬性都是varchar。

3、運行有四種:命令生成(最簡單)、Java生成、ant生成、maven生成。這里說兩種,有興趣其余的可以在mybatis官網去學習。

1)、運行-》cmd->java - jar jar包的文件路徑  -configfile  generator.xml的文件路徑  -overwrite 命令。
如下:

Java代碼   收藏代碼
  1. java -jar E:\Websoft\mybaits\mybatis-generator-core-1.3.2\lib\mybatis-generator-core-1.3.2.jar -configfile E:\WebWorkSpace\workspace_js\downAttachdemo\src\com\mochasoft\down\generator.xml -overwrite  


成功時輸出:MyBatis Generator finished successfully.
2)、java運行關鍵代碼:

 

Java代碼   收藏代碼
  1. List<String> warnings = new ArrayList<String>();  
  2.   boolean overwrite = true;  
  3.   File configFile = new File("generatorConfig.xml");  
  4.   ConfigurationParser cp = new ConfigurationParser(warnings);  
  5.   Configuration config = cp.parseConfiguration(configFile);  
  6.   DefaultShellCallback callback = new DefaultShellCallback(overwrite);  
  7.   MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);  
  8.   myBatisGenerator.generate(null);  


  
其實Java運行,細分可以分兩種,還有一種可以去官網學習。
 
4、生成代碼之后,根據自己的實際項目架構,可以對生成的代碼進行適當的修改,如把數據庫管理交有spring等等。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM