新增: 加载表方法

master
曹世达 6 months ago
parent 385ea82755
commit ad79acc598

@ -0,0 +1,69 @@
package space.caoshd.otone.entity;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class TableInfo {
private String comment;
private String tableName;
private String beanName;
private String beanParamName;
private List<FieldInfo> fieldInfoList = new ArrayList<>();
private Map<String, List<FieldInfo>> indexMap = new LinkedHashMap<>();
private Boolean booleanExists;
private Boolean dateExists;
private Boolean dateTimeExists;
private Boolean bigDecimalExists;
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public String getBeanName() {
return beanName;
}
public void setBeanName(String beanName) {
this.beanName = beanName;
}
public String getBeanParamName() {
return beanParamName;
}
public void setBeanParamName(String beanParamName) {
this.beanParamName = beanParamName;
}
public List<FieldInfo> getFieldInfoList() {
return fieldInfoList;
}
public void setFieldInfoList(List<FieldInfo> fieldInfoList) {
this.fieldInfoList = fieldInfoList;
}
}

@ -1,9 +1,20 @@
package space.caoshd.otone.tool; package space.caoshd.otone.tool;
import space.caoshd.otone.util.ResourceUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties; import java.util.Properties;
/** /**
@ -17,6 +28,10 @@ public class PropertyTools {
propertyMap = loadProperties(stream); propertyMap = loadProperties(stream);
} }
public PropertyTools(String filename) {
this(ResourceUtils.getClassPathFileStream(filename));
}
public Map<String, String> loadProperties(InputStream stream) { public Map<String, String> loadProperties(InputStream stream) {
try { try {
Properties properties = new Properties(); Properties properties = new Properties();
@ -30,15 +45,31 @@ public class PropertyTools {
} }
public String getString(String key) { public String getString(String key) {
return propertyMap.get(key); return Optional.ofNullable(propertyMap.get(key)).orElse("");
} }
public Integer getInt(String key) { public Integer getInt(String key) {
return Integer.valueOf(propertyMap.get(key)); return Integer.valueOf(propertyMap.get(key));
} }
public Boolean getBoolean(String key) {
return Boolean.valueOf(propertyMap.get(key));
}
public Long getLong(String key) { public Long getLong(String key) {
return Long.valueOf(propertyMap.get(key)); return Long.valueOf(propertyMap.get(key));
} }
public List<String> getStrings(String key) {
List<String> result = new ArrayList<>();
String values = propertyMap.get(key);
if (Objects.isNull(values)) {
return result;
}
for (String value : values.split(",")) {
result.add(value.trim());
}
return result;
}
} }

@ -0,0 +1,12 @@
package space.caoshd.otone.util;
public class ConfigConstants {
public static final String SCHEMA_NAMES = "database.schemas";
public static final String TABLE_NAMES = "table.names";
public static final String TABLE_PREFIXIES = "table.prefixes";
}

@ -1,11 +0,0 @@
package space.caoshd.otone.util;
public class Constants {
public Constants() {
}
public static final String DATASOURCE_PATH = "datasource.properties";
public static final String CONFIG_PATH = "config.properties";
}

@ -0,0 +1,13 @@
package space.caoshd.otone.util;
public class PropertiesConstants {
public PropertiesConstants() {}
public static final String DATASOURCE_PROPERTIES_PATH = "datasource.properties";
public static final String CONFIG_PROPERTIES_PATH = "config.properties";
public static final String MYSQL_PROPERTIES_PATH = "mysql.properties";
}

@ -0,0 +1,3 @@
database.schemas=otone
table.names=t_file_export_instance,t_file_export_path,t_file_export_type_text,t_file_export_workspace
table.prefixes=t_,m_

@ -1,4 +1,4 @@
username=root username=root
password=123456 password=123456
url=jdbc:mysql://localhost:3306/otone?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai url=jdbc:mysql://192.168.0.100:3306/otone?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
driver-class-name=com.mysql.cj.jdbc.Driver driver-class-name=com.mysql.cj.jdbc.Driver

@ -0,0 +1,5 @@
table.detail.sql=select * from information_schema.tables
table.detail.sql.schema=select * from information_schema.tables where table_schema = ?
table.detail.sql.schema.table=select * from information_schema.tables where table_schema = ? and table_name = ?
table.detail.label.table-name=TABLE_NAME
table.detail.label.table-comment=TABLE_COMMENT

@ -2,20 +2,18 @@ package space.caoshd.otone.tool;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import space.caoshd.otone.util.Constants; import space.caoshd.otone.util.ConfigConstants;
import space.caoshd.otone.util.PropertiesConstants;
import space.caoshd.otone.util.ResourceUtils; import space.caoshd.otone.util.ResourceUtils;
import java.io.*; import java.io.*;
import java.nio.file.Files; import java.util.List;
import java.nio.file.Paths;
import static org.junit.jupiter.api.Assertions.*;
class PropertyToolsTest { class PropertyToolsTest {
@Test @Test
void loadProperties() { void loadProperties() {
InputStream fileStream = ResourceUtils.getClassPathFileStream(Constants.DATASOURCE_PATH); InputStream fileStream = ResourceUtils.getClassPathFileStream(PropertiesConstants.DATASOURCE_PROPERTIES_PATH);
PropertyTools propertyTools = new PropertyTools(fileStream); PropertyTools propertyTools = new PropertyTools(fileStream);
Assertions.assertNotNull(propertyTools); Assertions.assertNotNull(propertyTools);
} }
@ -24,6 +22,14 @@ class PropertyToolsTest {
void getString() { void getString() {
} }
@Test
void getStrings() {
InputStream fileStream = ResourceUtils.getClassPathFileStream(PropertiesConstants.CONFIG_PROPERTIES_PATH);
PropertyTools configProperties = new PropertyTools(fileStream);
List<String> strings = configProperties.getStrings(ConfigConstants.TABLE_PREFIXIES);
System.out.println(strings);
}
@Test @Test
void getInt() { void getInt() {
} }

@ -0,0 +1,25 @@
package space.caoshd.otone.util;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import space.caoshd.otone.tool.PropertyTools;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
class DatabaseUtilsTest {
@Test
void executeQuery() {
PropertyTools sqlProperties = new PropertyTools(PropertiesConstants.MYSQL_PROPERTIES_PATH);
String sql = sqlProperties.getString(SqlConstants.TABLE_DETAIL_SQL_SCHEMA_TABLE);
PropertyTools configProperties =
new PropertyTools(PropertiesConstants.CONFIG_PROPERTIES_PATH);
List<String> schemas = configProperties.getStrings(ConfigConstants.SCHEMA_NAMES);
List<String> tables = configProperties.getStrings(ConfigConstants.TABLE_NAMES);
List<Object> params = Arrays.asList(schemas.get(0), tables.get(0));
List<Map<String, String>> maps = DatabaseUtils.list(sql, params);
Assertions.assertFalse(maps.isEmpty());
}
}

@ -0,0 +1,3 @@
database.schemas=otone
table.names=t_file_export_instance,t_file_export_path,t_file_export_type_text,t_file_export_workspace
table.prefixes=t_,m_

@ -1,4 +1,4 @@
username=root username=root
password=123456 password=123456
url=jdbc:mysql://localhost:3306/otone?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai url=jdbc:mysql://192.168.0.100:3306/otone?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
driver-class-name=com.mysql.cj.jdbc.Driver driver-class-name=com.mysql.cj.jdbc.Driver

@ -0,0 +1,5 @@
table.detail.sql=select * from information_schema.tables
table.detail.sql.schema=select * from information_schema.tables where table_schema = ?
table.detail.sql.schema.table=select * from information_schema.tables where table_schema = ? and table_name = ?
table.detail.label.table-name=TABLE_NAME
table.detail.label.table-comment=TABLE_COMMENT
Loading…
Cancel
Save