什么是逆向工程?
在学习的过程中会发现,需要我们写大量的sql语句
此时mybaatis官方为我们提供逆向工程可以针对单表自动生成的mybatis执行所需要的代码
 
 
使用方法:
   MyBatis Generator (MBG) can be run in the following ways:
 
推荐使用红色的方法
 
jar包

官方文档在解压后的doc文件中点击index.html

简单的讲解一下:

MyBatis GeneratorXML Configuration File Reference

建立新的工程:
导入需要加入的jar

在config文件下的
gengeratorConfig.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>
<context id="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3307/shopping"
userId="root"
password="1234">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="com.MrChengs.po"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator> <!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.MrChengs.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator> <!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.MrChengs.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator> <!-- 指定数据库表 -->
<table tableName="items"></table>
<table tableName="orders"></table>
<table tableName="orderdetail"></table>
<table tableName="user"></table>
</context>
</generatorConfiguration>
test目录下
GeneratorSqlmap.java
public class GeneratorSqlmap {
public void generator() throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//指定 逆向工程配置文件
File configFile = new File("config/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);
}
public static void main(String[] args) throws Exception {
try {
GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}
都是固定的格式,固体的可以参考官方的文档。
然后运行在
简单说明一下
如下的注释
首先是表
其次是表中的字段
2018-10-25 16:04:22,837 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Retrieving column information for table "items"
2018-10-25 16:04:22,848 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "id", data type 4, in table "shopping..items"
2018-10-25 16:04:22,849 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "name", data type 12, in table "shopping..items"
2018-10-25 16:04:22,849 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "price", data type 7, in table "shopping..items"
2018-10-25 16:04:22,849 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "detail", data type -1, in table "shopping..items"
2018-10-25 16:04:22,849 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "pic", data type 12, in table "shopping..items"
2018-10-25 16:04:22,849 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "createtime", data type 93, in table "shopping..items"
2018-10-25 16:04:22,855 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Retrieving column information for table "orders"
2018-10-25 16:04:22,859 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "id", data type 4, in table "shopping..orders"
2018-10-25 16:04:22,859 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "user_id", data type 4, in table "shopping..orders"
2018-10-25 16:04:22,859 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "number", data type 12, in table "shopping..orders"
2018-10-25 16:04:22,859 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "createtime", data type 93, in table "shopping..orders"
2018-10-25 16:04:22,859 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "note", data type 12, in table "shopping..orders"
2018-10-25 16:04:22,864 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Retrieving column information for table "orderdetail"
2018-10-25 16:04:22,868 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "id", data type 4, in table "shopping..orderdetail"
2018-10-25 16:04:22,868 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "orders_id", data type 4, in table "shopping..orderdetail"
2018-10-25 16:04:22,868 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "items_id", data type 4, in table "shopping..orderdetail"
2018-10-25 16:04:22,868 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "items_num", data type 4, in table "shopping..orderdetail"
2018-10-25 16:04:22,869 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Retrieving column information for table "user"
2018-10-25 16:04:22,874 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "id", data type 4, in table "shopping..user"
2018-10-25 16:04:22,874 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "username", data type 12, in table "shopping..user"
2018-10-25 16:04:22,874 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "birthday", data type 91, in table "shopping..user"
2018-10-25 16:04:22,874 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "sex", data type 1, in table "shopping..user"
2018-10-25 16:04:22,874 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "address", data type 12, in table "shopping..user"
 
刷新观察
com.MrChengs.po
com.MrCehngs.mapper
两个包里面的文件

 
此时的文件我们得到了,在使用的时候我们不要进行对原有的代码的修改。
 
 
 
 
 返回上一个博文将进行简单的小案例测试:
有不懂的可以参考:MyBatis整合Spring
 

复制图中的文件到本工程中

 
 
 
 新建测试类

public class test {
private ApplicationContext applicationContext; public ApplicationContext getApplication(){
applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
return applicationContext;
} //根据主键id进行查询
@Test
public void test() { ItemsMapper items = (ItemsMapper) getApplication().getBean("itemsMapper"); Items it = items.selectByPrimaryKey(1);
System.out.println(it.toString());
}
//自定义查询条件
@Test
public void test1() {
ItemsMapper itemsMapperems = (ItemsMapper) getApplication().getBean("itemsMapper"); ItemsExample itemsExample = new ItemsExample();
//通过criteria构造条件查询
ItemsExample.Criteria criteria = itemsExample.createCriteria();
criteria.andNameEqualTo("笔记本");
//可能返回多个记录
List<Items> items = itemsMapperems.selectByExample(itemsExample); System.out.println(items);
}
}

均可以测试成功!!
 
对于其他的方法,在使用的时候,可以参考自动生成的源码进行测试,都是可以测试成功的!
 
 
 逆向工程的使用极大的方便我们进行开发,作为程序员不需要过多的关注这个,我们进行了解基本的使用和用法即可。
其余的增删改除,可以进行测试,在不会使用的情况下,可以参考源码,源码是之前的前几章节的内容.
 
 
 
 
 
 
 
 
 
 

MyBatis(10)逆向工程的更多相关文章

  1. MyBatis框架——逆向工程

    什么是逆向工程? 逆向工程师MyBatis提供的一种自动化配置方案,针对数据表自动生成MyBatis所需的各种资源,包括实体类.Mapper接口.Mapper.xml,但是逆向工程的缺陷在于只能针对单 ...

  2. mybatis的逆向工程

    mybatis的逆向工程是很大的减少了程序员对代码的编写工作,由于mybatis是半自动的sql语句使用,我们在项目中一般都是采用逆向工程来生成mybatis的文件,mapper接口相当于我们平常所说 ...

  3. Mybatis(七) mybatis的逆向工程的配置详解

    还是觉得看书学习有意思~嘿嘿.今天把mybatis给结束掉. --WH 一.什么是逆向工程? 简单点说,就是通过数据库中的单表,自动生成java代码. Mybatis官方提供了逆向工程,可以针对单表自 ...

  4. Mybatis【逆向工程,缓存,代理】知识要点

    前言 本文主要讲解Mybatis的以下知识点: Mybatis缓存 一级缓存 二级缓存 与Ehcache整合 Mapper代理 使用Mapper代理就不用写实现类了 逆向工程 自动生成代码 Mybat ...

  5. Mybatis学习(七)————— mybatis的逆向工程的配置详解

    一.什么是逆向工程? 简单点说,就是通过数据库中的单表,自动生成java代码. Mybatis官方提供了逆向工程,可以针对单表自动生成mybatis代码(mapper.java\mapper.xml\ ...

  6. SpringBoot+Mybatis+Generator 逆向工程使用(二)

    Mybatis-Genarator 逆向工程使用 个人开发环境 java环境:Jdk1.8.0_60 编译器:IntelliJ IDEA 2017.1.4 mysql驱动:mysql-connecto ...

  7. 【MyBatis学习15】MyBatis的逆向工程生成代码

    1. 什么是逆向工程 mybatis的一个主要的特点就是需要程序员自己编写sql,那么如果表太多的话,难免会很麻烦,所以mybatis官方提供了一个逆向工程,可以针对单表自动生成mybatis执行所需 ...

  8. 创建mybatis的逆向工程

    1.mybatis的逆向工程(我使用的是maven仓库创建) 工作原理:反向工程(通过数据库中的表和字段信息去生成对应的增删改查方法) 其实就是一个自动生成工具 生成实体类(pojo)和映射文件(ma ...

  9. Mybatis的逆向工程以及Example的实例函数及详解

    Mybatis-generator是Mybatis的逆向工程  (根据数据库中的表生成java代码) Mybatis的逆向工程会生成实例及实例对应的example,example用于添加条件,相当于w ...

  10. Mybatis Generator逆向工程的使用

    一.在 idea 中使用 mybatis generator 逆向工程 1.在IDEA上创建maven工程. 2.在pom.xml中配置MyBatis逆向工程插件 <!--MyBatis自动生成 ...

随机推荐

  1. [转]How can I install the VS2017 version of msbuild on a build server without installing the IDE?

    本文转自:http://stackoverflow.com/questions/42696948/how-can-i-install-the-vs2017-version-of-msbuild-on- ...

  2. linux创建日期文件名

    linux创建文件名添加当前系统日期时间的方法 使用`date +%y%m%d` Example: mkdir `date +%y%m%d` tar cfvz /tmp/bak.`date +%y%m ...

  3. encodeURI和 encodeURIComponent 的作用及应用

    首先解释下 encodeURIComponent 的作用:将文本字符串编码为一个有效的统一资源标识符 (URI).为什么要用这个是因为我想把 username 整个当做参数传递给 CGI, 而不让 C ...

  4. 互联网轻量级框架SSM-查缺补漏第五天

    简言:这个地方我就草草过了,NBA圣诞大战,偷偷看比赛,真香~ 第五章映射器 5.2select元素 自动映射和驼峰映射:MyBatis提供了自动映射功能,在默认的情况下自动映射功能是开启的. 在se ...

  5. swoole安装

    转自:http://blog.csdn.net/u014207604/article/details/49926207 Windows 下安装 swoole 具体步骤: Swoole,原本不支持在Wi ...

  6. git的问题(error: object file .git/objects/* is empty...)的解决方案及对git版本库文件的了解

    由于操作不当,导致git版本库出了大问题,如下所示: error: object file .git/objects/8b/61d0135d3195966b443f6c73fb68466264c68e ...

  7. Hibernate 函数 ,子查询 和原生SQL查询

    一. 函数 聚合函数:count(),avg(),sum(),min(),max() 例:(1)查询Dept表中的所有的记录条数. String hql=" select count(*) ...

  8. SpringMVC学习(五)——拦截器示例

    部分内容摘自开涛的<跟我学SpringMVC.PDF> 拦截器,本质类似于AOP,主要的应用场景: 1.日志记录:记录请求信息的日志,以便进行信息监控.信息统计.计算PV等. 2.权限检查 ...

  9. html5自我总结

    2017年7月30日 合抱之木,生于毫末.九层之台,起于累土.软件行业要熟记和训练的东西有很多,在此,写一下如何快速搭建html及自我见解(这里只介绍我自己用到的,还有部分存在但是用不到的就不讲解了) ...

  10. SSIS ->> 管理和维护SSISDB

    Comming soon!!! 参考文献: Setup and Performance Issues with the Integration Services (SSIS) 2012 Catalog ...