package cn.zm.mybatis;
import cn.zm.mybatis.utils.ObjectConvert;
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.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ClassPathResource;
import java.util.*;
/** 功能描述: <br>
* <逆向工程生成类>
*
* @author 十渊
* @date 2021/10/12 13:29
* @return
*/
public class CodeGenerator {
private static YamlPropertiesFactoryBean factoryBean = new YamlPropertiesFactoryBean();;
private static Properties object;
private static String driver;
private static String url;
private static String username;
private static String password;
private static String packageName;
private static String module;
// 代码生成器
private static AutoGenerator mpg;
// 全局配置
private static GlobalConfig gc;
private static TemplateConfig templateConfig;
private static String projectPath = System.getProperty("user.dir");
static {
templateConfig = new TemplateConfig();
// 配置自定义输出模板
//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
// templateConfig.setEntity("templates/entity2.java");
// templateConfig.setService();
// templateConfig.setController();
templateConfig.setEntity("/templates/code/entity.java");
templateConfig.setMapper("/templates/code/mapper.java");
templateConfig.setService("/templates/code/service.java");
templateConfig.setServiceImpl("/templates/code/serviceImpl.java");
templateConfig.setController("/templates/code/controller.java");
templateConfig.setXml(null);
}
public static void main(String[] args) {
String[] tables = scanner("表名,多个英文逗号分割").split(",");
ClassPathResource app = new ClassPathResource("gen.yml");
ClassPathResource[] apps = new ClassPathResource[]{app};
factoryBean.setResources(apps);
object = factoryBean.getObject();
driver = object.getProperty("spring.datasource.driver-class-name");
url = object.getProperty("spring.datasource.url");
username = object.getProperty("spring.datasource.username");
password = object.getProperty("spring.datasource.password");
packageName = object.getProperty("mybatis-plus.generate.package");
module = object.getProperty("mybatis-plus.generate.module");
// 代码生成器
mpg = new AutoGenerator();
// 全局配置
gc = new GlobalConfig();
genDO(tables);
genDTO(tables);
genVO(tables);
}
/**
* <p>
* 读取控制台内容
* </p>
*/
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入" + tip + ":");
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotBlank(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
private static void genDO(String[] tables, ClassPathResource... apps) {
// YamlPropertiesFactoryBean factoryBean = new YamlPropertiesFactoryBean();
// factoryBean.setResources(apps);
// Properties object = factoryBean.getObject();
// String driver = object.getProperty("spring.datasource.driver-class-name");
// String url = object.getProperty("spring.datasource.url");
// String username = object.getProperty("spring.datasource.username");
// String password = object.getProperty("spring.datasource.password");
// String packageName = object.getProperty("mybatis-plus.generate.package");
//
// // 代码生成器
// AutoGenerator mpg = new AutoGenerator();
//
// // 全局配置
// GlobalConfig gc = new GlobalConfig();
// String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + '/' + module + "/src/main/java");
gc.setAuthor("十渊");
gc.setOpen(false);
gc.setSwagger2(true); //实体属性 Swagger2 注解
gc.setEntityName("%s");
//gc.setMapperName("%sMapper");
//gc.setXmlName("%sMapper");
//gc.setServiceName("%sService");
//gc.setServiceImplName("%sServiceImpl");
// gc.setControllerName("%rest");
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl(url);
dsc.setDriverName(driver);
dsc.setUsername(username);
dsc.setPassword(password);
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName("web");
pc.setParent(packageName);
pc.setEntity("entity");
pc.setController("rest");
pc.setService("service");
pc.setServiceImpl("service.impl");
pc.setMapper("mapper");
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
String templatePath = "/templates/code/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 + "/src/main/resources/mapper/" + pc.getModuleName()
return projectPath + '/' + module + "/src/main/resources/mapper/"
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
/*
cfg.setFileCreate(new IFileCreate() {
@Override
public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
// 判断自定义文件夹是否需要创建
checkDir("调用默认方法创建的目录,自定义目录用");
if (fileType == FileType.MAPPER) {
// 已经生成 mapper 文件判断存在,不想重新生成返回 false
return !new File(filePath).exists();
}
// 允许生成模板文件
return true;
}
});
*/
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// // 配置模板
// templateConfig = new TemplateConfig();
//
// // 配置自定义输出模板
// //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
// // templateConfig.setEntity("templates/entity2.java");
// // templateConfig.setService();
// // templateConfig.setController();
// templateConfig.setEntity("/templates/code/entity.java");
// templateConfig.setMapper("/templates/code/map
没有合适的资源?快使用搜索试试~ 我知道了~
基于SSM和SpringBoot的毕业设计.zip
共111个文件
java:73个
xml:17个
ftl:12个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 144 浏览量
2023-07-08
16:02:31
上传
评论
收藏 20.98MB ZIP 举报
温馨提示
基于ssm写的系统(含数据库文件) 语言:java 框架:ssm 数据库:mysql
资源推荐
资源详情
资源评论
收起资源包目录
基于SSM和SpringBoot的毕业设计.zip (111个子文件)
ssm2019.7z 20.87MB
controller.java.ftl 6KB
entity.java.ftl 4KB
entity.java.ftl 4KB
controller.java.ftl 3KB
mapper.xml.ftl 3KB
mapper.xml.ftl 3KB
serviceImpl.java.ftl 2KB
serviceImpl.java.ftl 1KB
service.java.ftl 734B
service.java.ftl 697B
mapper.java.ftl 590B
mapper.java.ftl 325B
.gitignore 422B
CodeGenerator.java 13KB
MinIoUtil.java 8KB
DataTransferObjectAspect.java 5KB
DruidConfig.java 4KB
AssertUtil.java 4KB
BusCommentsController.java 4KB
AppRegexpConstants.java 4KB
ServiceException.java 4KB
ScrewApplicationTests.java 3KB
GlobalExceptionHandler.java 3KB
BaseScenicSpotController.java 3KB
BaseAccountController.java 3KB
BaseUserController.java 3KB
ServiceExceptionBuilder.java 3KB
ObjectsUtils.java 3KB
BaseController.java 2KB
LocalDateTimeConfiguration.java 2KB
FastJsonConfiguration.java 2KB
MyLocalDateTimeDeserializer.java 2KB
BaseScenicSpotDTO.java 2KB
BaseScenicSpot.java 2KB
BaseScenicSpotVO.java 2KB
WebsocketServer.java 2KB
Knife4jConfiguration.java 2KB
ConvertUtil.java 1KB
AuthController.java 1KB
MyLocalDateTimeSerializer.java 1KB
RelaUserAccountServiceImpl.java 1KB
ThreadPoolConfiguration.java 1KB
BaseScenicSpotServiceImpl.java 1KB
BusCommentsDTO.java 1KB
BusComments.java 1KB
BaseUser.java 1KB
BaseAccountServiceImpl.java 1KB
BusCommentsServiceImpl.java 1KB
BusCommentsVO.java 1KB
RelaUserAccount.java 1018B
BaseUserServiceImpl.java 1017B
BaseAccount.java 999B
BaseUserDTO.java 968B
ResResult.java 951B
ObjectConvert.java 917B
BaseUserVO.java 899B
RelaUserAccountDTO.java 883B
BaseAccountDTO.java 864B
RelaUserAccountVO.java 800B
BaseAccountVO.java 789B
MybatisPlusConfiguration.java 719B
ResEnum.java 699B
CorsConfiguration.java 634B
ScenicSpotCommentsUserDTO.java 633B
IRelaUserAccountService.java 628B
IBaseScenicSpotService.java 606B
ConcurrentUtils.java 588B
IBaseAccountService.java 579B
IBusCommentsService.java 579B
ScenicSpotCommentsDTO.java 578B
BaseEntity.java 558B
IBaseUserService.java 546B
SetNull.java 489B
GlobalConfig.java 442B
TestObje.java 428B
TripWebApplication.java 340B
RelaUserAccountMapper.java 260B
BaseScenicSpotMapper.java 257B
BusCommentsMapper.java 248B
BaseAccountMapper.java 248B
Child.java 241B
BaseUserMapper.java 239B
CommentsEntityConstants.java 180B
UpdateDataTransferObject.java 154B
QueryDataTransferObject.java 154B
CreateDataTransferObject.java 154B
trip_web-202207121702.sql 10KB
pom.xml 4KB
pom.xml 3KB
pom.xml 2KB
pom.xml 2KB
pom.xml 2KB
Project_Default.xml 1KB
encodings.xml 1KB
pom.xml 990B
pom.xml 873B
pom.xml 870B
pom.xml 853B
jarRepositories.xml 852B
共 111 条
- 1
- 2
资源评论
我慢慢地也过来了
- 粉丝: 6224
- 资源: 3935
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功