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 ...
随机推荐
- 学习笔记32—python常见问题及解决办法
1.Anaconda3 中 Spyder 无法打开/点击没有反应 应对方法 1).通过pip安装pyqt5:pip install pyqt5 2).输入以下命令:spyder --new-insta ...
- git 先创建本地仓库,再关联远程
之前都是先在GitHub或者bitbucket上创建repo,然后在本地直接git clone下来. 如果一定需要先在本地创建好文件夹,然后再关联远程仓库. 是这样: 1在远程创建仓库这步不变. 2 ...
- Python中什么是变量
在Python中,变量的概念基本上和初中代数的方程变量是一致的. 例如,对于方程式 y=x*x ,x就是变量.当x=2时,计算结果是4,当x=5时,计算结果是25. 只是在计算机程序中,变量不仅可以是 ...
- lambda表达式与方法重载问题
笔者之前在学习Java8新特性的时候,最吸引我的就是lambda表达式,它无疑为Java函数编程提供了强有力的支持.lambda表达式的使用方法很简单,下面给出最简单的用法. // Interface ...
- 基于C# winform实现图片流存储到文件
本文所述实例实现将一张图片上传到指定的文件夹,然后在窗体上的PictrueBox控件中显示出来. 具体功能代码如下: private void btnUpload_Click(object sende ...
- LeetCode--004--寻找两个有序数组的中位数(java)
转自https://blog.csdn.net/chen_xinjia/article/details/69258706 其中,N1=4,N2=6,size=4+6=10. 1,现在有的是两个已经排好 ...
- (Gorails) activeStore模块,把一堆属性放在一个hash对象内。gem 'activerecord-typedstore'增强了store模块,更好用了
https://api.rubyonrails.org/classes/ActiveRecord/Store.html https://gorails.com/episodes/preferences ...
- 字符串hash
hash[i]=(hash[i-1]*p+idx(s[i]))%mod p和mod取不同的较大的素数
- ddt 实例
from :https://blog.csdn.net/wushuai150831/article/details/78453549
- lvalue require as increment operand
#include<stdio.h> #include<stdlib.h> int main() { char source[]="hello"; //创建一 ...