Mybatiplus通用3.5.1版本及其以上的代码生成器工具类
Mybatiplus通用3.5.1版本及其以上的代码生成器工具类
package com.gton.util;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
/**
* @description: Mybatis-Plus3.5.1代码生成器
* @author: GuoTong
* @createTime: 2022-02-13 16:59
* @since JDK 1.8 OR 11
**/
@SuppressWarnings("unchecked")
public class AutoGeneratorTools {
/**
* Description:
* 使用前:
* 指定生成的模块名称:modelName
* 指定生成业务子包名称:mkdirFiile
*
* @author: GuoTong
* @date: 2022-10-22 22:03:01
*/
private static String modelName = "/commodity/";
// 按照业务划分不同多个子包
private static String mkdirFiile = "user";
private final String url = "jdbc:mysql://127.0.0.1:3306/study?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC";
// 高版本的DataSource不需要驱动 保留
private final String driver = "com.mysql.jdbc.Driver";
private final String username = "root";
private final String password = "root";
public static void main(String[] args) {
AutoGeneratorTools autoGeneratorTools = new AutoGeneratorTools();
String userDir = System.getProperty("user.dir");
String path = "src/main/java";
String parentPath = "com.gton." + mkdirFiile;
String mapperXmlPath = "/com/gton/" + mkdirFiile + "/mapper/xml";
List<String> tables = new ArrayList<>();
tables.add("product_buying");
autoGeneratorTools.getDataSourceByFile();
// 代码自动生成
autoCreateCode(autoGeneratorTools, userDir, path, parentPath, mapperXmlPath, tables);
}
private Properties getDataSourceByFile() {
FileInputStream dataSource = null;
InputStream dataSourceStream = null;
try {
String property = System.getProperty("user.dir");
dataSource = new FileInputStream(property + "/common/src/main/resources/application.properties");
dataSourceStream = new BufferedInputStream(dataSource);
Properties ps = new Properties();
ps.load(dataSourceStream);
return ps;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (dataSourceStream != null) {
try {
dataSourceStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (dataSource != null) {
try {
dataSource.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return new Properties();
}
private static void autoCreateCode(AutoGeneratorTools autoGeneratorTools, String userDir, String path, String parentPath, String mapperXmlPath, List<String> tables) {
FastAutoGenerator.create(autoGeneratorTools.getDataConfigBuilder()).
globalConfig(builder ->
builder.author("GuoTong").
commentDate("yyyy-MM-dd").
enableSwagger().
dateType(DateType.TIME_PACK).
disableOpenDir().
outputDir(userDir + modelName + path)).
packageConfig(builder ->
builder.parent(parentPath).
moduleName("").
entity("entity").
service("service").
serviceImpl("service.impl").
mapper("mapper").
xml("mapper.xml").
controller("controller").
pathInfo(Collections.singletonMap(OutputFile.mapperXml, userDir + modelName + path + mapperXmlPath))).
strategyConfig(builder ->
builder.addInclude(tables).
entityBuilder().
idType(IdType.ASSIGN_ID).
enableLombok().
enableTableFieldAnnotation().
enableChainModel().
logicDeleteColumnName("is_del").
logicDeletePropertyName("isDel").
addTableFills(new Column("create_time", FieldFill.INSERT)).
addTableFills(new Property("createTime", FieldFill.INSERT)).
addTableFills(new Column("update_time", FieldFill.INSERT_UPDATE)).
addTableFills(new Property("updateTime", FieldFill.INSERT_UPDATE)).
controllerBuilder().
formatFileName("%sController").
enableRestStyle().
serviceBuilder().
formatServiceFileName("%sService").
formatServiceImplFileName("%sServiceImpl").
mapperBuilder().
enableBaseColumnList().
enableMapperAnnotation().
enableBaseResultMap().
formatMapperFileName("%sMapper").
formatXmlFileName("%sMapper")).
templateEngine(new VelocityTemplateEngine()).execute();
}
private DataSourceConfig.Builder getDataConfigBuilder() {
Properties dataSourceByFile = getDataSourceByFile();
Object dbURl = dataSourceByFile.getOrDefault("spring.datasource.url", url);
Object dbUsername = dataSourceByFile.getOrDefault("spring.datasource.username", username);
Object dbPassword = dataSourceByFile.getOrDefault("spring.datasource.password", password);
return new DataSourceConfig.Builder(dbURl.toString(), dbUsername.toString(), dbPassword.toString()).
dbQuery(new MySqlQuery()).
keyWordsHandler(new MySqlKeyWordsHandler()).
schema("Mybatis-Plus").
typeConvert(new MySqlTypeConvert());
}
}
可以读取classpath下的数据源配置:application.properties
#数据源
spring.datasource.url=jdbc:mysql://130.33.238.27:3306/cloud_market?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
Mybatiplus通用3.5.1版本及其以上的代码生成器工具类的更多相关文章
- Asp.net Core + EF Core + Bootstrap搭建的MVC后台通用管理系统模板(跨平台版本)
Asp.net Core + EF Core + Bootstrap搭建的MVC后台通用管理系统模板(跨平台版本) 原创 2016年07月22日 10:33:51 23125 6月随着.NET COR ...
- 通用访问 - 用“反射”来设计通用的通信协议,以及配套的SDK、工具
1. 效果演示 2. 通信协议 功能介绍 特点 TCP协议 WebApi协议 3. SDK与工具 4. 应用示例 迷你网管 通用GIS 系统管理 5. 设计初衷与演化 1. 效果演示 服务 ...
- 通用超级强大的基于Oracle数据库的代码生成器
项目中使用了Oracle数据库,命名基本规范为表名和字段名全部大写,用下划线分割各个单词: 如“BASE_USER_LOGON_EXTEND”这个表表示用户登录的扩展表. 基于这个规范,通用权限管理系 ...
- 一个基于POI的通用excel导入导出工具类的简单实现及使用方法
前言: 最近PM来了一个需求,简单来说就是在录入数据时一条一条插入到系统显得非常麻烦,让我实现一个直接通过excel导入的方法一次性录入所有数据.网上关于excel导入导出的例子很多,但大多相互借鉴. ...
- Excel和Word 简易工具类,JEasyPoi 2.1.5 版本发布
Excel和Word 简易工具类,JEasyPoi 2.1.5 版本发布 摘要: jeasypoi 功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员 就可以方便的写出Excel导 ...
- JSON工具类的构建(前端版本)
前言 在前后端交互的选择上,之前一直采用的是模板引擎(因为我只负责后端). 而这次的一个算是作业吧,前后端都是我,所以就研究了一下JSON交互在java web的应用(主要是前端). 优缺点 因为我是 ...
- utils部分--一些通用的工具类封装
1.简介 utils部分是对一些常用的工具类进行简单的封装,使用起来比较方便.这里列举常用的一些. 2.ContextUtils使用 主要封装了网络判断.一些方法解释如下: ? 1 2 3 4 5 6 ...
- JEasyPoi 2.1.4 (Jeecg订制) 版本发布,Excel 和 Word 简易工具类
JEasyPoi 2.1.4 (jeecg订制)版本发布,EasyPoi Excel 和 Word 简易工具类 easypoi 功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员 ...
- 快速创建SpringBoot2.x应用之工具类自动创建web应用、SpringBoot2.x的依赖默认Maven版本
快速创建SpringBoot2.x应用之工具类自动创建web应用简介:使用构建工具自动生成项目基本架构 1.工具自动创建:http://start.spring.io/ 2.访问地址:http://l ...
- liferay总结的通用的工具类
在写增删改查的时候,自己动手写了几个通用的工具类,这几个通用的工具类也是基于API写的 第一个是liferay中的分页.跟我们做普通的web开发,分页是一样的首先需要建立一个分页的实体的类 packa ...
随机推荐
- 深度解读 Linux 内核级通用内存池 —— kmalloc 体系
本文是笔者 slab 系列的最后一篇文章,为了方便大家快速检索,先将相关的文章列举出来: <细节拉满,80 张图带你一步一步推演 slab 内存池的设计与实现> <从内核源码看 sl ...
- 「学习笔记」CDQ分治
CDQ 分治的思想最早由 IOI2008 金牌得主陈丹琦在高中时整理并总结,目前这个思想的拓展十分广泛. 优点:可以将数据结构或者 DP 优化掉一维 缺点:这是离线算法. 引入 让我们来看一个问题 有 ...
- P3134 [USACO16JAN] Lights Out G 关灯
P3134 [USACO16JAN] Lights Out G 关灯 目录 P3134 [USACO16JAN] Lights Out G 关灯 [USACO16JAN] Lights Out G 题 ...
- Vue两组间通过传值设置另一个组件背景
vue项目中,从一个组件获取图片url,并为另一个组件设置该图片为背景的盒子 子组件 <template> <div class="intd_content_banner& ...
- Java IO流 - 字节流的使用详细介绍
IO流的基本介绍: IO流的概述: i 表示intput,是数据从硬盘文件读入到内存的过程,称之输入,负责读. o 表示output,是内存程序的数据从内存到写出到硬盘文件的过程,称之输出,负责写. ...
- 详解同为4800W像素的相机传感器,三星GM1和索尼IMX586区别在哪里?
数字影像之父Bryce Bayer基于RGB模式,通过在感光元件前加上一个滤镜的方法终于实现了彩色照片.Bayer滤镜跨出了照片从黑白到彩色的一大步,但是对于挑剔的人眼来说,每个像素只有一个颜色是远远 ...
- mysql创建可以让Django链接的用户名
输入以下命令: 1 grant all privileges on 库名.* to 账户名@'%' identified by 'password'; 2 flush privileges;
- 【技术积累】Linux中的命令行【理论篇】【四】
ar命令 命令介绍 ar命令是Linux系统中的一个工具,用于创建.修改和提取静态库文件(archive files).静态库文件是一组已编译的目标文件的集合,可以被链接到可执行文件中. 命令说明 a ...
- 配置MySQL主从复制(一主一从)
前言 MySQL主从复制简介 MySQL主从复制的目的是实现数据库冗余备份,将master数据库的数据定时同步到slave库中,一旦master数据库宕机,可以将Web应用数据库配置快速切换到slav ...
- centos7升级内核到最新稳定版
前言 centos7默认的内核版本才3.10,诸如VXLAN.eBPF等特性无法体验,因此需要升级.目前(2022.02)Linux的内核版本已更新到5.16. 步骤 更新仓库 yum update ...