1. 程式人生 > >自動生成實體類 dao類

自動生成實體類 dao類

自動生成dao方法  實體類

工程前   :借用 mybatis-gennerator外掛自動生成mybatis所需要的dao、bean、mapper xml檔案, 這樣我們可以節省一部分精力,把精力放在業務邏輯上。

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

第一步  :  可以在D盤新建一個資料夾  命名為 generator(這是為了後續步驟不再更改)

第二步  :  準備需要的jar包   分別為 ::mybatis-generator-core-1.3.2.jar、mysql-connector-java-5.1.34.jar(可以使用不同的版本)

第三步  : 在generator資料夾中建立一個檔案  命名為 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\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

可以成功

如圖