package com.beadhouse.beadhouse;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.util.ArrayList;
import java.util.Scanner;
/**
* @Package: com.ourwill.digitalmuseum
* @Author: LiuFeng
* @Description: AutoGenerator MyBatis-Plus 代码生成器
* @Date: Created in 2020/4/28
*/
public class MybatisGenerator {
/**
* <p>
* 读取控制台内容
* </p>
*/
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotEmpty(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
//生成文件输出目录
gc.setOutputDir(projectPath + "/src/main/java");
//开发人员
gc.setAuthor("Hou Huiyao");
//是否打开输出目录
gc.setOpen(false);
gc.setSwagger2(true); //实体属性 Swagger2 注解
gc.setEntityName("%s");
//service命名方式
gc.setServiceName("I%sService");
//service impl命名方式
gc.setServiceImplName("%sServiceImpl");
//自定义文件命名,注意 %s 会自动填充表实体属性!
gc.setMapperName("%sMapper");
gc.setXmlName("%sMapper");
gc.setIdType(IdType.ID_WORKER);
gc.setDateType(DateType.ONLY_DATE);
gc.setFileOverride(true);
gc.setActiveRecord(true);
gc.setEnableCache(false);// XML 二级缓存
gc.setBaseResultMap(true);// XML ResultMap
gc.setBaseColumnList(false);// XML columList
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/yanglaoyuan?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
//父包模块名
//pc.setModuleName(scanner("kk"));
//父包名。// 自定义包路径 如果为空,将下面子包名必须写全部, 否则就只需写子包名
pc.setParent("com.beadhouse.beadhouse");
pc.setEntity("entity");
pc.setService("service");
pc.setServiceImpl("service.impl");
pc.setMapper("mapper");
//设置控制器包名
pc.setController("controller");
mpg.setPackageInfo(pc);
/**
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
List<FileOutConfig> focList = new ArrayList<>();
focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
@Override
public String outputFile(TableInfo arg0) {
// 自定义输入文件名称
return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
+ "/" + arg0.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
**/
mpg.setTemplate(new TemplateConfig().setXml(null));
// 策略配置
StrategyConfig strategy = new StrategyConfig();
//数据库表映射到实体的命名策略
strategy.setNaming(NamingStrategy.underline_to_camel);
//数据库表字段映射到实体的命名策略, 未指定按照 naming 执行
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//自定义继承的Entity类全称,带包名
//strategy.setSuperEntityClass("com.ourwill.digitalmuseum.common.BaseEntity");
//【实体】是否为lombok模型(默认 false)
strategy.setEntityLombokModel(true);
//生成 @RestController 控制器
strategy.setRestControllerStyle(true);
//自定义继承的Controller类全称,带包名
//strategy.setSuperControllerClass("com.ourwill.digitalmuseum.common.BaseController");
//需要包含的表名,允许正则表达式
//strategy.setInclude(scanner("user"));
//strategy.setInclude("user");
//自定义基础的Entity类,公共字段
// strategy.setSuperEntityColumns("id");
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
//驼峰转连字符
strategy.setControllerMappingHyphenStyle(true);
//表前缀
// strategy.setTablePrefix(pc.getModuleName() + "_");
// 自动填充配置
TableFill gmtCreate = new TableFill("createTime", FieldFill.INSERT);
TableFill gmtModified = new TableFill("updateTime", FieldFill.INSERT_UPDATE);
ArrayList<TableFill> tableFills = new ArrayList<>();
tableFills.add(gmtCreate);
tableFills.add(gmtModified);
strategy.setTableFillList(tableFills);
mpg.setStrategy(strategy);
//mpg.setTemplateEngine(new FreemarkerTemplateEngine());
InjectionConfig injectionConfig = new InjectionConfig() {
@Override
public void initMap() {
}
};
mpg.setCfg(injectionConfig);
mpg.execute();
}
}
InjectionConfig injectionConfig = new InjectionConfig() {
@Override
public void initMap() {
}
};
//不知道为什么没有这个方法就构建失败,加上就好了
mpg.setCfg(injectionConfig);
需要添加依赖
<!-- 代码生成器 start -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.1.2</version>
</dependency>
<!-- 代码生成器需要的引擎模板 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
这些都没问题的话重新构建项目一下,或者clean,重启等万能方法