带界面的代码生成器

Posted 一只y

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了带界面的代码生成器相关的知识,希望对你有一定的参考价值。

1.项目gitthub地址链接: https://github.com/baisul/generateCode.git切换到master分支

2.环境

2.1 springboot+freemarker+mysql
2.2 要装node.js,vue文件运行依赖node.js

3.以下就只拿生成java实体类来作为例子

4.application.xml

server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root

5.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yl</groupId>
    <artifactId>generate_code</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>generate_code</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>30.1-jre</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

6.Utils

package com.yl.generate_code.utils;

import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

public class CORFSConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //所有请求都允许跨域
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("*")
                .allowedHeaders("*");
    }
}

package com.yl.generate_code.utils;

import com.yl.generate_code.model.Db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBUtils {
    private static Connection connection;
    public static Connection getConnection() {
        return connection;
    }

    public static Connection init(Db db) {
        if (connection == null) {
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                connection = DriverManager.getConnection( db.getUrl(),db.getUsername(), db.getPassword());
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        return connection;
    }
}

7.生成模板

package ${packetName}.model;

import java.util.Date;
/**
* @Description: ${modelName}实体类
* @Author: wfj
* @CreateDate ${createDate}
* @Version: 1.0V
*/
public class ${modelName} {
    <#if columns??>
        <#list columns as column>
            <#if column.type='VARCHAR' || column.type='TEXT' || column.type='CHAR'>
                /**
                * ${column.remark}
                */
                private String ${column.propertyName?uncap_first};
            </#if>
            <#if column.type='INT'>
                /**
                * ${column.remark}
                */
                private Integer ${column.propertyName?uncap_first};
            </#if>
            <#if column.type='DATETIME'>
                /**
                * ${column.remark}
                */
                private Date ${column.propertyName?uncap_first};
            </#if>
            <#if column.type='BIGINT'>
                /**
                * ${column.remark}
                */
                private Long ${column.propertyName?uncap_first};
            </#if>
            <#if column.type='DOUBLE'>
                /**
                * ${column.remark}
                */
                private Double ${column.propertyName?uncap_first};
            </#if>
            <#if column.type='BIT'>
                /**
                * ${column.remark}
                */
                private Boolean ${column.propertyName?uncap_first};
            </#if>
        </#list>
        <#list columns as column>
            <#if column.type='VARCHAR' || column.type='TEXT' || column.type='CHAR'>
               public String get${column.propertyName}() {
                    return ${column.propertyName?uncap_first};
                }
                public void set${column.propertyName}(String ${column.propertyName?uncap_first}) {
                    this.${column.propertyName?uncap_first} = ${column.propertyName?uncap_first};
                }
            </#if>
            <#if column.type='INT'>
                public Integer get${column.propertyName}() {
                return ${column.propertyName?uncap_first};
                }
                public void set${column.propertyName}(Integer ${column.propertyName?uncap_first}) {
                this.${column.propertyName?uncap_first} = ${column.propertyName?uncap_first};
                }
            </#if>
            <#if column.type='DATETIME'>
                public Date get${column.propertyName}() {
                return ${column.propertyName?uncap_first};
                }
                public void set${column.propertyName}(Date ${column.propertyName?uncap_first}) {
                this.${column.propertyName?uncap_first} = ${column.propertyName?uncap_first};
                }
            </#if>
            <#if column.type='BIGINT'>
                public Long get${column.propertyName}() {
                return ${column.propertyName?uncap_first};
                }
                public void set${column.propertyName}(Long ${column.propertyName?uncap_first}) {
                this.${column.propertyName?uncap_first} = ${column.propertyName?uncap_first};
                }
            </#if>
            <#if column.type='DOUBLE'>
                public Double get${column.propertyName}() {
                return ${column.propertyName?uncap_first};
                }
                public void set${column.propertyName}(Double ${column.propertyName?uncap_first}) {
                this.${column.propertyName?uncap_first} = ${column.propertyName?uncap_first};
                }
            </#if>
            <#if column.type='BIT'>
                public Boolean get${column.propertyName}() {
                return ${column.propertyName?uncap_first};
                }
                public void set${column.propertyName}(Boolean ${column.propertyName?uncap_first}) {
                this.${column.propertyName?uncap_first} = ${column.propertyName?uncap_first};
                }
            </#if>
        </#list>
    </#if>
}

8.Controller

package com.yl.generate_code.controller;

import com.google.common.base.CaseFormat;
import com.yl.generate_code.model.Db;
import com.yl.generate_code.model.ResultModel;
import com.yl.generate_code.model.TableClass;
import com.yl.generate_code.utils.DBUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class DBController {

    @PostMapping("/connect")
    public ResultModel connect(@RequestBody Db db) {
        Connection connection = DBUtils.init(db);
        if (connection == null) {
            return ResultModel.fail("数据库连接失败");
        } else {
            return ResultModel.success("数据库连接成功");
        }
    }

    @PostMapping("/config")
    public ResultModel config(@RequestBody Map<String,Object> map) {
        String packetName = (String)map.get("packetName");
        try {
            //获取数据库连接
            Connection connection = DBUtils.getConnection();
            //获取数据库元数据
            DatabaseMetaData metaData = connection.getMetaData();
            //获取数据库所有的表
            ResultSet rs = metaData.getTables(connection.getCatalog(), null, null, null);
            List<TableClass> list = new ArrayList<>();
            while (rs.next()) {
                TableClass tableClass = new TableClass();
                tableClass.setPacketName(packetName);
                //获取表名
                String tableName = rs.getString("TABLE_NAME");
                //数据库表下划线的字段转成驼峰,且首字母大写
                String modelName = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, tableName);
                tableClass.setTableName(tableName);
                tableClass.setModelName(modelName);
                tableClass.setServiceName(modelName + "Service");
                tableClass.setMapperName(modelName + "Mapper");
                tableClass.setControllerName(modelName + "Controller");
                list.add(tableClass);
            }
            return ResultModel.success("数据库信息读取成功",list);
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultModel.fail("数据库信息读取失败");
        }
    }
}

package com.yl.generate_code.controller;

import com.yl.generate_code.model.ResultModel;
import com.yl.generate_code.model.TableClass;
import com.yl.generate_code.service.GenerateCodeService;
import org.springframework.beans.factoryLodop打印设计界面生成代码带”...(省略)”

150+行Python代码实现带界面的数独游戏

150+行Python代码实现带界面的数独游戏!益智烧脑了解一下!

[vscode]--HTML代码片段(基础版,reactvuejquery)

iPhone SDK : 通过代码修改界面生成器制作的标签

postman 自动生成 curl 代码片段