如果这篇文章能给你带来帮助 不胜荣幸,如果有不同的意见也欢迎批评指正,废话不多说直接上代码。(参考文档:https://www.cnblogs.com/kibana/p/8930248.html)

  第一既然是逆向工程 就需要加载一定的maven依赖:

	<!-- mybatis-generator-core 反向生成java代码-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>

  有的人还喜欢在后面加上这两行代码(我也加上了,只是不是很懂,但是不影响正常使用):

<!-- mybatis-generator  插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>

然后你需要在resource这里通常是用来放配置文件的地方来建一个配置文件 mybatis-generator.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">
<!-- mybatis逆向生成xml配置 -->
<generatorConfiguration>
<properties resource="application.properties" /> <!-- 数据库连接配置文件 -->
<context id="sqlserverTables" targetRuntime="MyBatis3">
<!-- 生成的pojo,将implements Serializable-->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator> <!-- 数据库链接URL、用户名、密码(这个就是你的spring boot项目自带的那个配置文件里面的数据库的配置) -->
    
<jdbcConnection driverClass="${spring.datasource.driverClassName}"
connectionURL="${spring.datasource.url}"
userId="${spring.datasource.username}"
password="${spring.datasource.password}">
</jdbcConnection> <!--
默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer
true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal
-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver> <!--
生成model模型,对应的包路径,以及文件存放路径(targetProject),targetProject可以指定具体的路径,如./src/main/java,
也可以使用“MAVEN”来自动生成,这样生成的代码会在target/generatord-source目录下
       (通俗的讲就是你想要把生成的实体类的放到哪里)
-->
<!--<javaModelGenerator targetPackage="com.joey.mybaties.test.pojo" targetProject="MAVEN">-->
<javaModelGenerator targetPackage="com.example.demo.bean" targetProject="./src/main/java">
<property name="enableSubPackages" value="true"/>
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator> <!--对应的mapper.xml文件(通俗的讲就是你要把mapper.xml文件放到什么地方去,我是放到resource下一个名叫mappers的文件夹里面了) -->
<sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator> <!-- 对应的Mapper接口类文件 (通俗的讲就是你要生成的dao层mapper接口的地方 需要根据自己的文件进行配置) -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.dao" targetProject="./src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator> <!-- 列出要生成代码的所有表,这里配置的是不生成Example文件 -->

    <!-- 这个地方呢 也是你需要自动修改的地方 第一个参数是你数据库的表名 第二个参数就是想要生成实体类的名称 -->
<table tableName="product" domainObjectName="Product" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" >
<property name="useActualColumnNames" value="false"/>
</table>
</context>
</generatorConfiguration>

  这个时候有的人可能就要问了,springboot不是基本上不用写配置文件的吗?为什么又要使用配置文件的了?其实springboot不是不使用配置文件,而是可以把配置文件弄成一个类的形式来实现功能。

最后还要再写上一个启动类:

package com.example.demo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.springframework.util.ResourceUtils;
/**************************************
* 类说明:
* mybatis逆向工程main函数
***************************************
*/
public class GenMain {
public static void main(String[] args) throws FileNotFoundException {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//如果这里出现空指针,直接写绝对路径即可。

    //这个地方是一个非常大的重点 你的逆向工程的配置文件的地址 为了确定可以写成绝对的 具体的 就是点击配置文件 右击最后一个就看见了 ctrl c+v 大法来完成

String genCfg = "D:\\workspace\\wechat\\src\\main\\resources\\mybatis-generator.xml";
// File configFile = new File(GenMain.class.getResource(genCfg).getFile()); //获取路径出错
File configFile = ResourceUtils.getFile(genCfg);
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = null;
try {
config = cp.parseConfiguration(configFile);
} catch (IOException e) {
e.printStackTrace();
} catch (XMLParserException e) {
e.printStackTrace();
}
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = null;
try {
myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
} catch (InvalidConfigurationException e) {
e.printStackTrace();
}
try {
myBatisGenerator.generate(null);
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

  完成了之后呢,直接运行这个启动类就可以了!一般我是把这个启动类放到跟spring boot启动类的同级目录下,这样感觉都是启动类比较方便,,最后运行完了之后呢,刷新项目就可以了。我为了大家能够看懂(因为很多人跟我一样都是上学时基础没打牢,都进行了一些通俗的解释,感觉人家原文章写的比较好,就经行了简单的说明,非常好用)。

springboot中实现逆向工程的更多相关文章

  1. springboot中mybatis逆向工程与分页的应用

    最近在项目中应用到springboot与mybatis,在进行整合过程中遇到一些坑,在此将其整理出来,便于以后查阅与复习. 项目运行环境为:eclispe+jdk1.8+maven 一.搭建Sprin ...

  2. springboot实现mybaitis逆向工程

    springboot实现mybaitis逆向工程 首先引入依赖,一共需要两个依赖(一个是mybaits依赖,一个是mybatis逆向工程插件) <dependency> <group ...

  3. SpringBoot中yaml配置对象

    转载请在页首注明作者与出处 一:前言 YAML可以代替传统的xx.properties文件,但是它支持声明map,数组,list,字符串,boolean值,数值,NULL,日期,基本满足开发过程中的所 ...

  4. 如何在SpringBoot中使用JSP ?但强烈不推荐,果断改Themeleaf吧

    做WEB项目,一定都用过JSP这个大牌.Spring MVC里面也可以很方便的将JSP与一个View关联起来,使用还是非常方便的.当你从一个传统的Spring MVC项目转入一个Spring Boot ...

  5. springboot中swaggerUI的使用

    demo地址:demo-swagger-springboot springboot中swaggerUI的使用 1.pom文件中添加swagger依赖 2.从github项目中下载swaggerUI 然 ...

  6. spring-boot+mybatis开发实战:如何在spring-boot中使用myabtis持久层框架

    前言: 本项目基于maven构建,使用mybatis-spring-boot作为spring-boot项目的持久层框架 spring-boot中使用mybatis持久层框架与原spring项目使用方式 ...

  7. 由浅入深学习springboot中使用redis

    很多时候,我们会在springboot中配置redis,但是就那么几个配置就配好了,没办法知道为什么,这里就详细的讲解一下 这里假设已经成功创建了一个springboot项目. redis连接工厂类 ...

  8. Springboot中使用AOP统一处理Web请求日志

    title: Springboot中使用AOP统一处理Web请求日志 date: 2017-04-26 16:30:48 tags: ['Spring Boot','AOP'] categories: ...

  9. SpringBoot 中常用注解

    本篇博文将介绍几种SpringBoot 中常用注解 其中,各注解的作用为: @PathVaribale 获取url中的数据 @RequestParam 获取请求参数的值 @GetMapping 组合注 ...

  10. SpringBoot中关于Mybatis使用的三个问题

    SpringBoot中关于Mybatis使用的三个问题 转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/8495453.html 原本是要讲讲PostgreSQL ...

随机推荐

  1. S2-052 CVE-2017-9805 远程代码执行

    漏洞名称 S2-052 CVE-2017-9805 远程代码执行 利用条件 Struts 2.1.6 - Struts 2.3.33 Struts 2.5 - Struts 2.5.12 漏洞原理 S ...

  2. SpringBoot+Mybatis-plus整合easyExcel批量导入Excel到数据库+导出Excel

    一.前言 今天小编带大家一起整合一下easyExcel,之所以用这个,是因为easyExcel性能比较好,不会报OOM! 市面上常见的导入导出Excel分为三种: hutool easyExcel p ...

  3. finally代码块-多异常的捕获处理

    finally代码块 finally :有一些特定的代码无论异常是否发生,都需要执行.另外,因为异常会引发程序跳转,导致有些语句执行不到.而finally就是解决这个问题的,在finally代码块中存 ...

  4. 明解STM32—GPIO理论基础知识篇之基本结构

    ​ 一.前言 万物皆有源头,大家学习单片机的源头操作就是通过GPIO口点灯,GPIO作为STM32最基础的外设,也是大家最先接触的外设.当然,看似基础的GPIO,不仅仅是简单的设置好IO口,让灯亮起就 ...

  5. 关于Mysql外键从新学习

    关于Mysql外键从新学习 参考:https://blog.csdn.net/u010373419/article/details/9321331 说实话,这是一个抄剩饭的文档. 为什么会从新学习外键 ...

  6. Vue.config.js配置 最新可用版本

    最近 在学前端,然后,学了这个vue-cli脚手架,虽然,我这个vue-cli还不算入门,后我会把这个笔记补上 下面是我的Vue.config.js的配置,我感觉这个复用的程度高,所以记下 了这个随笔 ...

  7. vuluhub_jangow-01-1.0.1

    前言 靶机:jangow-01-1.0.1 攻击机:kali linux2022.4 靶机描述 打靶ing 靶机探测 使用nmap扫描网段 点击查看代码 ┌──(root㉿kali)-[/home/k ...

  8. 罗姆BU32107EFV缩写词

    ADC Analog-to-Digital Converter Att Attenuation 衰减 AMix Analog Mixing AVol Analog Volume BPF Band-Pa ...

  9. odoo 为可编辑列表视图字段搜索添加查询过滤条件

    实践环境 Odoo 14.0-20221212 (Community Edition) 需求描述 如下图,列表网仓记录详情页面(form视图),编辑内联视图中的货主记录,为货主和仓库字段搜索,添加过滤 ...

  10. JZOJ 5347. 【NOIP2017提高A组模拟9.5】遥远的金字塔

    题目 分析 毫无疑问 \(dp\) 设 \(f_{i,j}\) 表示选到第 \(i\) 层,已选 \(j\) 个矩阵最大覆盖面积 那么 \(f_{i,j}=\max{f_{l,j}+w_i*(i-l) ...