mybatis generator工具的使用
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工具的使用的更多相关文章
- springboot集成mybatis及mybatis generator工具使用
原文链接 前言mybatis是一个半自动化的orm框架,所谓半自动化就是mybaitis只支持数据库查出的数据映射到pojo类上,而实体到数据库的映射需要自己编写sql语句实现,相较于hibernat ...
- springboot入门(三)-- springboot集成mybatis及mybatis generator工具使用
前言 mybatis是一个半自动化的orm框架,所谓半自动化就是mybaitis只支持数据库查出的数据映射到pojo类上,而实体到数据库的映射需要自己编写sql语句实现,相较于hibernate这种完 ...
- mybatis generator工具集成(一)
第一步,pom中加入 <build> <plugins> <plugin> <groupId>org.springframework.boot</ ...
- Hello Mybatis 02 mybatis generator
接着上一篇文章通过Mybatis完成了一个User的CRUD的功能之后,这篇开始还需要建立一个Blog类,这样就可以模拟一个简单的微博平台的数据库了. 数据库准备 首先我们,还是需要在数据库中新建一个 ...
- Mybatis generator 自动生成代码(2)
最近准备开始做一个项目,需要开始手动创建sql,于是将Mybatis generator 工具功能强化了下. 首先,这里引入到版本一点的包 <dependency> <groupId ...
- Eclipse 使用mybatis generator插件自动生成代码
Eclipse 使用mybatis generator插件自动生成代码 标签: mybatis 2016-12-07 15:10 5247人阅读 评论(0) 收藏 举报 .embody{ paddin ...
- MyBatis之七:使用generator工具
可以将mybatis理解成一种半自动化orm框架,通过注解或者配置xml映射文件来手写相关sql语句,不能像我之前介绍orm的文章那样全对象化操作数据库增删改查.其实你会发现,手写配置xml映射文件是 ...
- 还在使用MyBatis Generator?试试这个工具
代码生成 在企业软件开发过程中,大多数时间都是面向数据库表的增删改查开发.通过通用的增删改查代码生成器,可以有效的提高效率,降低成本:把有规则的重复性劳动让机器完成,解放开发人员. MyBatis G ...
- Mybatis Generator生成工具配置文件详解
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...
随机推荐
- Golang socket
1.本例子实现了一个简单的TCP echo.客户端发送Hello,服务端回应World. 参考:<Socket编程> 2.服务端代码 package main import ( " ...
- Cocos Creator 脚本模板
1.由于新建Cocos Creator脚本带有很多注释,并且有时候需要增加定制的默认注释,所以需要修改脚本生成模板. 2.在CocosCreator\resources\static\template ...
- cocos2dx spine之一 :spine缓存 (c++ & lua)
cocos2dx版本为3.10 1.在使用spine的过程中,发现了一个比较严重的问题:每次创建SkeletonAnimation的时候都会很卡,即使是使用同一个骨骼数据skeletonData. 跟 ...
- vue cli 打包项目造成css背景图路径错误
vue cli做项目的时候难免会碰到,css引用背景图或者css,js之间的相互引用!!!这时候打包后可能会出现一个错误!!如下图: 写法: 错误: 会无端多出一个“/css/static/” 这样就 ...
- oracle查看表中否存在某字段,数据库是否存在某张表
数据库是否存在某字段 SELECT COUNT(*) FROM USER_TAB_COLUMNS WHERE TABLE_NAME = '表名' AND COLUMN_NAME = '字段名'; ...
- Learning by doing——百日“扇贝打卡” 历程&展望
Java结课了.如果说这学期学习这门课后最明显的成果,那就是写了那么多的博客吧.而如果说本学期最有里程碑的事,那就是背了100多天单词,其中还获得了徽章! 这次想说说从中学以来一直喜欢的一门课--英语 ...
- 牛客寒假算法基础集训营6 E 海啸
题目链接点这里 这个题输入类型是第一次见,并没有把n和m的具体范围给你,但是给了n*m的范围,武断的设为1e6*1e6的二维数组铁铁WA,就将二维数组转换为一维数组 题目类型属于二维数组前缀和,有um ...
- 『TensorFlow × MXNet』SSD项目复现经验
『TensorFlow』SSD源码学习_其一:论文及开源项目文档介绍 『TensorFlow』SSD源码学习_其二:基于VGG的SSD网络前向架构 『TensorFlow』SSD源码学习_其三:锚框生 ...
- 【其他】【navicat】【1】navicat导入txt文件中文乱码问题解决
正文: TXT文件默认编码为ANSI,另存为编码为UTF-8的文本文件即可 备注: 1,一般需要导入的数据都是一张excel表,需要将excel表另存为“文本文件(制表符分隔)(*.txt)”保存类型 ...
- 关于js原型链
关于原型链,我们先贴上一张图(来自某知乎大佬专栏),然后听我娓娓道来. 先来说说什么是原型? JavaScript 中的对象有一个特殊的 [[Prototype]] 内置属性,其实就是对于其他对象的引 ...
