springboot项目基于mybatis-plus创建逆向工程
pom 依赖
<!--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>
---------------------------------------------------------------
模板生成代码
package com.woniuxy.generator;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* 执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
*
* @author ykq
* @since 1.0.0
*/
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 + "/backmanager-server/src/main/java");
//作者
gc.setAuthor("yuankaiqiang");
//打开输出目录
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.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent("com.woniuxy")
.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 + "/backmanager-server/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();
}
}
springboot项目基于mybatis-plus创建逆向工程的更多相关文章
- JAVA - SpringBoot项目引用MyBatis操作数据库
JAVA - SpringBoot项目引用MyBatis操作数据库 1. 创建SpringBoot项目,参考:https://www.cnblogs.com/1285026182YUAN/p/1232 ...
- SpringBoot项目的几种创建方式,启动、和访问
最常用的4种方式,但除了这些以外,还有其他方式: ①在线创建 ②STS构建 ③Intell Idea内置构建工具 ④Maven创建 STS官网:https://start.spring.io .S ...
- springboot项目整合mybatis
记录创建springboot项目并配置mybatis中间件: 资源准备及版本说明 编程工具:IDEA JDK版本:1.8 Maven版本:Apache Maven 3.6.3 springboot版本 ...
- 从零开始的SpringBoot项目 ( 二 ) 使用IDEA创建一个SpringBoot项目
工欲善其事 , 必先利其器 . IntelliJ IDEA 2019.3.3 x64的安装与破解 下面详细说明下如何使用idea创建我们的第一个springboot项目: 首先打开idea主界面选择 ...
- Springboot项目启动后自动创建多表关联的数据库与表的方案
文/朱季谦 在一些项目开发当中,存在这样一种需求,即开发完成的项目,在第一次部署启动时,需能自行构建系统需要的数据库及其对应的数据库表. 若要解决这类需求,其实现在已有不少开源框架都能实现自动生成数据 ...
- 从零开始的SpringBoot项目 ( 四 ) 整合mybatis
一.创建一个SpringBoot项目 从零开始的SpringBoot项目 ( 二 ) 使用IDEA创建一个SpringBoot项目 二.引入相关依赖 <!--mysql数据库驱动--> & ...
- 解决SpringBoot项目创建缓慢问题
SpringBoot项目构建缓慢 快速创建springboot项目 在创建一个springboot项目的时候,往往速度会很慢,原因是下载springboot文件的默认地址是springboot官网(国 ...
- 【SpringBoot】01.创建Springboot项目及启动器
创建Springboot项目及启动器 1.创建一个简单maven项目 SpringBoot2.0以下需要使用JDK1.7 ,2.0以上使用JDK1.8 如果需要修改JDK的版本需要打开pom文件: & ...
- 在springboot中使用Mybatis Generator的两种方式
介绍 Mybatis Generator(MBG)是Mybatis的一个代码生成工具.MBG解决了对数据库操作有最大影响的一些CRUD操作,很大程度上提升开发效率.如果需要联合查询仍然需要手写sql. ...
- SpringBoot之入门教程-SpringBoot项目搭建
SpringBoot大大的简化了Spring的配置,把Spring从配置炼狱中解救出来了,以前天天配置Spring和Mybatis,Springmvc,Hibernate等整合在一起,感觉用起来还是挺 ...
随机推荐
- C#反射运行该类下的方法
Text:反射的类名 s:方法名 data:参数 如果无参则: (string)method.Invoke(obj, null); Type type = typeof(Text); MethodIn ...
- 计算机网络基础05-Web应用
1 Web最重要的构成基础 网页 网页互相连接 1.1 网页 网页包含多个对象 对象:HTML文件.图片文件.视频文件.动态脚本等 基本HTML文件:包含对其它对象引用的连接 1.2 对象的寻址 UR ...
- Rpc-实现Zookeeper注册中心
1.前言 本文章是笔主在声哥的手写RPC框架的学习下,对注册中心的一个拓展.因为声哥某些部分没有保留拓展性,所以本文章的项目与声哥的工程有部分区别,核心内容在Curator的注册发现与注销,思想看准即 ...
- LG P4449 & JZOJ 于神之怒
\(\text{Problem}\) JZOJ上,求 \[\sum_{i=1}^n \sum_{j=1}^m \gcd(i,j)^k \] 对 \(10^9+7\) 取模 \(n,m,k \le 5 ...
- JZOJ 5432. 【NOIP2017提高A组集训10.28】三元组
题目 有 \(X+Y+Z\) 个三元组 \((x[i],y[i],z[i])\),请你从每个三元组中挑数,并满足以下条件: 1.每个三元组中可以且仅可以选择一个数(即 \(x[i],y[i],z[i] ...
- 流量加密之:MSF流量加密
流量加密之:MSF流量加密 目录 流量加密之:MSF流量加密 1 背景 2 生成SSL证书 3 使用MSF生成带证书的后门 4 验证流量 1 背景 在MSF中生成shell,并上线运行时.都是通过ht ...
- Express 项目发送 Cookie
在使用 Cookie 之前,需要安装 Express 中间件--cookie-parser: npm i cookie-parser import express from "express ...
- LeetCode-688 骑士在棋盘上的概率
来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/knight-probability-in-chessboard 题目描述 在一个 n x n 的 ...
- Linus对Linux 6.3内核的合并解释不满
Linux 6.3 内核的合并窗口已开启,Linus Torvalds 也收到了大量的 PR,目前总体看来正在有序进行.但 Linus 对部分合并请求的日志信息非常不满:"我之前就已经说过, ...
- RT-Thread Studio使用——创建工程并配置外部时钟(转)
硬件:正点原子阿波罗F429开发板,主控STM32F429IGT6,晶振25MHz. 软件:RT-Thread Studio RT-Thread版本:4.1.0 1.创建工程 根据所使用的硬件信息,配 ...