使用MyBatis Generator插件自动生成Dto、Dao、Mapping

来源:互联网 发布:mac os 图片编辑 编辑:程序博客网 时间:2024/05/17 01:07

一、下载mybatis-generator-core

进入:http://code.google.com/p/mybatis/

选择Downloads,再选择MyBatis Generator Tool下载即可。

二、生成配置文件

新建一个空的XML配置文件,名称可以随便取,这里以generatorConfig.xml为名。最好将这个文件放在下载后的lib目录中,如图:


其中mysql的驱动可以随便放在非中文路径的地方,这里为了方便就放在lib目录下。

自动生成最重要的就是配置文件的书写,现在就开始介绍generatorConfig.xml这个文件的具体内容:

[html] view plaincopy
  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="mysql-connector-java-5.0.6-bin.jar"/>  
  8.     <context id="DB2Tables"  targetRuntime="MyBatis3">  
  9.         <commentGenerator>  
  10.             <property name="suppressDate" value="true"/>  
  11.             <!-- 是否去除自动生成的注释 true:是 : false:否 -->  
  12.             <property name="suppressAllComments" value="true"/>  
  13.         </commentGenerator>  
  14.         <!--数据库链接URL,用户名、密码 -->  
  15.         <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/test" userId="test" password="test">  
  16.         </jdbcConnection>  
  17.         <javaTypeResolver>  
  18.             <property name="forceBigDecimals" value="false"/>  
  19.         </javaTypeResolver>  
  20.         <!-- 生成模型的包名和位置-->  
  21.         <javaModelGenerator targetPackage="test.model" targetProject="src">  
  22.             <property name="enableSubPackages" value="true"/>  
  23.             <property name="trimStrings" value="true"/>  
  24.         </javaModelGenerator>  
  25.         <!-- 生成映射文件的包名和位置-->  
  26.         <sqlMapGenerator targetPackage="test.mapping" targetProject="src">  
  27.             <property name="enableSubPackages" value="true"/>  
  28.         </sqlMapGenerator>  
  29.         <!-- 生成DAO的包名和位置-->  
  30.         <javaClientGenerator type="XMLMAPPER" targetPackage="test.dao" targetProject="src">  
  31.             <property name="enableSubPackages" value="true"/>  
  32.         </javaClientGenerator>  
  33.         <!-- 要生成哪些表-->  
  34.         <table tableName="about" domainObjectName="AboutDto" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>  
  35.         <table tableName="user" domainObjectName="UserDto" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>  
  36.         <table tableName="syslogs" domainObjectName="SyslogsDto" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>  
  37.     </context>  
  38. </generatorConfiguration>  
其中需要注意的有数据库驱动、数据库URL、用户名、密码、生成模型的包名和位置、生成映射文件的包名和位置、生成DAO的包名和位置以及最后需要生成的表名和对应的类名。


三、运行

需要通过CMD命令行方式来运行,首先可以先准备一个运行的脚本,这里使用的脚本是:java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

需要注意的是:mybatis-generator-core-1.3.2.jar为下载的对应版本的jar,generatorConfig.xml 为配置文件名,如果不为这个可以在这里进行修改。

启动cmd进入到“F:\soft\mybatis-generator-core-1.3.2\lib”这个目录下,如图:


生成成功后进到src目录下,可以看到已经生成了对应的model、dao、mapping.

1 0
原创粉丝点击