mybatis-generator生成逆向工程两种方式
本文博客地址:http://blog.csdn.net/soonfly/article/details/64499423
逆向工程下载
链接:https://pan.baidu.com/s/1YOAq8w-1gex9e-n8wFARpA 密码:op9h
mybatis官方提供了一个逆向工程包,可以针对数据库表自动生成mybatis执行所需要的Pojo、Mapper xml文件、Mapper Interface接口文件。
mybatis-generator有很多种用法:命令行、eclipse/IDEA、Maven插件,其使用原理完全一样。
无论哪种方式,首先要准备两个组件包:mybatis-generator-core-1.X.X.jar 和MySQL-connector-Java-5.X.XX.jar (点击下载两个组件)
命令行方式
从这个入手,因为最方便。
1、新建任意目录(D:\A-TWM\Mybatis),把两个组件拷入目录。
2、新建配置文件,命名:config.xml
补充:下载好的jar包里面有帮助文档,打开后里面有配置文件的模板。
config.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="D:\A-TWM\Mybatis\mysql-connector-java-5.1.26-bin.jar" /> <context id="sqlGenerate" targetRuntime="MyBatis3">
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator> <!-- 数据库链接URL、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/tangwenmingdb?characterEncoding=utf8"
userId="root" password="root">
</jdbcConnection> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer;
为 true时把JDBC DECIMAL和NUMERIC类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver> <!-- 生成Pojo包名和位置 -->
<javaModelGenerator targetPackage="twm.mybatisdemo.pojo"
targetProject="D:\A-TWM\Mybatis\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="true" />
<!-- 清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator> <!-- 生成Mapper映射XML文件位置 -->
<sqlMapGenerator targetPackage="twm.mybatisdemo.mapper"
targetProject="D:\A-TWM\Mybatis\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator> <!-- 生成Mapper接口文件位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="twm.mybatisdemo.mapper" targetProject="D:\A-TWM\Mybatis\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator> <!-- 要生成哪些表(更改tableName和domainObjectName就可以) -->
<!-- tableName:要生成的表名
domainObjectName:生成后的实例名
enableCountByExample:Count语句中加入where条件查询,默认为true开启
enableUpdateByExample:Update语句中加入where条件查询,默认为true开启
enableDeleteByExample:Delete语句中加入where条件查询,默认为true开启
enableSelectByExample:Select多条语句中加入where条件查询,默认为true开启
selectByExampleQueryId:Select单个对象语句中加入where条件查询,默认为true开启
-->
<table tableName="user" domainObjectName="User"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false" />
<table tableName="category" />
<table tableName="order"/>
<table tableName="product"/>
<table tableName="order_detail"/>
</context>
</generatorConfiguration>
如果table里边不配置property,默认将所有字段逆向生成为类属性。
如果有些字段并不想生成为类属性,可以用ignoreColumn标签:
<ignoreColumn column="FRED" />//忽略字段
还可以指定逆向生成时,字段到属性的转换对应关系
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />//无论字段是什么类型,生成的类属性都是varchar。
3、通过cmd打开命令窗口
运行:java -jar mybatis-generator-core-1.3.2.jar -configfile config.xml -overwrite
出现MyBatis Generator finished successfully.表示运行成功,将指定生成位置(这里是src)的源码拷入工作项目中即可。
Eclipse方式
1、新建工程、将组件和将配置文件config.xml放到对应的目录
2、在main函数中写代码运行
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//指向逆向工程配置文件
File configFile = new File("generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
callback, warnings);
myBatisGenerator.generate(null);
}
3、以application的方式运行就可以了
mybatis-generator生成逆向工程两种方式的更多相关文章
- MyBatis配置数据源的两种方式
---------------------siwuxie095 MyBatis 配置数据源的两种方式 1.配置方 ...
- MyBatis获取参数值的两种方式
MyBatis获取参数值的两种方式:${}和#{} ${}的本质就是字符串拼接,#{}的本质就是占位符赋值 ${}使用字符串拼接的方式拼接sql,若为字符串类型或日期类型的字段进行赋值时,需要手动加单 ...
- mybatis批量保存的两种方式(高效插入)
知识点:mybatis中,批量保存的两种方式 1.使用mybatis foreach标签 2.mybatis ExecutorType.BATCH 参考博客:https://www.jb51.net/ ...
- mybatis Generator生成代码及使用方式
本文原创,转载请注明:http://www.cnblogs.com/fengzheng/p/5889312.html 为什么要有mybatis mybatis 是一个 Java 的 ORM 框架,OR ...
- mybatis调用存储过程的两种方式
先总结和说明一下注意点: 1.如果传入的某个参数可能为空,必须指定jdbcType 2.当传入map作为参数时,必须指定JavaType 3.如果做动态查询(参数为表名,sql关键词),可以使用${} ...
- 数据库链接 mybatis spring data jpa 两种方式
jdbc mybatis spring data jpa dao service webservice jaxrs jaxws springmvc w ...
- Mybatis Dao开发的两种方式(一)
原始Dao的开发方式: 1.创建数据库配置文件db.properties jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localh ...
- mybatis 传递参数的两种方式与模糊匹配 很重要
- mybatis Generator生成代码及使用方式(转载)
转载自:http://www.cnblogs.com/fengzheng/p/5889312.html 文章很棒,很不错,转了.
随机推荐
- Java命令学习系列(零)——常见命令及Java Dump介绍
一.常用命令: 在JDK的bin目彔下,包含了java命令及其他实用工具. jps:查看本机的Java中进程信息. jstack:打印线程的栈信息,制作线程Dump. jmap:打印内存映射,制作堆D ...
- hdu 1038 Biker's Trip Odometer(水题)
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=1038 Biker's Trip Odometer Time Limit: 2000/1000 MS ...
- Swift:宏定义
一.简述 swift中并没有加入宏系统,C语言使用#define定义的基本常量在导入Swift时被Swift编译自动转为Swfit语言的全局变量.但复杂的宏定义不能被Swift转换.Swift中类似宏 ...
- go语言之进阶篇方法的继承
1.方法的继承 示例: package main import "fmt" type Person struct { name string //名字 sex byte //性别, ...
- 屌丝就爱尝鲜头——java8再判断
这节,我们来通过具体的实例来看看Java8的具体用法. 首当其冲,就是lambda用法. 这里的案例,就是用lambda来实现runnable接口,我们知道以前用匿名内部类的方式来实现runnable ...
- (转)Unity3D研究院之IOS&Android收集Log文件
转自:http://www.xuanyusong.com/archives/2477 有段时间没有写过文章了,不知道大伙儿还记得雨松MOMO吗? 嘿嘿. 开发项目的时候尤其在处理与服务器交互这块,如果 ...
- Angular入门笔记
AngularJS(下面简称其为ng)是Google开源的一款JavaScript MVC框架,弥补了HTML在构建应用方面的不足,其通过使用指令(directives)结构来扩展HTML词汇,使开发 ...
- SpringBoot 启动错误搜集
Spring Boot:The Bean Validation API is on the classpath but no implementation could be found https:/ ...
- gcc 0长数组学习
首先,我们要知道,0长度的数组在ISO C和C++的规格说明书中是不允许的.这也就是为什么在VC++2012下编译你会得到一个警告:“warning C4200: 使用了非标准扩展 : 结构/联合中的 ...
- SQL Server如何进行时间比较的代码
例子: datediff(dd,add_time,getdate()) not between 0 and 7 select count(*) from table where DATEDIFF ([ ...