1 package com.kyplatform.generator;
2
3 import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
4 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
5 import com.baomidou.mybatisplus.generator.AutoGenerator;
6 import com.baomidou.mybatisplus.generator.config.*;
7 import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
8
9 import java.util.Scanner;
10
11 /**
12 * mybatis代码生成器
13 */
14 public class CodeGenerator {
15 /**
16 * <p>
17 * 读取控制台内容
18 * </p>
19 */
20 public static String scanner(String tip) {
21 Scanner scanner = new Scanner(System.in);
22 StringBuilder help = new StringBuilder();
23 help.append("请输入" + tip + ":");
24 System.out.println(help.toString());
25 if (scanner.hasNext()) {
26 String ipt = scanner.next();
27 if (StringUtils.isNotEmpty(ipt)) {
28 return ipt;
29 }
30 }
31 throw new MybatisPlusException("请输入正确的" + tip + "!");
32 }
33
34 public static void main(String[] args) {
35 // 代码生成器
36 AutoGenerator mpg = new AutoGenerator();
37
38 // 全局配置
39 GlobalConfig gc = new GlobalConfig();
40 String projectPath = System.getProperty("user.dir");
41 gc.setOutputDir(projectPath + "/src/main/java");//生成文件的输出目录
42 gc.setAuthor("zhicaili");//开发人员
43 gc.setOpen(true);//是否打开输出目录
44 gc.setServiceName("%sService");//service 命名方式
45 gc.setServiceImplName("%sServiceImpl");//service impl 命名方式
46 // 自定义文件命名,注意 %s 会自动填充表实体属性!
47 gc.setMapperName("%sMapper");
48 gc.setXmlName("%sMapper");
49 gc.setFileOverride(true);
50 gc.setActiveRecord(true);
51 gc.setEnableCache(false);// XML 二级缓存
52 gc.setBaseResultMap(true);// XML ResultMap
53 gc.setBaseColumnList(false);// XML columList
54 mpg.setGlobalConfig(gc);
55
56 // 数据源配置
57 DataSourceConfig dsc = new DataSourceConfig();
58 dsc.setUrl("jdbc:mysql://127.0.0.1:3306/xxxxx?useUnicode=true&useSSL=false&characterEncoding=utf8");
59 // dsc.setSchemaName("public"); 数据库 schema name
60 dsc.setDriverName("com.mysql.jdbc.Driver");
61 dsc.setUsername("root");
62 dsc.setPassword("******");
63 mpg.setDataSource(dsc);
64
65 // 包配置
66 PackageConfig pc = new PackageConfig();
67 //pc.setModuleName(scanner("模块名"));//父包模块名
68 pc.setParent("com.kyplatform.admin");//父包名。// 自定义包路径 如果为空,将下面子包名必须写全部, 否则就只需写子包名
69 pc.setEntity("pojo");
70 pc.setService("service");
71 pc.setServiceImpl("service.impl");
72 pc.setController("controller");//设置控制器包名
73 mpg.setPackageInfo(pc);
74
75 // 自定义配置
76 /* InjectionConfig cfg = new InjectionConfig() {
77 @Override
78 public void initMap() {
79 // to do nothing
80 }
81 };
82 List<FileOutConfig> focList = new ArrayList<>();
83 focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
84 @Override
85 public String outputFile(TableInfo tableInfo) {
86 // 自定义输入文件名称
87 return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
88 + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
89 }
90 });*/
91 /* cfg.setFileOutConfigList(focList);
92 mpg.setCfg(cfg);*/
93 mpg.setTemplate(new TemplateConfig().setXml(null));
94
95 // 策略配置
96 StrategyConfig strategy = new StrategyConfig();
97 strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
98 strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略, 未指定按照 naming 执行
99 // strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");//自定义继承的Entity类全称,带包名
100 strategy.setEntityLombokModel(true);//【实体】是否为lombok模型(默认 false)
101 strategy.setRestControllerStyle(true);//生成 @RestController 控制器
102 //strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");//自定义继承的Controller类全称,带包名
103 strategy.setInclude("tb_user","tb_organization","tb_person","tb_signin","tb_sys_config","tb_sys_log");//需要包含的表名,允许正则表达式
104 //strategy.setSuperEntityColumns("id");//自定义基础的Entity类,公共字段
105 strategy.setControllerMappingHyphenStyle(true);//驼峰转连字符
106 strategy.setTablePrefix("tb_");//表前缀
107 mpg.setStrategy(strategy);
108 //mpg.setTemplateEngine(new FreemarkerTemplateEngine());
109 mpg.execute();
110 }
111
112
113 }