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,下载mybatis-generator-core-1.3.2-bundle.zip,解压
       找到lib下的需要jar包。

2、编写genertor的xml文件,名下:generator.xml

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. <!-- classPathEntry:数据库的JDBC驱动的jar包地址-->
  7. <classPathEntry location="E:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar" />
  8. <context id="DB2Tables" targetRuntime="MyBatis3">
  9. <commentGenerator>
  10. <!-- 是否去除自动生成的注释 true:是 : false:否 -->
  11. <property name="suppressAllComments" value="true" />
  12. <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
  13. </commentGenerator>
  14. <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
  15. connectionURL="jdbc:oracle:thin:@198.17.1.1:1521:ORCL"
  16. userId="unuser"
  17. password="password">
  18. </jdbcConnection>
  19. <!--  默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer
  20. true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal
  21. -->
  22. <javaTypeResolver >
  23. <property name="forceBigDecimals" value="false" />
  24. </javaTypeResolver>
  25. <!-- targetProject:自动生成代码的位置 -->
  26. <javaModelGenerator targetPackage="com.soft.model" targetProject="E:\WebWorkSpace\workspace_js\downAttachdemo\src">
  27. <!-- enableSubPackages:是否让schema作为包的后缀 -->
  28. <property name="enableSubPackages" value="true" />
  29. <!-- 从数据库返回的值被清理前后的空格  -->
  30. <property name="trimStrings" value="true" />
  31. </javaModelGenerator>
  32. <sqlMapGenerator targetPackage="sqlmap"  targetProject="E:\WebWorkSpace\workspace_js\downAttachdemo\conf">
  33. <property name="enableSubPackages" value="false" />
  34. </sqlMapGenerator>
  35. <javaClientGenerator type="XMLMAPPER" targetPackage="com.soft.mapping"  targetProject="E:\WebWorkSpace\workspace_js\downAttachdemo\src">
  36. <property name="enableSubPackages" value="true" />
  37. </javaClientGenerator>
  38. <!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 -->
  39. <table schema="untodo" tableName="mocha_t_app" domainObjectName="MochaTodoApp" >
  40. </table>
  41. </context>
  42. </generatorConfiguration>

able其他属性:
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,默认字段都生成为类属性。
<ignoreColumn column="FRED" />//忽略字段
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />//无论字段是什么类型,生成的类属性都是varchar。

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

1)、运行-》cmd->java - jar jar包的文件路径  -configfile  generator.xml的文件路径  -overwrite 命令。
如下:

  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运行关键代码:

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

generator自动生成mybatis的xml配置的更多相关文章

  1. generator自动生成mybatis配置和类信息

    generator自动生成mybatis的xml配置.model.map等信息: 1.下载mybatis-generator-core-1.3.2.jar包.        网址:http://cod ...

  2. Mybatis generator自动生成mybatis配置和类信息

    自动生成代码方式两种: 1.命令形式生成代码,详细讲解每一个配置参数. 2.Eclipse利用插件形式生成代码. 安装插件方式: eclipse插件安装地址:http://mybatis.google ...

  3. Mybatis最入门---代码自动生成(generatorConfig.xml配置)

    [一步是咫尺,一步即天涯] 经过前文的叙述,各位看官是不是已经被Mybatis的强大功能给折服了呢?本文我们将介绍一个能够极大提升我们开发效率的插件:即代码自动生成.这里的代码自动生成包括,与数据库一 ...

  4. Mybatis 代码自动生成(generatorConfig.xml配置)

    博客推荐: Mybatis最入门---代码自动生成(generatorConfig.xml配置) MyBatis Generator generatorConfig.xml配置详解 pom.xml&l ...

  5. 使用Mybatis Generator自动生成Mybatis相关代码

    本文将简要介绍怎样利用Mybatis Generator自动生成Mybatis的相关代码: 一.构建一个环境: 1. 首先创建一个表: CREATE TABLE pet (name VARCHAR(2 ...

  6. 使用generator自动生成mybatis model、mapper.xml、mapper等(转)

    原文链接:http://www.cnblogs.com/lichenwei/p/4145696.html Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件 ...

  7. 使用generator自动生成Mybatis映射配置文件

    在使用mybatis时,映射文件的配置非常麻烦,对于做逻辑不是很复杂,功能不是特别关键的模块的时候,我们没有必要手动书写,可以使用generator工具生成. generator工具实际上就是根据数据 ...

  8. 使用MyBatis Generator自动生成MyBatis的代码

    这两天需要用到MyBatis的代码自动生成的功能,由于MyBatis属于一种半自动的ORM框架,所以主要的工作就是配置Mapping映射文件,但是由于手写映射文件很容易出错,所以可利用MyBatis生 ...

  9. MyBatis Generator自动生成MyBatis的映射代码

    MyBatis Generator大大简化了MyBatis的数据库的代码编写,有了一个配置文件,就可以直接根据表映射成实体类.Dao类和xml映射.资源地址:MyBatis项目地址:http://my ...

随机推荐

  1. Bootstrap FileInput 多图上传插件 文档属性说明

    Bootstrap FileInput 多图上传插件   原文链接:http://blog.csdn.net/misterwho/article/details/72886248?utm_source ...

  2. git —— 标签

    标签:为分支添加一个可读标识. 1.创建标签 操作步骤: 切换到需要打标签的分支上 $ git branch $ git checkout master 为当前分支新增一个标签 $ git tag v ...

  3. Docker容器跨主机通信之:直接路由方式

    一.Docker网络基本原理 直观上看,要实现网络通信,机器需要至少一个网络接口(物理接口或虚拟接口)与外界相通,并可以收发数据包:此外,如果不同子网之间要进行通信,需要额外的路由机制. Docker ...

  4. 基于docker 搭建Elasticsearch6.2.4(centos)

    一.介绍 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作为 ...

  5. grail开发环境的搭建

    本文参考:Grails入门指南(第二版) 1. 下载jdk和Grail http://www.oracle.com/technetwork/java/javase/downloads/ http:// ...

  6. 经典面试题:n个数字(0,1,…,n-1)形成一个圆圈

    题目: n个数字(0,1,…,n-1)形成一个圆圈,从数字0开始, 每次从这个圆圈中删除第m个数字(第一个为当前数字本身,第二个为当前数字的下一个数字). 当一个数字删除后,从被删除数字的下一个继续删 ...

  7. #Git 详细中文安装教程

    Step 1 Information 信息 Please read the following important information before continuing 继续之前,请阅读以下重要 ...

  8. Hive(四)Hive的3种连接方式与DbVisualizer连接Hive

    一.CLI连接 进入到 bin 目录下,直接输入命令: [root@node21 ~]# hive SLF4J: Class path contains multiple SLF4J bindings ...

  9. CentOS7.6安装rime輸入法

    # solve dependencyyum install -y gcc gcc-c++ boost boost-devel cmake make cmake3yum install glog glo ...

  10. poj1611 The Suspects(并查集)

    题目链接 http://poj.org/problem?id=1611 题意 有n个学生,编号0~n-1,m个社团,每个社团有k个学生,如果社团里有1个学生是SARS的疑似患者,则该社团所有人都要被隔 ...