1.官网

image.png

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

  1. <dependencies>
  2. <dependency>
  3. <groupId>mysql</groupId>
  4. <artifactId>mysql-connector-java</artifactId>
  5. <version>5.1.41</version>
  6. </dependency>
  7. <!--mybatis-->
  8. <dependency>
  9. <groupId>org.mybatis.spring.boot</groupId>
  10. <artifactId>mybatis-spring-boot-starter</artifactId>
  11. <version>1.3.1</version>
  12. </dependency>
  13. <!--mapper-->
  14. <dependency>
  15. <groupId>tk.mybatis</groupId>
  16. <artifactId>mapper-spring-boot-starter</artifactId>
  17. <version>1.2.4</version>
  18. </dependency>
  19. <!-- mybatis 逆向生成工具 -->
  20. <dependency>
  21. <groupId>org.mybatis.generator</groupId>
  22. <artifactId>mybatis-generator-core</artifactId>
  23. <version>1.3.2</version>
  24. <scope>compile</scope>
  25. <optional>true</optional>
  26. </dependency>
  27. </dependencies>

2.3 创建文件

1. generatorConfig.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration
  3. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  4. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  5. <generatorConfiguration>
  6. <context id="MysqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
  7. <property name="beginningDelimiter" value="`"/>
  8. <property name="endingDelimiter" value="`"/>
  9. <!-- 通用mapper所在目录 -->
  10. <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
  11. <property name="mappers" value="com.lv.my.mapper.MyMapper"/>
  12. </plugin>
  13. <jdbcConnection driverClass="com.mysql.jdbc.Driver"
  14. connectionURL="jdbc:mysql://localhost:3306/foodie-shop"
  15. userId="root"
  16. password="root">
  17. </jdbcConnection>
  18. <!-- 对应生成的pojo所在包 -->
  19. <javaModelGenerator targetPackage="com.lv.pojo" targetProject="src/main/java"/>
  20. <!-- 对应生成的mapper所在目录 -->
  21. <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>
  22. <!-- 配置mapper对应的java映射 -->
  23. <javaClientGenerator targetPackage="com.lv.mapper" targetProject="src/main/java" type="XMLMAPPER"/>
  24. <!-- 数据库表 -->
  25. <table tableName="carousel"></table>
  26. <table tableName="category"></table>
  27. <table tableName="items"></table>
  28. <table tableName="items_comments"></table>
  29. <table tableName="items_img"></table>
  30. <table tableName="items_param"></table>
  31. <table tableName="items_spec"></table>
  32. <table tableName="order_items"></table>
  33. <table tableName="order_status"></table>
  34. <table tableName="orders"></table>
  35. <table tableName="user_address"></table>
  36. <table tableName="users"></table>
  37. </context>
  38. </generatorConfiguration>

2. 创建通用mapper为MyMapper类,生成的mapper都会继承这个通用mapper

image.png
image.png
MyMapper类

  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2014-2016 abel533@gmail.com
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package com.lv.my.mapper;
  25. import tk.mybatis.mapper.common.Mapper;
  26. import tk.mybatis.mapper.common.MySqlMapper;
  27. /**
  28. * 继承自己的MyMapper
  29. */
  30. public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {
  31. }

3. 启动类GeneratorDisplay

  1. import org.mybatis.generator.api.MyBatisGenerator;
  2. import org.mybatis.generator.config.Configuration;
  3. import org.mybatis.generator.config.xml.ConfigurationParser;
  4. import org.mybatis.generator.internal.DefaultShellCallback;
  5. import java.io.File;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. public class GeneratorDisplay {
  9. public void generator() throws Exception {
  10. List<String> warnings = new ArrayList<String>();
  11. boolean overwrite = true;
  12. //指定 逆向工程配置文件
  13. File configFile = new File("generatorConfig.xml");
  14. ConfigurationParser cp = new ConfigurationParser(warnings);
  15. Configuration config = cp.parseConfiguration(configFile);
  16. DefaultShellCallback callback = new DefaultShellCallback(overwrite);
  17. MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
  18. callback, warnings);
  19. myBatisGenerator.generate(null);
  20. }
  21. public static void main(String[] args) throws Exception {
  22. try {
  23. GeneratorDisplay generatorSqlmap = new GeneratorDisplay();
  24. generatorSqlmap.generator();
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. }

2.3 对应修改的地方

image.png

3. 主项目中步骤

3.1 在父项目中补充pom依赖

image.png

  1. <!-- 通用mapper逆向工具 -->
  2. <dependency>
  3. <groupId>tk.mybatis</groupId>
  4. <artifactId>mapper-spring-boot-starter</artifactId>
  5. <version>2.1.5</version>
  6. </dependency>

3.2 生成的将项目复制到主项目中(包括MyMapper接口类)

image.png

3.3 在yml文件中引入通用mapper配置

image.png

  1. ############################################################
  2. #
  3. # mybatis mapper 配置
  4. #
  5. ############################################################
  6. # 通用 Mapper 配置
  7. mapper:
  8. mappers: com.lv.my.mapper.MyMapper # 需要创建此路径
  9. not-empty: false # 在进行数据库操作的时候,判断表达式 username!=null 是否会追加 username!=''
  10. identity: MYSQL

注意:mapper.mapper需要手动创建此层级,并拷贝MyMapper.java(逆向生成工具中的文件)到此路径中

4. 运行检验

image.png
先打开日志
image.png
重新install
image.png