从命令行运行 MyBatis Generator

? ?

一、准备

1. 获取最新jar包

2. 获取xml dtd

<?xml version="1.0" encoding="UTF-8"?>

<!--
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->

<!--
  This DTD defines the structure of the MyBatis generator configuration file.
  Configuration files should declare the DOCTYPE as follows:

  <!DOCTYPE generatorConfiguration PUBLIC
    "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
    "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

  Please see the documentation included with MyBatis generator for details on each option
  in the DTD.  You may also view documentation on-line here:

  http://mybatis.org/docs/generator

-->

<!--
  The generatorConfiguration element is the root element for configurations.
-->
<!ELEMENT generatorConfiguration (properties?, classPathEntry*, context+)>

<!--
  The properties element is used to define a standard Java properties file
  that contains placeholders for use in the remainder of the configuration
  file.
-->
<!ELEMENT properties EMPTY>
<!ATTLIST properties
  resource CDATA #IMPLIED
  url CDATA #IMPLIED>

<!--
  The context element is used to describe a context for generating files, and the source
  tables.
-->
<!ELEMENT context (property*, plugin*, commentGenerator?, jdbcConnection, javaTypeResolver?,
                         javaModelGenerator, sqlMapGenerator?, javaClientGenerator?, table+)>
<!ATTLIST context id ID #REQUIRED
  defaultModelType CDATA #IMPLIED
  targetRuntime CDATA #IMPLIED
  introspectedColumnImpl CDATA #IMPLIED>

<!--
  The jdbcConnection element is used to describe the JDBC connection that the generator
  will use to introspect the database.
-->
<!ELEMENT jdbcConnection (property*)>
<!ATTLIST jdbcConnection
  driverClass CDATA #REQUIRED
  connectionURL CDATA #REQUIRED
  userId CDATA #IMPLIED
  password CDATA #IMPLIED>

<!--
  The classPathEntry element is used to add the JDBC driver to the run-time classpath.
  Repeat this element as often as needed to add elements to the classpath.
-->
<!ELEMENT classPathEntry EMPTY>
<!ATTLIST classPathEntry
  location CDATA #REQUIRED>

<!--
  The property element is used to add custom properties to many of the generator‘s
  configuration elements.  See each element for example properties.
  Repeat this element as often as needed to add as many properties as necessary
  to the configuration element.
-->
<!ELEMENT property EMPTY>
<!ATTLIST property
  name CDATA #REQUIRED
  value CDATA #REQUIRED>

<!--
  The plugin element is used to define a plugin.
-->
<!ELEMENT plugin (property*)>
<!ATTLIST plugin
  type CDATA #REQUIRED>

<!--
  The javaModelGenerator element is used to define properties of the Java Model Generator.
  The Java Model Generator builds primary key classes, record classes, and Query by Example
  indicator classes.
-->
<!ELEMENT javaModelGenerator (property*)>
<!ATTLIST javaModelGenerator
  targetPackage CDATA #REQUIRED
  targetProject CDATA #REQUIRED>

<!--
  The javaTypeResolver element is used to define properties of the Java Type Resolver.
  The Java Type Resolver is used to calculate Java types from database column information.
  The default Java Type Resolver attempts to make JDBC DECIMAL and NUMERIC types easier
  to use by substituting Integral types if possible (Long, Integer, Short, etc.)
-->
<!ELEMENT javaTypeResolver (property*)>
<!ATTLIST javaTypeResolver
  type CDATA #IMPLIED>

<!--
  The sqlMapGenerator element is used to define properties of the SQL Map Generator.
  The SQL Map Generator builds an XML file for each table that conforms to iBATIS‘
  SqlMap DTD.
-->
<!ELEMENT sqlMapGenerator (property*)>
<!ATTLIST sqlMapGenerator
  targetPackage CDATA #REQUIRED
  targetProject CDATA #REQUIRED>

<!--
  The javaClientGenerator element is used to define properties of the Java client Generator.
  The Java Client Generator builds Java interface and implementation classes
  (as required) for each table.
  If this element is missing, then the generator will not build Java Client classes.
-->
<!ELEMENT javaClientGenerator (property*)>
<!ATTLIST javaClientGenerator
  type CDATA #REQUIRED
  targetPackage CDATA #REQUIRED
  targetProject CDATA #REQUIRED
  implementationPackage CDATA #IMPLIED>

<!--
  The table element is used to specify a database table that will be the source information
  for a set of generated objects.
-->
<!ELEMENT table (property*, generatedKey?, columnRenamingRule?, (columnOverride | ignoreColumn)*) >
<!ATTLIST table
  catalog CDATA #IMPLIED
  schema CDATA #IMPLIED
  tableName CDATA #REQUIRED
  alias CDATA #IMPLIED
  domainObjectName CDATA #IMPLIED
  enableInsert CDATA #IMPLIED
  enableSelectByPrimaryKey CDATA #IMPLIED
  enableSelectByExample CDATA #IMPLIED
  enableUpdateByPrimaryKey CDATA #IMPLIED
  enableDeleteByPrimaryKey CDATA #IMPLIED
  enableDeleteByExample CDATA #IMPLIED
  enableCountByExample CDATA #IMPLIED
  enableUpdateByExample CDATA #IMPLIED
  selectByPrimaryKeyQueryId CDATA #IMPLIED
  selectByExampleQueryId CDATA #IMPLIED
  modelType CDATA #IMPLIED
  escapeWildcards CDATA #IMPLIED
  delimitIdentifiers CDATA #IMPLIED
  delimitAllColumns CDATA #IMPLIED>

<!--
  The columnOverride element is used to change certain attributes of the column
  from their default values.
-->
<!ELEMENT columnOverride (property*)>
<!ATTLIST columnOverride
  column CDATA #REQUIRED
  property CDATA #IMPLIED
  javaType CDATA #IMPLIED
  jdbcType CDATA #IMPLIED
  typeHandler CDATA #IMPLIED
  delimitedColumnName CDATA #IMPLIED>

<!--
  The ignoreColumn element is used to identify a column that should be ignored.
  No generated SQL will refer to the column, and no property will be generated
  for the column in the model objects.
-->
<!ELEMENT ignoreColumn EMPTY>
<!ATTLIST ignoreColumn
  column CDATA #REQUIRED
  delimitedColumnName CDATA #IMPLIED>

<!--
  The generatedKey element is used to identify a column in the table whose value
  is calculated - either from a sequence (or some other query), or as an identity column.
-->
<!ELEMENT generatedKey EMPTY>
<!ATTLIST generatedKey
  column CDATA #REQUIRED
  sqlStatement CDATA #REQUIRED
  identity CDATA #IMPLIED
  type CDATA #IMPLIED>

<!--
  The columnRenamingRule element is used to specify a rule for renaming
  columns before the corresponding property name is calculated
-->
<!ELEMENT columnRenamingRule EMPTY>
<!ATTLIST columnRenamingRule
  searchString CDATA #REQUIRED
  replaceString CDATA #IMPLIED>

<!--
  The commentGenerator element is used to define properties of the Comment Generator.
  The Comment Generator adds comments to generated elements.
-->
<!ELEMENT commentGenerator (property*)>
<!ATTLIST commentGenerator
  type CDATA #IMPLIED>
  

二、说明

mybatis-generator-core-1.3.2.jar 是可执行jar

主函数类是 Main-Class: org.mybatis.generator.api.ShellRunner (见jar包中 /META-INF/MANIFEST.MF )

点击查看源码

需要jdk环境,环境变量PATH需要设置

编写.bat 文件(用来执行java 命令),文件内容如下:

java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml 

准备适当的数据库驱动jar包,本例使用oracle驱动点击下载ojdbc14

xml 配置文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
  <classPathEntry location="./ojdbc14.jar" />

  <context id="OracleTables" targetRuntime="MyBatis3">

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

    <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
        connectionURL="jdbc:oracle:thin:@//172.16.50.67:1521/orcl"
        userId="e_channel"
        password="e_channel_test">
    </jdbcConnection>

    <javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>

    <!-- 模型 Plain Ordinary Java Object -->
    <javaModelGenerator targetPackage="cn.zno.pojo" targetProject="./">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>
    <!-- sql -->
    <sqlMapGenerator targetPackage="cn.zno.dao"  targetProject="./">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>
    <!-- 调用接口 Data Access Object -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="cn.zno.dao"  targetProject="./">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>
    <!-- table+ -->
    <table schema="" tableName="PERSON" domainObjectName="Person" ></table>

  </context>
</generatorConfiguration>

表结构如下:

create table PERSON
(
  ID   VARCHAR2(10),
  NAME VARCHAR2(10),
  AGE  NUMBER
)

三、清单

generator.bat
generator.xml
mybatis-generator-core-1.3.2.jar
ojdbc14.jar

四、生成

D:.
│  generator.bat
│  generator.xml
│  mybatis-generator-core-1.3.2.jar
│  ojdbc14.jar
│
└─cn
    └─zno
        ├─dao
        │      PersonMapper.java
        │      PersonMapper.xml
        │
        └─pojo
                Person.java
                PersonExample.java

五、获取地址

[email protected]:witaste/mybatis-generator-commondline.git

时间: 2024-12-27 20:29:48

从命令行运行 MyBatis Generator的相关文章

从命令行及java程序运行MyBatis Generator 1.3.x生成MyBatis3.x代码

近期因为项目需要,调研了myBatis 3.x的使用,当然,顺便也就研究了一下使用Generator来通过逆向工程生成pojo,mapper等文件.使用这个工具之前,要先下载相关的jar包,我使用的是最新的mybatis-generator-core-1.3.2.jar.下面将generatorConfig.xml列出来: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConf

用 Maven 运行 MyBatis Generator(Running MyBatis Generator With Maven)《IT蓝豹》

用 Maven 运行 MyBatis Generator(Running MyBatis Generator With Maven) IT蓝豹:http://itlanbao.com/codes.aspx#1,0 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. Running MyBatis Generator With Maven MBG

Beginning Python From Novice to Professional (2) - 命令行运行Python脚本

命令行运行Python脚本 Linux下先创建一个hello.py [python] view plaincopy $ gedit hello.py 输入: [python] view plaincopy #!/usr/bin/env python print 2+2 保存退出,运行: [python] view plaincopy $ python hello.py 4 我们也可以让它变得和普通程序一样执行 执行之前,让脚本文件具备可执行属性: [python] view plaincopy

从命令行运行django数据库操作

从命令行运行django数据库操作,报错: django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before

UIAutomation: 通过命令行运行测试用例

为了实现自动定时的运行脚本,我们需要在命令行运行和启动脚本,具体代码如下: instruments -t /Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.bundle/Contents/Resources/Automation.tracetemplate <full_path_to_application> -e UIASCRIPT <p

Java命令行运行参数说明大全(偷来的)

Java在运行已编译完成的类时,是通过java虚拟机来装载和执行的,java虚拟机通过操作系统命令JAVA_HOME"bin"java –option 来启动,-option为虚拟机参数,JAVA_HOME为JDK安装路径,通过这些参数可对虚拟机的运行状态进行调整,掌握参数的含义可对虚拟机的运行模式有更深入理解. 一.         查看参数列表:虚拟机参数分为基本和扩展两类,在命令行中输入JAVA_HOME"bin"java 就可得到基本参数列表,在命令行输入J

命令行运行Android Robotium自动化用例或单元测试用例

本文目录 1.运行所有的测试用例 2.运行单个测试类或某个TestSuite 3.运行某个测试类里面的某个测试方法 4.运行两个不同的测试类或类中的方法 命令行运行Android Robotium自动化用例或单元测试用例 1.运行所有的测试用例 举个栗子:运行测试工程下的所有用例 1 adb shell am instrument -w com.taobao.taobao.test/android.test.InstrumentationTestRunner 2.运行单个测试类或某个TestSu

使用命令行运行Java程序

我现在身边有好多人在学习java,只要一提到学习java语言,我们马上想到的工具是eclipse,MyEclipse,NetBeans等等. 也许是我们用惯了vc,对集成开发环境有太多的依赖.但是,我个人觉得,对于一个开发人员有必要熟悉各种开发工具,更要尝试最原始的开发工具,即命令行. 同时我还发现用命令行更对java的语法有更深刻的理解,而不是仅仅停留在背诵记忆的层次上. 我们先介绍一下常用的命令: ①cd命令---切换目录:   e.g. cd src ②javac命令---编译java源程

java要在命令行运行eclipse的项目的方法

在命令行运行eclipse的项目时需要把该项目生成一个可以执行的jar包,才可以在命令行下执行:分为两种情况,一种是项目中没有调用第三方的jar包,这种比较简单,网上的资源也很多,本文主要讲述第二中情况该项目调用了第三方的jar包,生成该包有两种方法: 方法一:手动生成一个"MANIFEST.MF"的文件 而且还需要写一些文件路劲什么的信息:太过于复杂,而且极容易出错,如果有兴趣请参考博客:http://www.cnblogs.com/lanxuezaipiao/p/3291641.h