验证中...
sqlUtil 工具类
Raw Copy
public class SqlUtil {
/**
* 建表语句
* @author Administrator
*
*/
public static String createTable(String tableName , List<FieldName> fieldNameList){
String sqlCreate = "CREATE TABLE " + tableName + "(" + getRowName(fieldNameList);//生成建表语句
return sqlCreate;
}
/**
* 建表语句的列名
*
* @author Administrator
*/
public static String getRowName(List<FieldName> fieldNameList) {
StringBuffer sqlRowNameBuffer = new StringBuffer();
try {
sqlRowNameBuffer.append("id VARCHAR(40) PRIMARY KEY,");
for (FieldName fieldName : fieldNameList) {
Options options = fieldName.getOptions();
String nameType = null;// 字段类型
String nameE = fieldName.getNameE();// 获取字段名
String dataType = options.getDataTypeCopy();// 获取json字段类型
if (dataType.equals("_string") || dataType.equals("_url") || dataType.equals("_email")) {
nameType = SqlFieldType._string + "(" + options.getStringNum() + ")";
} else if (dataType.equals("_number")) {
nameType = SqlFieldType._number;
} else if (dataType.equals("_boolean")) {
nameType = SqlFieldType._boolean;
} else if (dataType.equals("_float")) {
nameType = SqlFieldType._float;
} else if (dataType.equals("_integer")) {
nameType = SqlFieldType._integer;
}
// sqlRowNameBuffer.append(nameE).append(" ").append(nameType).append("
// ").append("NOT NULL,");
sqlRowNameBuffer.append(nameE).append(" ").append(nameType).append(" ").append(",");
}
sqlRowNameBuffer.deleteCharAt(sqlRowNameBuffer.length() - 1);
sqlRowNameBuffer.append(")");
} catch (Exception e) {
e.getMessage();
}
return sqlRowNameBuffer.toString();
}
/**
* 生成字段注释语句
* @param tableFormat
* @return
*/
public static String fieldComment(TableFormat tableFormat) {
StringBuffer sqlRowNameBuffer = new StringBuffer();
try {
FormTitle formTitle = tableFormat.getFormTitle();
sqlRowNameBuffer.append(
"COMMENT ON TABLE " + formTitle.getNameEnglish() + " IS '" + formTitle.getNameChina() + "';");
for (FieldName fieldName : tableFormat.getList()) {
String nameE = fieldName.getNameE();// 获取字段名
String name = fieldName.getName();// 获取字段名注释
sqlRowNameBuffer.append("COMMENT ON COLUMN ").append(formTitle.getNameEnglish()).append(".")
.append(nameE).append(" IS").append(" ").append("'" + name + "';");
}
} catch (Exception e) {
e.getMessage();
}
return sqlRowNameBuffer.toString();
}
/**
* 转化成JSON对象
*/
public static String toJSONString(Object object) {
return JSON.toJSONString(object, SerializerFeature.DisableCircularReferenceDetect);// DisableCircularReferenceDetect防止循环引用
}
/**
* 转化成java对象
*/
public static <T> T toJAVAObject(String json, Class<T> object) {
return JSON.parseObject(json, object);
}
}
Service 创建表语句 和 创建表字段注释的语句
Raw Copy
@Service
public class Service {
@Autowired
private JdbcTemplate jdbcTemplate;
/**
* 创建表格
*
* @param jsonString
* json字符串
* @return
*/
public String parseJson(String jsonString) {
try {
jsonString = StringEscapeUtils.unescapeJava(jsonString);
TableFormat tableFormat = SqlUtil.toJAVAObject(jsonString, TableFormat.class);
String createTableName = tableFormat.getFormTitle().getNameEnglish();
// 是否已经存在相同表名的文件
List<TableNameAndComment> tableNameList = getAllTableName();//查询数据库中所有的表名和注释
for(TableNameAndComment tableNameAndComment:tableNameList) {
if (tableNameAndComment.getTabName().equals(createTableName)) {//判断表名是否重复
return "表名已经存在,请勿重复创建";
}
}
// tableStructure(tableFormat.getFormTitle().getNameEnglish());//查询数据表表结构
String sqlCreateTable = SqlUtil.createTable(createTableName,tableFormat.getList());// 建表
String sqlCreateAnnotation = SqlUtil.fieldComment(tableFormat);//添加字段注释
jdbcTemplate.execute(sqlCreateTable+";"+sqlCreateAnnotation);// 执行创建表语句
return sqlCreateTable;
} catch (Exception e) {
e.getMessage();
return "创建表格失败";
}
}
}
SqlFieldType 匹配数据类型
Raw Copy
public class SqlFieldType {
public static String _string = "VARCHAR";
public static String _number = "FLOAT";
public static String _boolean = "BOOL";
public static String _integer = "INT";
public static String _float = "FLOAT";
public static String _url = "VARCHAR";
public static String _email = "VARCHAR";
}
Application
Raw Copy
@RestController
@SpringBootApplication
@RequestMapping("/")
@PropertySource("classpath:config.properties")
public class Application extends BaseService{
@Resource
Service Service;
public static void main( String[] args )
{
SpringApplication.run(Application.class, args);
}
/**
* 创建表
* @param jsonString
* @return
*/
@PostMapping(path = "create/table/json", consumes = "application/json")
@ResponseBody
public String parseJson(@RequestBody String jsonString) {
return informationService.parseJson(jsonString);
}
}
config.properties
Raw Copy
##mysql
#spring.datasource.url=jdbc:mysql://127.0.0.1:3306/sqlName?useUnicode=true&characterEncoding=utf8&useSSL=true&autoReconnect=true&rewriteBatchedStatements=true
#spring.datasource.username=root
#spring.datasource.password=
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#pgSql
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=root
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.show-sql=true
#start tomcat port config
server.port : 16002
pom.xml
Raw Copy
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.information.service</groupId>
<artifactId>information</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>information</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<com.alibaba.dubbo.version>2.5.4</com.alibaba.dubbo.version>
<org.apache.zookeeper.version>3.4.9</org.apache.zookeeper.version>
<zkclient_version>0.2</zkclient_version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- 发布时去除内置tomcat -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-websocket</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk16</artifactId>
<version>1.46</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils-core</artifactId>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.39</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
</dependency>
<!-- Spring-jdbc 用于配置JdbcTemplate -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
</dependencies>
<resources>
<resource>
<directory>lib</directory>
<targetPath>BOOT-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</project>

Comment list( 0 )

You need to Sign in for post a comment

Help Search

183227_9af5e6a8_1826025 111910_4d91f001_1826025