(二)一个很好用的自动生成工具——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中还有很多配置,还未用到,之后有机会再深入研究~

原文地址:https://www.cnblogs.com/zuzZ/p/8087135.html