代码生成器

代码生成器

原理是读取表结构,根据表结构的字段名称、数据类型、注释生成实体类,然后根据实体类生成controller和service

freemarker

标签参数

${pramName}: 根据controller中定义的值,对pramName进行替换
<#if>: 当结果为true时才会进行展示

<p>你好,
    <#if userName == "lyra">
        <strong>${userName}</strong>
    <#elseif userName == "bonbon">
        <h1>${userName}</h1>
    <#else>
        ${userName}
    </#if>
</p>

<#list>: 对列表进行逐个进行遍历并输出

<#list sequence as loopVariable>
    repeatThis
</#list>

${date?date}: 当前日期
${date?time}: 当前时间
${date?datetime}: 当前日期和时间
${date?string("yyyy-MM-dd HH:mm:ss")}: 自定义日期格式

代码生成器

如下所示,定义了一个entity模板

package ${packageName}.domain.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModelProperty;

@TableName("${tableName}")
public class ${className} {
<#list filedList as item>
    ${item.annotation}
    private ${item.type} ${item.name};

</#list>

<#list filedList as item>
    public ${item.type}  get${item.getSetName}() {
        return this.${item.name};
    }

    public void set${item.getSetName}(${item.type} ${item.name}) {
        this.${item.name} = ${item.name};
    }
</#list>

}

再读取数据库,将表字段、类型、注释读取出来,之后设置到定义的模板引擎上即可

        response.setCharacterEncoding("UTF-8");

        // 1. 连接MySQL
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://" + mysqlEntity.getIp() + ":" + mysqlEntity.getPort() + "/information_schema");
        dataSource.setUsername(mysqlEntity.getUsername());
        dataSource.setPassword(mysqlEntity.getPassword());
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

        Map<String, Object> templatePramMap = new HashMap<>();

        templatePramMap.put("packageName", mysqlEntity.getPackageName());
        templatePramMap.put("tableName", mysqlEntity.getTableName());
        String className = StrUtil.toCamelCase(mysqlEntity.getTableName());
        templatePramMap.put("className", StrUtil.upperFirst(className));

        // 设置导入包字段 Date、BigDecimal实体类
        List<String> importPackageList = new ArrayList<>();
        templatePramMap.put("importPackageList", importPackageList);


        // 2. 获取表字段
        List<Map<String, Object>> maps = jdbcTemplate.queryForList("select TABLE_NAME, COLUMN_NAME, DATA_TYPE, COLUMN_COMMENT, COLUMN_KEY from COLUMNS where TABLE_NAME = ? and TABLE_SCHEMA = ?", mysqlEntity.getTableName(), mysqlEntity.getDatabase());


        List<MyBatisEntityFiled> myBatisEntityFileds = new ArrayList<>();

        for (Map<String, Object> map : maps) {
            MyBatisEntityFiled myBatisEntityFiled = new MyBatisEntityFiled();
            // 3. 获取字段名称 将字段名称下划线转驼峰并首字母大写
            String name = StrUtil.toCamelCase(map.get("COLUMN_NAME").toString());
            myBatisEntityFiled.setGetSetName(StrUtil.upperFirst(name));
            myBatisEntityFiled.setName(name);

            // 4. 设置swagger注解 注解内容为表注释
            myBatisEntityFiled.setAnnotation("@ApiModelProperty(value = \"" + map.get("COLUMN_COMMENT") + "\", position = 1)");

            // 5. 根据MySQL数据类型与Java数据类型映射表 设置数据类型
            myBatisEntityFiled.setType(MySQLJavaTypeMappingEnum.MySQlTypeToJavaType(map.get("DATA_TYPE").toString()));

            myBatisEntityFileds.add(myBatisEntityFiled);
        }

        templatePramMap.put("filedList", myBatisEntityFileds);

        // 创建freemaker配置类
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_26);
        try {
            // 设置模板模板
            configuration.setDirectoryForTemplateLoading(new File("D:\\java-project\\code-generate\\src\\main\\resources\\templates\\mybatisplus"));
            configuration.setDefaultEncoding("UTF-8");

            // 创建模板
            Template template = configuration.getTemplate("entity.ftl");
            PrintWriter writer = response.getWriter();
            // 设置输出流和模板参数对象
            template.process(templatePramMap, writer);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
posted @   RainbowMagic  阅读(14)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2021-05-27 配置密钥与资源文件
2021-05-27 Swagger
点击右上角即可分享
微信分享提示