---
title: "generator自动生成mybatis配置和类信息"
author: "javaEEdevelop"
date: "2012-11-15T11:40:52Z"
tags: []
categories: ["开源软件"]
slug: "generator-mybatis"
draft: false
lastmod: "2012-11-15T11:41:06Z"
id: 1726132
url: "https://javaeedevelop.iteye.com/blog/1726132"
---

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](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代码      [![收藏代码](http://jadethao.iteye.com/images/icon_star.png)]()

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;!DOCTYPE generatorConfiguration
03. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
04. "http://mybatis.org/dtd/mybatis-generator-config\_1\_0.dtd"&gt;
05. &lt;generatorConfiguration&gt;
06. &lt;!-- classPathEntry:数据库的JDBC驱动的jar包地址--&gt;
07. &lt;classPathEntry location="E:\\oracle\\product\\10.2.0\\db\_1\\jdbc\\lib\\ojdbc14.jar" /&gt;
08. &lt;context id="DB2Tables" targetRuntime="MyBatis3"&gt;
09. &lt;commentGenerator&gt;
10. &lt;!-- 是否去除自动生成的注释 true：是 ： false:否 --&gt;
11. &lt;property name="suppressAllComments" value="true" /&gt;
12. &lt;!--数据库连接的信息：驱动类、连接地址、用户名、密码 --&gt;
13. &lt;/commentGenerator&gt;
14. &lt;jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
15. connectionURL="jdbc:oracle:thin:@198.17.1.1:1521:ORCL"
16. userId="unuser"
17. password="password"&gt;
18. &lt;/jdbcConnection&gt;
19. &lt;!--  默认false，把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer
20. true，把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal
21. --&gt;
22. &lt;javaTypeResolver &gt;
23. &lt;property name="forceBigDecimals" value="false" /&gt;
24. &lt;/javaTypeResolver&gt;
25. &lt;!-- targetProject:自动生成代码的位置 --&gt;
26. &lt;javaModelGenerator targetPackage="com.soft.model" targetProject="E:\\WebWorkSpace\\workspace\_js\\downAttachdemo\\src"&gt;
27. &lt;!-- enableSubPackages:是否让schema作为包的后缀 --&gt;
28. &lt;property name="enableSubPackages" value="true" /&gt;
29. &lt;!-- 从数据库返回的值被清理前后的空格  --&gt;
30. &lt;property name="trimStrings" value="true" /&gt;
31. &lt;/javaModelGenerator&gt;
32. &lt;sqlMapGenerator targetPackage="sqlmap"  targetProject="E:\\WebWorkSpace\\workspace\_js\\downAttachdemo\\conf"&gt;
33. &lt;property name="enableSubPackages" value="false" /&gt;
34. &lt;/sqlMapGenerator&gt;
35. &lt;javaClientGenerator type="XMLMAPPER" targetPackage="com.soft.mapping"  targetProject="E:\\WebWorkSpace\\workspace\_js\\downAttachdemo\\src"&gt;
36. &lt;property name="enableSubPackages" value="true" /&gt;
37. &lt;/javaClientGenerator&gt;
38. &lt;!-- tableName:用于自动生成代码的数据库表；domainObjectName:对应于数据库表的javaBean类名 --&gt;
39. &lt;table schema="untodo" tableName="mocha\_t\_app" domainObjectName="MochaTodoApp" &gt;
40. &lt;/table&gt;
41. &lt;/context&gt;
42. &lt;/generatorConfiguration&gt;

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，默认字段都生成为类属性。   
&lt;ignoreColumn column="FRED" /&gt;//忽略字段   
&lt;columnOverride column="LONG\_VARCHAR\_FIELD" jdbcType="VARCHAR" /&gt;//无论字段是什么类型，生成的类属性都是varchar。 

3、运行有四种：命令生成（最简单）、Java生成、ant生成、maven生成。这里说两种，有兴趣其余的可以在mybatis官网去学习。 

1）、运行-》cmd-&gt;java - jar jar包的文件路径  -configfile  generator.xml的文件路径  -overwrite 命令。  
如下： 

Java代码      [![收藏代码](http://jadethao.iteye.com/images/icon_star.png)]()

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代码      [![收藏代码](http://jadethao.iteye.com/images/icon_star.png)]()

1. List&lt;String&gt; warnings = new ArrayList&lt;String&gt;();
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等等。 

 

只是做个记录(http://jadethao.iteye.com/blog/1726115)，这个原址，自己没实验过呢。。