Mybatis-plus+springboot 自动代码生成配置
/*
* 项目名称:general-mall-system
* 类名称:GeneratorConfig.java
* 包名称:com.ly.generalmall.config.generator
*
* 修改履历:
* 日期 修正者 主要内容
* 2019-04-01 18:01 apple 初版完成
*
* Copyright (c) 2017-2019 咨同科技
*/
package com.ly.generalmall.config.generator;
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.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
/**
* GeneratorConfig
*
* @author apple
*/
@Configuration
public class GeneratorConfig {
/**
* 代码生成器
*
* @param globalConfig
* @param packageConfig
* @param templateConfig
* @param strategyConfig
* @return
*/
@Bean
public AutoGenerator autoGenerator(GlobalConfig globalConfig,
PackageConfig packageConfig,
TemplateConfig templateConfig,
StrategyConfig strategyConfig,
DataSourceConfig dataSourceConfig,
InjectionConfig injectionConfig) {
AutoGenerator autoGenerator = new AutoGenerator();
autoGenerator.setGlobalConfig(globalConfig);
autoGenerator.setPackageInfo(packageConfig);
autoGenerator.setTemplate(templateConfig);
autoGenerator.setStrategy(strategyConfig);
autoGenerator.setDataSource(dataSourceConfig);
autoGenerator.setCfg(injectionConfig);
autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());
return autoGenerator;
}
/**
* 全局配置
*
* @return
*/
@Bean
public GlobalConfig globalConfig() {
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setAuthor("liliudong");
globalConfig.setOpen(false);
globalConfig.setEntityName("%sEntity");
globalConfig.setMapperName("%sDao");
globalConfig.setServiceName("%sService");
globalConfig.setServiceImplName("%sServiceImpl");
globalConfig.setControllerName("%sController");
return globalConfig;
}
/**
* 模板配置
*
* @return
*/
@Bean
public TemplateConfig templateConfig() {
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setController("templates/controller.java");
templateConfig.setMapper("templates/dao.java");
templateConfig.setService("templates/service.java");
templateConfig.setServiceImpl("templates/serviceImpl.java");
return templateConfig;
}
/**
* 表策略配置
*
* @return
*/
@Bean
public StrategyConfig strategyConfig() {
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
strategyConfig.setEntityLombokModel(true);
strategyConfig.setRestControllerStyle(true);
strategyConfig.setControllerMappingHyphenStyle(true);
return strategyConfig;
}
/**
* 包配置
*
* @return
*/
@Bean
public PackageConfig packageConfig() {
PackageConfig packageConfig = new PackageConfig();
packageConfig.setParent("com.ly.generalmall.moudles");
return packageConfig;
}
/**
* 数据源配置
*
* @return
*/
@Bean
public DataSourceConfig dataSourceConfig() {
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setUsername("root");
dataSourceConfig.setPassword("Root");
dataSourceConfig.setUrl("jdbc:mysql://localhost:3306");
dataSourceConfig.setDriverName("com.mysql.jdbc.Driver");
return dataSourceConfig;
}
/**
* 自定义属性
*
* @return
*/
@Bean
public InjectionConfig injectionConfig(PackageConfig packageConfig, TemplateConfig templateConfig) {
// 自定义属性注入
ArrayList<FileOutConfig> fileOutConfigs = new ArrayList<>();
InjectionConfig injectionConfig = new InjectionConfig() {
@Override
public void initMap() {
HashMap<String, Object> map = new HashMap<>();
String property = System.getProperty("user.dir");
String[] strs = property.split(File.separator);
map.put("PROJECT_NAME", strs[strs.length - 1]);
this.setMap(map);
}
};
String projectPath = System.getProperty("user.dir");
FileOutConfig mapperOutConfig = new FileOutConfig(templateConfig.getMapper() + ".ftl") {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath + "/src/main/resources/mapper/" + packageConfig.getModuleName()
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
};
fileOutConfigs.add(mapperOutConfig);
injectionConfig.setFileOutConfigList(fileOutConfigs);
return injectionConfig;
}
}