1.官网
2.使用Mavan插件生成
将压缩包使用idea打开,打开根目录generatorConfig.xml文件,配置此文件内容
1、修改数据库连接信息(端口、数据库名、账号及密码)
2、填写对应pojo包路径,并创建相应路径的包
3、填写对应mapper所在路径,并创建相应路径的包
4、配置mapper对应的xml映射文件,并创建相应路径的包
5、填写数据库表名称
6、打开路径src-main-java-com-imooc-mybatis-utils-GeneratorDispory,
右键执行即可生成相应的pojo文件、mapper文件及映射xml文件,将其拷贝到自己的项目中即可
直接下载 mybatis-generator-for-imooc.java修改为zip
转载【https://www.yuque.com/yiqianshuguang/ugvad4/adyexb】
2.1 创建一个新的工程 mybatis_generator_xx
2.2 引入相关pom
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!--mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.2.4</version>
</dependency>
<!-- mybatis 逆向生成工具 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
</dependencies>
2.3 创建文件
1. 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>
<context id="MysqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!-- 通用mapper所在目录 -->
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="com.lv.my.mapper.MyMapper"/>
</plugin>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/foodie-shop"
userId="root"
password="root">
</jdbcConnection>
<!-- 对应生成的pojo所在包 -->
<javaModelGenerator targetPackage="com.lv.pojo" targetProject="src/main/java"/>
<!-- 对应生成的mapper所在目录 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>
<!-- 配置mapper对应的java映射 -->
<javaClientGenerator targetPackage="com.lv.mapper" targetProject="src/main/java" type="XMLMAPPER"/>
<!-- 数据库表 -->
<table tableName="carousel"></table>
<table tableName="category"></table>
<table tableName="items"></table>
<table tableName="items_comments"></table>
<table tableName="items_img"></table>
<table tableName="items_param"></table>
<table tableName="items_spec"></table>
<table tableName="order_items"></table>
<table tableName="order_status"></table>
<table tableName="orders"></table>
<table tableName="user_address"></table>
<table tableName="users"></table>
</context>
</generatorConfiguration>
2. 创建通用mapper为MyMapper类,生成的mapper都会继承这个通用mapper
MyMapper类
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2016 abel533@gmail.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.lv.my.mapper;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
/**
* 继承自己的MyMapper
*/
public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {
}
3. 启动类GeneratorDisplay
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class GeneratorDisplay {
public void generator() 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);
}
public static void main(String[] args) throws Exception {
try {
GeneratorDisplay generatorSqlmap = new GeneratorDisplay();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.3 对应修改的地方
3. 主项目中步骤
3.1 在父项目中补充pom依赖
<!-- 通用mapper逆向工具 -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
3.2 生成的将项目复制到主项目中(包括MyMapper接口类)
3.3 在yml文件中引入通用mapper配置
############################################################
#
# mybatis mapper 配置
#
############################################################
# 通用 Mapper 配置
mapper:
mappers: com.lv.my.mapper.MyMapper # 需要创建此路径
not-empty: false # 在进行数据库操作的时候,判断表达式 username!=null 是否会追加 username!=''
identity: MYSQL
注意:mapper.mapper需要手动创建此层级,并拷贝MyMapper.java(逆向生成工具中的文件)到此路径中
4. 运行检验
先打开日志
重新install