mybatis反转数据库的配置文件:

generatorConfig.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>
<!-- 配置mysql驱动包,使用的绝对路径 -->
<classPathEntry location="G:\javalib\jdbc\mysql-connector-java-5.0.8-bin.jar"/> <context id="westward_mysql_tables" targetRuntime="MyBatis3">
<!-- 控制生成的代码中的注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
<property name="suppressDate" value="true"/>
</commentGenerator> <!-- 数据库连接 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis"
userId="yao" password="y123" />
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver> <!-- 数据表对应的model 层 -->
<javaModelGenerator targetPackage="com.westward.bean" targetProject="src">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator> <!-- sql mapper 映射配置文件 -->
<sqlMapGenerator targetPackage="com.westward.mapper" targetProject="src">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator> <!-- 在ibatis2 中是dao层,但在mybatis3中,其实就是mapper接口 -->
<javaClientGenerator targetPackage="com.westward.inter" type="XMLMAPPER" targetProject="src">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator> <!-- 要对那些数据表进行生成操作,必须要有一个. -->
<table tableName="category" schema="mybatis" domainObjectName="Category"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false"
enableUpdateByExample="false" selectByExampleQueryId="false" >
</table>
</context> </generatorConfiguration>

根据配置文件,生成对应的bean,接口,mapper的方法:

mybatis官网:http://www.mybatis.org/generator/running/running.html

给了好几种方法:我就摘出两种最常用的吧:

1.命令行的方式:

java -jar mybatis-generator-core-x.x.x.jar -configfile generatorConfig.xml -overwrite

注意jar包和xml文件的路径
我的命令行是在项目根目录下 2.java代码的形式:
public static void main(String[] args) {
List<String> warnings= new ArrayList<String>();
boolean overwrite= true;
String genCfg= "G:/workspace10/mybatisgenerator/src/generatorConfig.xml";
File configFile= new File(genCfg);
ConfigurationParser cp= new ConfigurationParser(warnings);
Configuration config= null;
try {
config= cp.parseConfiguration(configFile);
DefaultShellCallback callback= new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator= null;
myBatisGenerator= new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null); System.out.println(warnings); } catch (IOException e) {
e.printStackTrace();
} catch (XMLParserException e) {
e.printStackTrace();
} catch (InvalidConfigurationException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

其中集合warning,会存储执行的错误信息,若无错误,则集合中无元素。

可能出现的错误:
[There are no statements enabled for table mybatis.category, this table will be ignored.]
原因:generatorConfig.xml中,<table>标签配成了这样,

把标红的去掉就行了,标红的默认是true,不需要显示配成false.上边的带Example的配成false就行,带Example的是指示例,这个基本不需要。

附上maven结构的web项目,一次构建多个表的配置:

<?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>
<!-- 配置mysql驱动包,使用的绝对路径 -->
<classPathEntry location="G:\javalib\jdbc\mysql-connector-java-5.0.8-bin.jar"/> <context id="westward_mysql_tables" targetRuntime="MyBatis3">
<!-- 控制生成的代码中的注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
<property name="suppressDate" value="true"/>
</commentGenerator> <!-- 数据库连接 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/bookestore"
userId="yao" password="y123" />
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver> <!-- 数据表对应的model 层 -->
<javaModelGenerator targetPackage="com.blue.bean" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator> <!-- sql mapper 映射配置文件 -->
<sqlMapGenerator targetPackage="com.blue.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator> <!-- 在ibatis2 中是dao层,但在mybatis3中,其实就是mapper接口 -->
<javaClientGenerator targetPackage="com.blue.dao" type="XMLMAPPER" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator> <!-- 要对那些数据表进行生成操作,必须要有一个. -->
<table tableName="orders" schema="mybatis" domainObjectName="Order"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false"
enableUpdateByExample="false" selectByExampleQueryId="false" >
</table>
<table tableName="orderitem" schema="mybatis" domainObjectName="OrderItem"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false"
enableUpdateByExample="false" selectByExampleQueryId="false" >
</table>
<table tableName="users" schema="mybatis" domainObjectName="User"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false"
enableUpdateByExample="false" selectByExampleQueryId="false" >
</table>
<table tableName="products" schema="mybatis" domainObjectName="Product"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false"
enableUpdateByExample="false" selectByExampleQueryId="false" >
</table>
</context> </generatorConfiguration>


mybatis generator工具的使用的更多相关文章

  1. springboot集成mybatis及mybatis generator工具使用

    原文链接 前言mybatis是一个半自动化的orm框架,所谓半自动化就是mybaitis只支持数据库查出的数据映射到pojo类上,而实体到数据库的映射需要自己编写sql语句实现,相较于hibernat ...

  2. springboot入门(三)-- springboot集成mybatis及mybatis generator工具使用

    前言 mybatis是一个半自动化的orm框架,所谓半自动化就是mybaitis只支持数据库查出的数据映射到pojo类上,而实体到数据库的映射需要自己编写sql语句实现,相较于hibernate这种完 ...

  3. mybatis generator工具集成(一)

    第一步,pom中加入 <build> <plugins> <plugin> <groupId>org.springframework.boot</ ...

  4. Hello Mybatis 02 mybatis generator

    接着上一篇文章通过Mybatis完成了一个User的CRUD的功能之后,这篇开始还需要建立一个Blog类,这样就可以模拟一个简单的微博平台的数据库了. 数据库准备 首先我们,还是需要在数据库中新建一个 ...

  5. Mybatis generator 自动生成代码(2)

    最近准备开始做一个项目,需要开始手动创建sql,于是将Mybatis generator 工具功能强化了下. 首先,这里引入到版本一点的包 <dependency> <groupId ...

  6. Eclipse 使用mybatis generator插件自动生成代码

    Eclipse 使用mybatis generator插件自动生成代码 标签: mybatis 2016-12-07 15:10 5247人阅读 评论(0) 收藏 举报 .embody{ paddin ...

  7. MyBatis之七:使用generator工具

    可以将mybatis理解成一种半自动化orm框架,通过注解或者配置xml映射文件来手写相关sql语句,不能像我之前介绍orm的文章那样全对象化操作数据库增删改查.其实你会发现,手写配置xml映射文件是 ...

  8. 还在使用MyBatis Generator?试试这个工具

    代码生成 在企业软件开发过程中,大多数时间都是面向数据库表的增删改查开发.通过通用的增删改查代码生成器,可以有效的提高效率,降低成本:把有规则的重复性劳动让机器完成,解放开发人员. MyBatis G ...

  9. Mybatis Generator生成工具配置文件详解

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...

随机推荐

  1. sort-插入排序

    void sort_insertion(vector<int> &v) { for(int i=1;i<v.size();i++) { for(int j=i;j>0; ...

  2. 关于fstream、ifstream、ofstream读写文本文件、二进制文件详解

    fstream.ifstream.ofstream是c++中关于文件操作的三个类 fstream类对文件进行读操作和写操作 打开文件 fstream fs("要打开的文件名",打开 ...

  3. 前端实现实时通讯-----ajax长连接

    因为web运行模式为请求-响应,服务端无法主动发起通讯,所以通讯实时性存在各种问题,ajax轮询可以模拟及时通讯,但连接太频繁将给服务端带来很大压力,不频繁则实时性很差. 下面介绍在web上真正实现实 ...

  4. POI 导入导出时异常[java.io.IOException: Broken pipe]

    使用用POI导出文件时抛出异常java.io.IOException: Broken pipe ERROR: 'java.io.IOException: Broken pipe' org.apache ...

  5. 图片路径转base64字节码

    package product; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOE ...

  6. Use GDB to debug a C++ program called from a shell script

    解决了我一个大问题!!! http://stackoverflow.com/questions/5048112/use-gdb-to-debug-a-c-program-called-from-a-s ...

  7. 雷林鹏分享:jQuery EasyUI 树形菜单 - 创建异步树形菜单

    jQuery EasyUI 树形菜单 - 创建异步树形菜单 为了创建异步的树形菜单(Tree),每一个树节点必须要有一个 'id' 属性,这个将提交回服务器去检索子节点数据. 创建树形菜单(Tree) ...

  8. causal snps | causal variants | tensorflow | 神经网络实战 | Data Simulation

    先读几篇文章: Interpretation of Association Signals and Identification of Causal Variants from Genome-wide ...

  9. 20165327 2017-2018-2 《Java程序设计》第7周学习总结

    20165327 2017-2018-2 <Java程序设计>第7周学习总结 教材内容总结 第十一章 (一)MySQL数据库服务器 下载安装MySQL服务器 启动MySQL数据库服务器 在 ...

  10. Lab 3-4

    Analyze the malware found in the file Lab03-04.exe using basic dynamic analysis tools. (This program ...