MyBatis(10)逆向工程
- From the command prompt with an XML configuration
- As an Ant task with an XML configuration
- As a Maven Plugin
- From another Java program with an XML configuration
- From another Java program with a Java based configuration

官方文档在解压后的doc文件中点击index.html
简单的讲解一下:
MyBatis GeneratorXML Configuration File Reference

<?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>
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"


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

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)逆向工程的更多相关文章
- MyBatis框架——逆向工程
什么是逆向工程? 逆向工程师MyBatis提供的一种自动化配置方案,针对数据表自动生成MyBatis所需的各种资源,包括实体类.Mapper接口.Mapper.xml,但是逆向工程的缺陷在于只能针对单 ...
- mybatis的逆向工程
mybatis的逆向工程是很大的减少了程序员对代码的编写工作,由于mybatis是半自动的sql语句使用,我们在项目中一般都是采用逆向工程来生成mybatis的文件,mapper接口相当于我们平常所说 ...
- Mybatis(七) mybatis的逆向工程的配置详解
还是觉得看书学习有意思~嘿嘿.今天把mybatis给结束掉. --WH 一.什么是逆向工程? 简单点说,就是通过数据库中的单表,自动生成java代码. Mybatis官方提供了逆向工程,可以针对单表自 ...
- Mybatis【逆向工程,缓存,代理】知识要点
前言 本文主要讲解Mybatis的以下知识点: Mybatis缓存 一级缓存 二级缓存 与Ehcache整合 Mapper代理 使用Mapper代理就不用写实现类了 逆向工程 自动生成代码 Mybat ...
- Mybatis学习(七)————— mybatis的逆向工程的配置详解
一.什么是逆向工程? 简单点说,就是通过数据库中的单表,自动生成java代码. Mybatis官方提供了逆向工程,可以针对单表自动生成mybatis代码(mapper.java\mapper.xml\ ...
- SpringBoot+Mybatis+Generator 逆向工程使用(二)
Mybatis-Genarator 逆向工程使用 个人开发环境 java环境:Jdk1.8.0_60 编译器:IntelliJ IDEA 2017.1.4 mysql驱动:mysql-connecto ...
- 【MyBatis学习15】MyBatis的逆向工程生成代码
1. 什么是逆向工程 mybatis的一个主要的特点就是需要程序员自己编写sql,那么如果表太多的话,难免会很麻烦,所以mybatis官方提供了一个逆向工程,可以针对单表自动生成mybatis执行所需 ...
- 创建mybatis的逆向工程
1.mybatis的逆向工程(我使用的是maven仓库创建) 工作原理:反向工程(通过数据库中的表和字段信息去生成对应的增删改查方法) 其实就是一个自动生成工具 生成实体类(pojo)和映射文件(ma ...
- Mybatis的逆向工程以及Example的实例函数及详解
Mybatis-generator是Mybatis的逆向工程 (根据数据库中的表生成java代码) Mybatis的逆向工程会生成实例及实例对应的example,example用于添加条件,相当于w ...
- Mybatis Generator逆向工程的使用
一.在 idea 中使用 mybatis generator 逆向工程 1.在IDEA上创建maven工程. 2.在pom.xml中配置MyBatis逆向工程插件 <!--MyBatis自动生成 ...
随机推荐
- Make sure that the controller has a parameterless public constructor.
An error occurred when trying to create a controller of type 'CCD.Web.Controllers.TWAccountControlle ...
- .NET Unity IOC框架使用实例
1.IOC简介 IOC(Inversion of Control), 控制反转 DI (Dependency Injection),依赖注入 IOC的基本概念是:不创建对象,但是描述创建它们的方式.在 ...
- 用 Redis Desktop Manager 远程连接 redis 数据库。
环境: 本机OS:window 10(本机没有安装redis) redis 服务器:centos 7 使用 Redis Desktop Manager 工具远程连接 redis. Redis Desk ...
- gc原理小结
一.相关概念 基本回收算法 1. 引用计数(Reference Counting) 比较古老的回收算法.原理是此对象有一个引用,即增加一个计数,删除一个引用则减少一个计数.垃圾回收时,只用收集计数为0 ...
- RegExp.prototype.exec()使用技巧
RegExp.prototype.exec() exec() 方法在一个指定字符串中执行一个搜索匹配.返回一个结果数组或 null. 如果你只是为了判断是否匹配(true或 false),可以使用 R ...
- VC++ IPv6的支持
最近根据项目需要,要在产品中添加对IpV6的支持,因此研究了一下IPV6的相关内容,Ipv6 与原来最直观的改变就是地址结构的改变,IP地址由原来的32位扩展为128,这样原来的地址结构肯定就不够用了 ...
- sql: TRIGGER
--Common Table Expressions(CTE) WITH HighSample (SampleId,SampleTitle,SampleContent) AS ( SELECT Sam ...
- csharp:Optical Character Recognition
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- 配置centos7 网卡
进入root模式,输入 cd /etc/sysconfig/network-scripts/ 按Tab键查看网卡配置文件名称,然后进入编辑: 如: cd /etc/sysconfig/network- ...
- 从零开始的全栈工程师——html篇1.5
列表与边距探讨和行块 一.列表 1.无序列表(UL) 1)内部必须有子标签<li></li>2)天生自带内外边距 p也是自带 大家会发现用UL的时候内容前面会出现一个像这样的一 ...