SpringBoot整合Mybatis-plus代码生成器(代码直接可用)

2023-05-16

前言

      在做SpringBoot项目时,因为要对数据库中各表进行增删改查操作,而表的数量又相对较多,故而需要进行较多的controller,mapper,service以及实体类的创建,工作重复而枯燥,还容易出错。

      Mybatis 为我们提供了强大的代码生成:MybatisGenerator。通过简单的配置,我们就可以直接生成所需controller,mapper,service以及实体类文件等。

      本文方法是基于Mybatis-plus的代码生成器,无需配置xml文件,直接执行代码即可。

一. 创建SpringBoot项目

 

二. 引入依赖

  <dependencies>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
    <!--web 依赖-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--mybatis-plus 依赖-->
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>3.3.1.tmp</version>
    </dependency>
    <!--mybatis-plus 代码生成器依赖-->
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-generator</artifactId>
      <version>3.3.1.tmp</version>
    </dependency>
    <!--freemarker 依赖-->
    <dependency>
      <groupId>org.freemarker</groupId>
      <artifactId>freemarker</artifactId>
    </dependency>
    <!--mysql 依赖-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
    </dependency>
    <!-- swagger2 依赖 -->
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>2.7.0</version>
    </dependency>
    <!-- Swagger第三方ui依赖 -->
    <dependency>
      <groupId>com.github.xiaoymin</groupId>
      <artifactId>swagger-bootstrap-ui</artifactId>
      <version>1.9.6</version>
    </dependency>
  </dependencies>
</project>

三.  添加代码生成器

public class CodeGenerator {
	/**
	 * <p>
	 * 读取控制台内容
	 * </p>
	 */
	public static String scanner(String tip) {
		Scanner scanner = new Scanner(System.in);
		StringBuilder help = new StringBuilder();
		help.append("请输入" + tip + ":");
		System.out.println(help.toString());
		if (scanner.hasNext()) {
			String ipt = scanner.next();
			if (StringUtils.isNotEmpty(ipt)) {
				return ipt;
			}
		}
		throw new MybatisPlusException("请输入正确的" + tip + "!");
	}

	public static void main(String[] args) {
		// 代码生成器
		AutoGenerator mpg = new AutoGenerator();

		// 全局配置
		GlobalConfig gc = new GlobalConfig();
		String projectPath = System.getProperty("user.dir");
		gc.setOutputDir(projectPath + "/yeb-generator/src/main/java");
		//作者
		gc.setAuthor("zs");
		//打开输出目录
		gc.setOpen(false);
		//xml开启 BaseResultMap
		gc.setBaseResultMap(true);
		//xml 开启BaseColumnList
		gc.setBaseColumnList(true);
		// 实体属性 Swagger2 注解
		gc.setSwagger2(true);
		mpg.setGlobalConfig(gc);

		// 数据源配置
		DataSourceConfig dsc = new DataSourceConfig();
		dsc.setUrl("jdbc:mysql://localhost:3306/yeb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia" +
				"/Shanghai");
		dsc.setDriverName("com.mysql.cj.jdbc.Driver");
		dsc.setUsername("root");
		dsc.setPassword("root");
		mpg.setDataSource(dsc);

		// 包配置
		PackageConfig pc = new PackageConfig();
		pc.setParent("com.xxxx")
				.setEntity("pojo")
				.setMapper("mapper")
				.setService("service")
				.setServiceImpl("service.impl")
				.setController("controller");
		mpg.setPackageInfo(pc);

		// 自定义配置
		InjectionConfig cfg = new InjectionConfig() {
			@Override
			public void initMap() {
				// to do nothing
			}
		};

		// 如果模板引擎是 freemarker
		String templatePath = "/templates/mapper.xml.ftl";
		// 如果模板引擎是 velocity
		// String templatePath = "/templates/mapper.xml.vm";

		// 自定义输出配置
		List<FileOutConfig> focList = new ArrayList<>();
		// 自定义配置会被优先输出
		focList.add(new FileOutConfig(templatePath) {
			@Override
			public String outputFile(TableInfo tableInfo) {
				// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
				return projectPath + "/yeb-generator/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper"
						+ StringPool.DOT_XML;
			}
		});
		cfg.setFileOutConfigList(focList);
		mpg.setCfg(cfg);

		// 配置模板
		TemplateConfig templateConfig = new TemplateConfig();

		templateConfig.setXml(null);
		mpg.setTemplate(templateConfig);

		// 策略配置
		StrategyConfig strategy = new StrategyConfig();
		//数据库表映射到实体的命名策略
		strategy.setNaming(NamingStrategy.underline_to_camel);
		//数据库表字段映射到实体的命名策略
		strategy.setColumnNaming(NamingStrategy.no_change);
		//lombok模型
		strategy.setEntityLombokModel(true);
		//生成 @RestController 控制器
		strategy.setRestControllerStyle(true);
		strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
		strategy.setControllerMappingHyphenStyle(true);
		//表前缀
		strategy.setTablePrefix("t_");
		mpg.setStrategy(strategy);
		mpg.setTemplateEngine(new FreemarkerTemplateEngine());
		mpg.execute();
	}

}

代码含有注释,请按照注释根据自身情况修改代码

四、执行代码生成器

按要求输入所连数据库中的各表表名

 五. 查看结果

成功生成所需controller,mapper,service以及实体类文件(同时配置了swagger)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SpringBoot整合Mybatis-plus代码生成器(代码直接可用) 的相关文章

随机推荐

  • Django(73)django-debug-toolbar调试工具

    介绍 Django框架的调试工具栏使用django debug toolbar库 xff0c 是一组可配置的面板 xff0c 显示有关当前请求 响应的各种调试信息 xff0c 点击时 xff0c 显示有关面板内容的更多详细信息 应用 1 安
  • mac环境下 解决“Conda command not found”

    在终端中输入 xff1a conda version gt 查看安装的conda版本 此时 xff0c 出现Conda command not found xff0c 说明需要配置环境变量 xff0c 具体操作如下 xff1a vim ba
  • 风控模型算法

    目录 1 蚂蚁金服2 陆金所3 京东金融4 苏宁金融5 百度金融6 腾讯理财通7 宜信8 钱大掌柜9 万达金融10 网易理财11 美团金融 主要是整理目前市面上的风控模型以及风控算法 xff08 不间断更新中 xff09 1 蚂蚁金服 xf
  • 两个List对象,指定属性,取差集、交集。

    文章目录 差集交集过滤实体类测试类打印效果 差集 span class token comment 差集 list1 list2 61 list1 中不同数据 span span class token class name List sp
  • linux 目录说明

    项目价格 bin存放二进制可执行文件 ls cat mkdir等 xff0c 常用命令一般都在这里 etc存放系统管理和配置文件 home存放所有用户文件的根目录 xff0c 是用户主目录的基点 xff0c 比如用户user的主目录就是 u
  • 辗转相除法的原理

    辗转相除法是求最大公约数的一种方法 它的具体做法是 xff1a 用较小数除较大数 xff0c 再用出现的余数 xff08 第一余数 xff09 去除除数 xff0c 再用出现的余数 xff08 第二余数 xff09 去除第一余数 xff0c
  • P2078 朋友--并查集--接上章--sabrindol--Sabrina

    P2078 朋友 并查集模板题 传送门 题目背景 小明在A公司工作 xff0c 小红在B公司工作 题目描述 这两个公司的员工有一个特点 xff1a 一个公司的员工都是同性 A公司有N名员工 xff0c 其中有P对朋友关系 B公司有M名员工
  • Springboot整合myBatis(附加pagehelper分页插件)

    Springboot整合myBatis xff08 附加pagehelper分页插件 xff09 前提第一步 xff0c 创建spring boot xff0c 导入maven依赖第二步 xff0c 进行appliction配置第三步 xf
  • 【linux免费安装ssl证书,自动续费,享受https】

    为了在 CentOS 上为您的域名安装免费的 SSL 证书并启用 HTTPS xff0c 您可以使用 Let s Encrypt 提供的免费证书 下面是使用 Certbot 工具在 CentOS 系统上为您的域名安装和配置 Let s En
  • VS2017修改代码编码格式为utf-8

    VS2017修改代码编码格式为utf 8 对于国内用户来说 xff0c 大多设置Windows操作系统语言为简体中文 编码为GBK或GB2312 xff0c 由此导致Visual Studio2017默认采用GBK GB2312编码格式 x
  • 【c++】string字符串拼接的三种方式

    span class token macro property span class token directive keyword include span span class token string lt iostream gt s
  • 【单调栈】最大矩形

    问题描述 给一个直方图 xff0c 求直方图中的最大矩形的面积 例如 xff0c 下面这个图片中直方图的高度从左到右分别是2 1 4 5 1 3 3 他们的宽都是1 xff0c 其中最大的矩形是阴影部分 Input amp Output I
  • 1002 A+B for Polynomials

    span class token macro property span class token directive keyword include span span class token string lt stdio h gt sp
  • Vision-based Vehicle Speed Estimation: A Survey

    该文是对 Vision based Vehicle Speed Estimation A Survey 的翻译 xff0c 为了个人记录学习 目录 摘要1 引言2 概念与术语2 1 输入数据2 2 检测与追踪2 3 距离和速度估计2 4 应
  • 【洛谷训练】阶乘数码

    题目链接 xff1a 阶乘数码 对于这道题 xff0c 一开始我没有理解题目的意思 xff0c 后来看了题解才明白是求5 xff01 61 120的结果中含有几个2 明白题意后 xff0c 想到了要结合之前做过的一道题 xff0c 阶乘之和
  • JAVA调用天气获取接口

    JAVA调用天气获取接口 xff08 百度天气API 高德天气API xff09 解决方法可以看第四部分一 需求 xff1a 在Java项目中需要获取天气情况 xff1b 二 开发环境 xff1a idea三 试错与学习1 1 百度天气AP
  • 洛谷 P1002 过河卒

    题目描述 棋盘上 A 点有一个过河卒 xff0c 需要走到目标 B 点 卒行走的规则 xff1a 可以向下 或者向右 同时在棋盘上 C 点有一个对方的马 xff0c 该马所在的点和所有跳跃一步可达的点称为对方马的控制点 因此称之为 马拦过河
  • SQL查询数据

    1 创建新的用户并授权 xff1a span class token keyword create span span class token keyword user span cc identified span class token
  • Windows10下Linux子系统Ubuntu使用教程——安装图形界面

    一 准备 在Ubuntu中执行以下操作 xff1a 1 安装xorg sudo apt span class token operator span get install xorg 2 安装xfce4 sudo apt span clas
  • SpringBoot整合Mybatis-plus代码生成器(代码直接可用)

    前言 在做SpringBoot项目时 因为要对数据库中各表进行增删改查操作 xff0c 而表的数量又相对较多 xff0c 故而需要进行较多的controller xff0c mapper xff0c service以及实体类的创建 xff0