Fastcms官方文档
首页
  • 快速入门
  • 用户手册
  • 快速开始
  • 模板开发
  • 插件开发
接口文档 (opens new window)
提问 (opens new window)
  • Gitee (opens new window)
  • Github (opens new window)
首页
  • 快速入门
  • 用户手册
  • 快速开始
  • 模板开发
  • 插件开发
接口文档 (opens new window)
提问 (opens new window)
  • Gitee (opens new window)
  • Github (opens new window)
  • 快速开始

    • 构建源码
    • 代码生成
      • Fastcms代码生成抽象类
      • Cms模块代码生成
  • 模板开发

    • 模板结构
    • 内置标签
    • 自定义标签
  • 插件开发

    • 插件结构
    • 如何开发插件

代码生成

本文帮助您了解并使用Fastcms代码生成工具,代码生成扩展自mybatis-plus-generator 3.5.1

# Fastcms代码生成抽象类

抽象类中配置数据库链接等信息,具体生成哪些表的代码,在其子类中指定

public abstract class AbstractCodeGen {

    final static String url = "jdbc:mysql://localhost:3306/fastcms?autoReconnect=true&useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai";
    final static String username = "root";
    final static String password = "root";

    abstract String getOutputDir();
    abstract String getModelName();
    abstract String[] getTableNames();

    protected void gen() {
        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author("wjun_java@163.com") // 设置作者
                            .disableOpenDir()
                            .outputDir(System.getProperty("user.dir") + getOutputDir() + "/src/main/java"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.fastcms") // 设置父包名
                            .moduleName(getModelName())
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, System.getProperty("user.dir") + getOutputDir() + "/src/main/resources/mapper/")); // 设置mapperXml生成路径
                })
                .templateConfig(builder -> builder.disable(TemplateType.CONTROLLER))
                .strategyConfig(builder -> {
                    builder.addInclude(getTableNames()); // 设置需要生成的表名
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
                .execute();
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

# Cms模块代码生成

CmsCodeGen从AbstractCodeGen继承,指定cms模块对应需要生成代码的相关表名,系统模块同理实现,代码如下

public class CmsCodeGen extends AbstractCodeGen {

	@Override
	String getOutputDir() {
		return System.getProperty("user.dir") + "/cms";
	}

	@Override
	protected String getModelName() {
		return "cms";
	}

	@Override
	String[] getTableNames() {
		return new String[] {"article", "article_category", "article_tag", "article_comment", "single_page", "single_page_comment", "menu"};
	}

	public static void main(String[] args) throws Exception {
		CmsCodeGen cmsCodeGen = new CmsCodeGen();
		cmsCodeGen.gen();
	}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

main方法运行即可生成该模块的代码到对应的工程目录下

上次更新: 2022/02/27, 02:44:28
构建源码
模板结构

← 构建源码 模板结构→

Theme by Vdoing | Copyright © 2016-2022 小橘灯科技 | .粤ICP备20053122号.
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式