(二)一個很好用的自動生成工具——mybatis generator


mybatis generator-自動生成代碼

准備材料:

  一個文件夾,一個數據庫的驅動包,mybatis-generator-core-1.3.5.jar,一條生成語句

  如圖:(我用的是derby數據庫,使用其他數據庫需修改相應的jar驅動包)

 

 

 

 

 

generatorConfig.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE generatorConfiguration
 3    PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
 4   "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
 5  <generatorConfiguration>
 6     <!--數據庫驅動-->
 7      <classPathEntry    location="derby.jar" />
 8      <context id="Derby"    targetRuntime="MyBatis3">
 9     <!--把數據庫注釋添加到java文件中-->
10          <commentGenerator>
11              <property name="suppressDate" value="true"/>
12              <property name="suppressAllComments" value="true"/>
13          </commentGenerator>
14          <!--數據庫鏈接地址賬號密碼-->
15          <jdbcConnection driverClass="org.apache.derby.jdbc.EmbeddedDriver" connectionURL="jdbc:derby:E:/shiny/DdlUtils-test/mydb" userId="root" password="root">
16          </jdbcConnection>
17          <javaTypeResolver>
18              <property name="forceBigDecimals" value="false"/>
19          </javaTypeResolver>
20          <!--生成Model類存放位置-->
21          <javaModelGenerator targetPackage="com.standard.model" targetProject="src">
22             <property name="enableSubPackages" value="true"/>
23            <!--  <property name="trimStrings" value="true"/>  -->
24          </javaModelGenerator>
25          <!--生成映射文件存放地址-->
26         <sqlMapGenerator targetPackage="com.standard.mapper" targetProject="src">
27              <property name="enableSubPackages" value="true"/>
28          </sqlMapGenerator>
29          <!--生成Dao類存放地址-->
30          <javaClientGenerator type="XMLMAPPER" targetPackage="com.standard.mapper" targetProject="src">
31              <property name="enableSubPackages" value="true"/>
32          </javaClientGenerator>
33          
34         <!--生成對應表及表名-->
35         
36      <table tableName="standard_user" domainObjectName="User"  enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
37           
38          </table>
39      <table tableName="role" domainObjectName="Role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
40 
41     </context>
42  </generatorConfiguration>    

可以根據表中的注釋配置相應的內容:

  1.數據庫驅動:location  jar包名

  2.數據庫鏈接地址(賬號密碼)

  3.model類、映射文件、接口類存放地址(Java項目中的包目錄)

  4.需要生成映射的表名

----------------------------------------------------------------------------------------

<commentGenerator>
  <property name="suppressDate" value="true"/>
  <property name="suppressAllComments" value="true"/>
</commentGenerator>

可配置數據庫注釋添加到Java文件中

----------------------------------------------------------------------------------------

在當前目錄打開cmd

  生成語句:java -jar mybatis-generator-core-1.3.5.jar -configfile generatorConfig.xml -overwrite

  生成結果:

最后將src下的文件夾復制到項目的src目錄下即可

  xml中還有很多配置,還未用到,之后有機會再深入研究~

 


免責聲明!

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



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