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 ...
随机推荐
- STL-array(ACM)
1.C++ 11 出现的, C++98没有 2.可以作为数组元素 pair<array<int, 3>, int>pair<int a[3], int > 不能这样 ...
- 尚医通-day10【微信扫码登录】(内附源码)
第01章-准备工作 1.申请微信登录 https://open.weixin.qq.com (1)注册开发者账号:准备营业执照 (2)邮箱激活 (3)完善开发者资料 (4)开发者资质认证:1-2个工作 ...
- 手动删除了Linux下syslog--/var/log/messages怎么办?
引言 Linux小白很容易犯得一个错误就是:查看日志的时候,尤其是系统日志,由于日志太多,把系统日志手动删除了.也就是把/var/log/messages文件删除了,而不是删除文件的内容.直接删除文件 ...
- Codeforces Round #879 (Div. 2) A-E
比赛链接 A 代码 #include <bits/stdc++.h> using namespace std; using ll = long long; bool solve() { i ...
- Spring Loaded代码热更新实践和原理分析
1.引言 开发者在编码效率和快速迭代中的痛点场景包括: 修改代码后,需要频繁重启应用,导致开发效率低下: 实时调试时,不能立即看到代码修改的结果: 大型项目中,重启的时间成本较高. 针对这些问题,本文 ...
- Github秒变VSCode在线编辑器
在仓库页面 在网页地址中github后面加上 1s 即可
- 【调制解调】SSB 单边带调幅
说明 学习数字信号处理算法时整理的学习笔记.同系列文章目录可见 <DSP 学习之路>目录,代码已上传到 Github - ModulationAndDemodulation.本篇介绍 SS ...
- 【git】基于JGit通过ssh-url拉取指定commit-id的代码
实现 1️⃣ pom依赖: <dependency> <groupId>org.eclipse.jgit</groupId> <artifactId>o ...
- Linux 如何删除乱码的文件
事情是这样,服务器很多人在使用,以前的离职同事留了一大堆不知道是什么东西. 那些文件看不了,又删不掉,非常碍眼. 我搜索了挺多资料,没有一篇文章能真的解决问题(感觉都是抄来抄去的). 用 SFTP 工 ...
- 我学到的一下vue使用技巧
这两天学到的vue使用技巧 v-if , 当封装组件的时候,用到的props,最外层最好加个v-if,防止出现cannot read property of undefined 这样的错误,如果pro ...