package com.wlk.study.util;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.WordUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* mybatis plus 代码自动生成器
* 参考: https://blog.csdn.net/ChunwaiLeung/article/details/121629340
*/
public class MyBatisPlusGenerator {
private static String PACKAGE = "com.wlk.study";
private static String AUTHOR = "luka";
private static String DATA_USER_NAME = "root";
private static String DATA_PASSWORD = "123";
private static String DATA_DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
private static String DATA_URL = "jdbc:mysql://127.0.0.1:3306/dblog?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&allowPublicKeyRetrieval=true&useLegacyDatetimeCode=false";
private static String PATH_MAPPER = "/src/main/java/com/wlk/study/";
private static String PATH_MAPPER_XML = "/src/main/resources/mapper/";
private static String PATH_CONTROLLER = "/src/main/java/com/wlk/study/";
private static String PATH_SERVICE = "/src/main/java/com/wlk/study/";
private static String PATH_SERVICE_IMP = "/src/main/java/com/wlk/study/";
private static String PATH_ENTITY = "/src/main/java/com/wlk/study/";
public static void main(String[] args) {
String[] tables = scanner("请输入要生成的表名多个用 , 分割").split(",");
for (String table : tables) {
shell(table);
}
}
public static String scanner(String someThing) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入" + someThing + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String sc = scanner.next();
if ("" != sc && null != sc) {
return sc;
}
}
throw new MybatisPlusException("请输入正确的" + someThing + "!");
}
private static void shell(String table) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDriverName(DATA_DRIVER_NAME);
dsc.setUsername(DATA_USER_NAME);
dsc.setPassword(DATA_PASSWORD);
dsc.setUrl(DATA_URL);
mpg.setDataSource(dsc);
// 全局配置
final String projectPath = System.getProperty("user.dir"); // 默认定位到的当前用户目录("user.dir")(即工程根目录) https://blog.csdn.net/qq_29964641/article/details/86686585
GlobalConfig gc = new GlobalConfig();
gc.setAuthor(AUTHOR);//作者名称
gc.setDateType(DateType.ONLY_DATE);
gc.setOpen(false); //生成后是否打开资源管理器
gc.setServiceName("%sService"); //自定义文件命名,注意 %s 会自动填充表实体属性! %s作为占位符
gc.setServiceImplName("%sServiceImpl");
gc.setMapperName("%sMapper");
gc.setXmlName("%sMapper");
gc.setSwagger2(true);// 是否开启Swagger2模式
gc.setFileOverride(true); //重新生成时文件是否覆盖
gc.setActiveRecord(false);// 不需要ActiveRecord特性的请改为false
gc.setEnableCache(false);// XML 二级缓存
gc.setBaseResultMap(true);// XML ResultMap
gc.setBaseColumnList(false);// XML columList
gc.setIdType(IdType.ID_WORKER_STR); //主键策略
gc.setOutputDir(projectPath + "/src/main/java");
gc.setControllerName("%sController");
mpg.setGlobalConfig(gc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent(PACKAGE);
pc.setMapper("mapper");//dao
pc.setService("service");//servcie
pc.setController("controller");//controller
pc.setEntity("entity");
// pc.setModuleName("model名"); 自定义包名
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// //表信息
String name = this.getConfig().getStrategyConfig().getColumnNaming().name();
}
};
// 模板引擎是 freemarker
// 自定义controller的代码模板
// 如果模板引擎是 velocity
String templatePath = "/template/Mapper.xml.vm";
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出,配置mapper.xml
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
//根据自己的位置修改
return projectPath + PATH_MAPPER_XML + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
//控制层
templatePath = "/template/Controller.java.vm";
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 + pc.getModuleName()
String expand = projectPath + PATH_CONTROLLER + "controller";
String entityFile = String.format((expand + File.separator + "%s" + ".java"), tableInfo.getControllerName());
return entityFile;
}
});
//业务层
templatePath = "/template/Service.java.vm";
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 + pc.getModuleName()
String expand = projectPath + PATH_SERVICE + "service";
String entityFile = String.format((expand + File.separator + "%s" + ".java"), tableInfo.getServiceName());
return entityFile;
}
});
templatePath = "/template/ServiceImpl.java.vm";
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 + pc.getModuleName()
String expand = projectPath + PATH_SERVICE_IMP + "service/impl";
String entityFile = String.format((expand + File.separator + "%s" + ".java"), tableInfo.getServiceImplName());
return entityFile;
}
});
//数据层
templatePath = "/template/Mapper.java.vm";
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 + pc.getModuleName()
String expand = projectPath + PATH_MAPPER + "mapper";
String entityFile = String.format((expand + File.separator + "%
没有合适的资源?快使用搜索试试~ 我知道了~
mybatis plus 模板文件
共7个文件
vm:6个
java:1个
需积分: 5 0 下载量 97 浏览量
2023-03-02
23:12:39
上传
评论
收藏 8KB ZIP 举报
温馨提示
mybatis plus 模板文件
资源推荐
资源详情
资源评论
收起资源包目录
template.zip (7个子文件)
template
Mapper.xml.vm 1KB
Entity.java.vm 4KB
ServiceImpl.java.vm 678B
MyBatisPlusGenerator.java 11KB
Service.java.vm 392B
Controller.java.vm 4KB
Mapper.java.vm 390B
共 7 条
- 1
资源评论
xiao魏同志
- 粉丝: 24
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功