新增: 获取生成文件路径

master
曹世达 6 months ago
parent 53a1bf0556
commit 7553b636cb

@ -26,6 +26,11 @@
<artifactId>logback-classic</artifactId> <artifactId>logback-classic</artifactId>
<version>1.4.14</version> <version>1.4.14</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId> <artifactId>fastjson</artifactId>

@ -1,9 +1,9 @@
package space.caoshd.otone.extension; package space.caoshd.otone.aware;
import space.caoshd.otone.entity.ColumnInfo; import space.caoshd.otone.entity.ColumnInfo;
import java.util.Map; import java.util.Map;
public interface ColumnAddonAble { public interface ColumnCreateAware {
void addon(ColumnInfo tableInfo, Map<String,Object> addInfo); void addon(ColumnInfo tableInfo, Map<String,Object> addInfo);
} }

@ -1,9 +1,9 @@
package space.caoshd.otone.extension; package space.caoshd.otone.aware;
import space.caoshd.otone.entity.TableInfo; import space.caoshd.otone.entity.TableInfo;
import java.util.Map; import java.util.Map;
public interface TableAddonAble { public interface TableCreateAware {
void addon(TableInfo tableInfo, Map<String, Object> addInfo); void addon(TableInfo tableInfo, Map<String, Object> addInfo);
} }

@ -3,14 +3,14 @@ package space.caoshd.otone.builder;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import space.caoshd.otone.entity.ColumnInfo; import space.caoshd.otone.entity.ColumnInfo;
import space.caoshd.otone.extension.ColumnAddonAble; import space.caoshd.otone.aware.ColumnCreateAware;
import space.caoshd.otone.tool.PropertyTools; import space.caoshd.otone.tool.PropTools;
import space.caoshd.otone.util.DataTypeUtils; import space.caoshd.otone.util.TypeUtils;
import space.caoshd.otone.util.DatabaseUtils; import space.caoshd.otone.util.DBUtils;
import space.caoshd.otone.util.JsonUtils; import space.caoshd.otone.util.JsonUtils;
import space.caoshd.otone.util.PropertiesConsts; import space.caoshd.otone.util.PathConsts;
import space.caoshd.otone.util.SqlConsts; import space.caoshd.otone.util.SqlConsts;
import space.caoshd.otone.util.StringUtils; import space.caoshd.otone.util.StrUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -21,8 +21,8 @@ public class ColumnBuilder {
private static final Logger LOGGER = LoggerFactory.getLogger(ColumnBuilder.class); private static final Logger LOGGER = LoggerFactory.getLogger(ColumnBuilder.class);
private static final PropertyTools sqlProperties = private static final PropTools sqlProperties =
new PropertyTools(PropertiesConsts.MYSQL_PROPERTIES_PATH); new PropTools(PathConsts.MYSQL_PROPERTIES_PATH);
private final String schemaName; private final String schemaName;
private final String tableName; private final String tableName;
@ -32,18 +32,16 @@ public class ColumnBuilder {
this.tableName = tableName; this.tableName = tableName;
} }
private final List<ColumnAddonAble> columnAddonAbles = new ArrayList<>(); private final List<ColumnCreateAware> columnCreateAwares = new ArrayList<>();
public ColumnBuilder addColumnAddonAble(ColumnCreateAware columnCreateAware) {
public ColumnBuilder addColumnAddonAble(ColumnAddonAble columnAddonAble) { this.columnCreateAwares.add(columnCreateAware);
this.columnAddonAbles.add(columnAddonAble);
return this; return this;
} }
public List<ColumnInfo> build() { public List<ColumnInfo> build() {
String sql = sqlProperties.getString(SqlConsts.COLUMN_SQL_SCHEMA_TABLE); String sql = sqlProperties.getString(SqlConsts.COLUMN_SQL_SCHEMA_TABLE);
List<String> params = Arrays.asList(schemaName, tableName); List<String> params = Arrays.asList(schemaName, tableName);
List<Map<String, String>> columns = DatabaseUtils.list(sql, params); List<Map<String, String>> columns = DBUtils.list(sql, params);
List<ColumnInfo> result = new ArrayList<>(); List<ColumnInfo> result = new ArrayList<>();
for (Map<String, String> column : columns) { for (Map<String, String> column : columns) {
@ -58,17 +56,17 @@ public class ColumnBuilder {
result.setComment(column.get(columnComment)); result.setComment(column.get(columnComment));
String columnName = sqlProperties.getString(SqlConsts.LABEL_COLUMN_NAME); String columnName = sqlProperties.getString(SqlConsts.LABEL_COLUMN_NAME);
result.setColumnName(column.get(columnName)); result.setColumnName(column.get(columnName));
String propertyName = StringUtils.toCamelCase(result.getColumnName(), false); String propertyName = StrUtils.toCamelCase(result.getColumnName(), false);
result.setPropertyName(propertyName); result.setPropertyName(propertyName);
String sqlDataType = sqlProperties.getString(SqlConsts.LABEL_DATA_TYPE); String sqlDataType = sqlProperties.getString(SqlConsts.LABEL_DATA_TYPE);
result.setSqlDataType(column.get(sqlDataType)); result.setSqlDataType(column.get(sqlDataType));
String javaDataType = DataTypeUtils.toJavaDataType(result.getSqlDataType()); String javaDataType = TypeUtils.toJavaDataType(result.getSqlDataType());
result.setJavaDataType(javaDataType); result.setJavaDataType(javaDataType);
String extra = sqlProperties.getString(SqlConsts.LABEL_EXTRA); String extra = sqlProperties.getString(SqlConsts.LABEL_EXTRA);
result.setAutoIncrement(column.get(extra).contains(SqlConsts.VALUE_AUTO_INCREMENT)); result.setAutoIncrement(column.get(extra).contains(SqlConsts.VALUE_AUTO_INCREMENT));
for (ColumnAddonAble columnAddonAble : columnAddonAbles) { for (ColumnCreateAware columnCreateAware : columnCreateAwares) {
columnAddonAble.addon(result, result.getAddonInfo()); columnCreateAware.addon(result, result.getAddonInfo());
} }
LOGGER.debug(JsonUtils.toJson(result)); LOGGER.debug(JsonUtils.toJson(result));

@ -3,10 +3,10 @@ package space.caoshd.otone.builder;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import space.caoshd.otone.entity.IndexInfo; import space.caoshd.otone.entity.IndexInfo;
import space.caoshd.otone.tool.PropertyTools; import space.caoshd.otone.tool.PropTools;
import space.caoshd.otone.util.DatabaseUtils; import space.caoshd.otone.util.DBUtils;
import space.caoshd.otone.util.JsonUtils; import space.caoshd.otone.util.JsonUtils;
import space.caoshd.otone.util.PropertiesConsts; import space.caoshd.otone.util.PathConsts;
import space.caoshd.otone.util.SqlConsts; import space.caoshd.otone.util.SqlConsts;
import java.util.ArrayList; import java.util.ArrayList;
@ -18,14 +18,14 @@ public class IndexBuilder {
private static final Logger LOGGER = LoggerFactory.getLogger(IndexBuilder.class); private static final Logger LOGGER = LoggerFactory.getLogger(IndexBuilder.class);
private static final PropertyTools sqlProperties = private static final PropTools sqlProperties =
new PropertyTools(PropertiesConsts.MYSQL_PROPERTIES_PATH); new PropTools(PathConsts.MYSQL_PROPERTIES_PATH);
public static List<IndexInfo> loadIndexInfo(String schemaName, String tableName) { public static List<IndexInfo> loadIndexInfo(String schemaName, String tableName) {
String sql = sqlProperties.getString(SqlConsts.INDEX_SQL_SCHEMA_TABLE); String sql = sqlProperties.getString(SqlConsts.INDEX_SQL_SCHEMA_TABLE);
List<String> params = Arrays.asList(schemaName, tableName); List<String> params = Arrays.asList(schemaName, tableName);
List<Map<String, String>> indexes = DatabaseUtils.list(sql, params); List<Map<String, String>> indexes = DBUtils.list(sql, params);
List<IndexInfo> result = new ArrayList<>(); List<IndexInfo> result = new ArrayList<>();
for (Map<String, String> index : indexes) { for (Map<String, String> index : indexes) {

@ -0,0 +1,52 @@
package space.caoshd.otone.builder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import space.caoshd.otone.entity.TableInfo;
import space.caoshd.otone.tool.PropTools;
import space.caoshd.otone.util.CfgConsts;
import space.caoshd.otone.util.PathConsts;
import space.caoshd.otone.util.VelocityUtils;
import java.nio.file.Paths;
import java.util.List;
public class PoBuilder {
private static final Logger LOGGER = LoggerFactory.getLogger(TableBuilder.class);
private static final PropTools propTools = new PropTools(PathConsts.CONFIG_PROPERTIES_PATH);
private final List<TableInfo> tableInfoList;
private String templatePath = PathConsts.PO_TEMPLATE_PATH;
private String outputBase = propTools.getString(CfgConsts.OUTPUT_BASE);
private String outputPo = propTools.getString(CfgConsts.OUTPUT_PO);
public PoBuilder(List<TableInfo> tableInfoList) {
this.tableInfoList = tableInfoList;
}
public PoBuilder setTemplatePath(String templatePath) {
this.templatePath = templatePath;
return this;
}
public PoBuilder setOutputBase(String outputBase) {
this.outputBase = outputBase;
return this;
}
public PoBuilder setOutputPo(String outputPo) {
this.outputPo = outputPo;
return this;
}
public void build() {
for (TableInfo tableInfo : tableInfoList) {
String beanName = tableInfo.getBeanName();
String outputPath = Paths.get(outputBase, outputPo, beanName + ".java").toString();
VelocityUtils.render(CfgConsts.VALUE_TABLE, tableInfo, templatePath, outputPath);
}
}
}

@ -5,16 +5,16 @@ import org.slf4j.LoggerFactory;
import space.caoshd.otone.entity.ColumnInfo; import space.caoshd.otone.entity.ColumnInfo;
import space.caoshd.otone.entity.IndexInfo; import space.caoshd.otone.entity.IndexInfo;
import space.caoshd.otone.entity.TableInfo; import space.caoshd.otone.entity.TableInfo;
import space.caoshd.otone.extension.ColumnAddonAble; import space.caoshd.otone.aware.ColumnCreateAware;
import space.caoshd.otone.extension.TableAddonAble; import space.caoshd.otone.aware.TableCreateAware;
import space.caoshd.otone.tool.PropertyTools; import space.caoshd.otone.tool.PropTools;
import space.caoshd.otone.util.ConfigConsts; import space.caoshd.otone.util.CfgConsts;
import space.caoshd.otone.util.DataTypeUtils; import space.caoshd.otone.util.TypeUtils;
import space.caoshd.otone.util.DatabaseUtils; import space.caoshd.otone.util.DBUtils;
import space.caoshd.otone.util.JsonUtils; import space.caoshd.otone.util.JsonUtils;
import space.caoshd.otone.util.PropertiesConsts; import space.caoshd.otone.util.PathConsts;
import space.caoshd.otone.util.SqlConsts; import space.caoshd.otone.util.SqlConsts;
import space.caoshd.otone.util.StringUtils; import space.caoshd.otone.util.StrUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -29,32 +29,31 @@ public class TableBuilder {
private static final Logger LOGGER = LoggerFactory.getLogger(TableBuilder.class); private static final Logger LOGGER = LoggerFactory.getLogger(TableBuilder.class);
private static final PropertyTools sqlProperties = private static final PropTools sqlProperties = new PropTools(PathConsts.MYSQL_PROPERTIES_PATH);
new PropertyTools(PropertiesConsts.MYSQL_PROPERTIES_PATH);
private static final PropertyTools configProperties = private static final PropTools configProperties =
new PropertyTools(PropertiesConsts.CONFIG_PROPERTIES_PATH); new PropTools(PathConsts.CONFIG_PROPERTIES_PATH);
private final List<TableAddonAble> tableAddonAbles = new ArrayList<>(); private final List<TableCreateAware> tableCreateAwares = new ArrayList<>();
public TableBuilder addTableAddonAble(TableAddonAble tableAddonAble) { public TableBuilder addTableAddonAble(TableCreateAware tableCreateAware) {
this.tableAddonAbles.add(tableAddonAble); this.tableCreateAwares.add(tableCreateAware);
return this; return this;
} }
private final List<ColumnAddonAble> columnAddonAbles = new ArrayList<>(); private final List<ColumnCreateAware> columnCreateAwares = new ArrayList<>();
public TableBuilder addColumnAddonAble(ColumnAddonAble columnAddonAble) { public TableBuilder addColumnAddonAble(ColumnCreateAware columnCreateAware) {
this.columnAddonAbles.add(columnAddonAble); this.columnCreateAwares.add(columnCreateAware);
return this; return this;
} }
public List<TableInfo> build() { public List<TableInfo> build() {
List<TableInfo> result = new ArrayList<>(); List<TableInfo> result = new ArrayList<>();
List<String> schemaNames = configProperties.getStrings(ConfigConsts.SCHEMA_NAMES); List<String> schemaNames = configProperties.getStrings(CfgConsts.SCHEMA_NAMES);
if (schemaNames.isEmpty()) { if (schemaNames.isEmpty()) {
String sql = sqlProperties.getString(SqlConsts.TABLE_SQL); String sql = sqlProperties.getString(SqlConsts.TABLE_SQL);
List<Map<String, String>> tables = DatabaseUtils.list(sql); List<Map<String, String>> tables = DBUtils.list(sql);
for (Map<String, String> table : tables) { for (Map<String, String> table : tables) {
result.add(createTableInfo(table)); result.add(createTableInfo(table));
} }
@ -69,11 +68,11 @@ public class TableBuilder {
public List<TableInfo> build(String schemaName) { public List<TableInfo> build(String schemaName) {
List<TableInfo> result = new ArrayList<>(); List<TableInfo> result = new ArrayList<>();
List<String> tableNames = configProperties.getStrings(ConfigConsts.TABLE_NAMES); List<String> tableNames = configProperties.getStrings(CfgConsts.TABLE_NAMES);
if (tableNames.isEmpty()) { if (tableNames.isEmpty()) {
String sql = sqlProperties.getString(SqlConsts.TABLE_SQL_SCHEMA); String sql = sqlProperties.getString(SqlConsts.TABLE_SQL_SCHEMA);
List<String> params = Collections.singletonList(schemaName); List<String> params = Collections.singletonList(schemaName);
List<Map<String, String>> tables = DatabaseUtils.list(sql, params); List<Map<String, String>> tables = DBUtils.list(sql, params);
for (Map<String, String> table : tables) { for (Map<String, String> table : tables) {
result.add(createTableInfo(table)); result.add(createTableInfo(table));
} }
@ -91,7 +90,7 @@ public class TableBuilder {
public TableInfo build(String schemaName, String tableName) { public TableInfo build(String schemaName, String tableName) {
String sql = sqlProperties.getString(SqlConsts.TABLE_SQL_SCHEMA_TABLE); String sql = sqlProperties.getString(SqlConsts.TABLE_SQL_SCHEMA_TABLE);
List<String> params = Arrays.asList(schemaName, tableName); List<String> params = Arrays.asList(schemaName, tableName);
Map<String, String> table = DatabaseUtils.one(sql, params); Map<String, String> table = DBUtils.one(sql, params);
if (Objects.isNull(table)) { if (Objects.isNull(table)) {
LOGGER.warn("table: [{}] not exists", tableName); LOGGER.warn("table: [{}] not exists", tableName);
return null; return null;
@ -115,35 +114,27 @@ public class TableBuilder {
result.setTableName(tableName); result.setTableName(tableName);
String tableNameWithoutPrefix = getTableNameWithoutPrefix(result.getTableName()); String tableNameWithoutPrefix = getTableNameWithoutPrefix(result.getTableName());
String beanName = StringUtils.toCamelCase(tableNameWithoutPrefix, true); String beanName = StrUtils.toCamelCase(tableNameWithoutPrefix, true);
result.setBeanName(beanName); result.setBeanName(beanName);
String suffix =
StringUtils.defaultIfBlank(
configProperties.getString(ConfigConsts.BEAN_SUFFIX),
ConfigConsts.VALUE_BEAN_SUFFIX
);
result.setQueryBeanName(beanName + suffix);
List<ColumnInfo> columnInfo = loadColumnInfo(tableSchema, tableName); List<ColumnInfo> columnInfo = loadColumnInfo(tableSchema, tableName);
result.setColumnInfo(columnInfo); result.setColumnInfo(columnInfo);
result.setDateExists(columnInfo.stream() result.setDateExists(columnInfo.stream()
.anyMatch(column -> DataTypeUtils.JAVA_TYPE_DATE.equals(column.getJavaDataType()))); .anyMatch(column -> TypeUtils.JAVA_TYPE_DATE.equals(column.getJavaDataType())));
result.setDecimalExists(columnInfo.stream() result.setDecimalExists(columnInfo.stream()
.anyMatch(column -> DataTypeUtils.JAVA_TYPE_DECIMAL.equals(column.getJavaDataType()))); .anyMatch(column -> TypeUtils.JAVA_TYPE_DECIMAL.equals(column.getJavaDataType())));
List<IndexInfo> indexInfoList = IndexBuilder.loadIndexInfo( List<IndexInfo> indexInfoList = IndexBuilder.loadIndexInfo(result.getTableSchema(),
result.getTableSchema(),
result.getTableName() result.getTableName()
); );
Map<String, List<ColumnInfo>> indexInfo = new HashMap<>(); Map<String, List<ColumnInfo>> indexInfo = new HashMap<>();
indexInfoList.forEach(index -> { indexInfoList.forEach(index -> {
List<ColumnInfo> indexColumnInfoList = indexInfo.computeIfAbsent( List<ColumnInfo> indexColumnInfoList = indexInfo.computeIfAbsent(index.getIndexName(),
index.getIndexName(),
k -> new ArrayList<>() k -> new ArrayList<>()
); );
ColumnInfo columnInfoByColumnName = getColumnInfoByColumnName( ColumnInfo columnInfoByColumnName = getColumnInfoByColumnName(columnInfo,
columnInfo,
index.getColumnName() index.getColumnName()
); );
indexColumnInfoList.add(columnInfoByColumnName); indexColumnInfoList.add(columnInfoByColumnName);
@ -151,8 +142,8 @@ public class TableBuilder {
result.setIndexInfo(indexInfo); result.setIndexInfo(indexInfo);
for (TableAddonAble tableAddonAble : tableAddonAbles) { for (TableCreateAware tableCreateAware : tableCreateAwares) {
tableAddonAble.addon(result, result.getAddonInfo()); tableCreateAware.addon(result, result.getAddonInfo());
} }
LOGGER.debug(JsonUtils.toJson(result)); LOGGER.debug(JsonUtils.toJson(result));
@ -161,8 +152,8 @@ public class TableBuilder {
private List<ColumnInfo> loadColumnInfo(String tableSchema, String tableName) { private List<ColumnInfo> loadColumnInfo(String tableSchema, String tableName) {
ColumnBuilder columnBuilder = new ColumnBuilder(tableSchema, tableName); ColumnBuilder columnBuilder = new ColumnBuilder(tableSchema, tableName);
for (ColumnAddonAble columnAddonAble : columnAddonAbles) { for (ColumnCreateAware columnCreateAware : columnCreateAwares) {
columnBuilder.addColumnAddonAble(columnAddonAble); columnBuilder.addColumnAddonAble(columnCreateAware);
} }
return columnBuilder.build(); return columnBuilder.build();
} }
@ -177,10 +168,10 @@ public class TableBuilder {
} }
private String getTableNameWithoutPrefix(String tableNameWithoutPrefix) { private String getTableNameWithoutPrefix(String tableNameWithoutPrefix) {
List<String> tablePrefixes = configProperties.getStrings(ConfigConsts.TABLE_PREFIXIES); List<String> tablePrefixes = configProperties.getStrings(CfgConsts.TABLE_PREFIXIES);
for (String tablePrefix : tablePrefixes) { for (String tablePrefix : tablePrefixes) {
tableNameWithoutPrefix = StringUtils.removePrefix(tableNameWithoutPrefix, tablePrefix); tableNameWithoutPrefix = StrUtils.removePrefix(tableNameWithoutPrefix, tablePrefix);
} }
return tableNameWithoutPrefix; return tableNameWithoutPrefix;
} }

@ -16,8 +16,6 @@ public class TableInfo {
private String beanName; private String beanName;
private String queryBeanName;
private Boolean dateExists; private Boolean dateExists;
private Boolean decimalExists; private Boolean decimalExists;
@ -60,14 +58,6 @@ public class TableInfo {
this.beanName = beanName; this.beanName = beanName;
} }
public String getQueryBeanName() {
return queryBeanName;
}
public void setQueryBeanName(String queryBeanName) {
this.queryBeanName = queryBeanName;
}
public Boolean getDateExists() { public Boolean getDateExists() {
return dateExists; return dateExists;
} }

@ -1,15 +1,10 @@
package space.caoshd.otone.tool; package space.caoshd.otone.tool;
import space.caoshd.otone.util.ResourceUtils; import space.caoshd.otone.util.ResUtils;
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.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -20,30 +15,37 @@ import java.util.Properties;
/** /**
* properties * properties
*/ */
public class PropertyTools { public class PropTools {
Map<String, String> propertyMap; private Map<String, String> propertyMap;
public PropertyTools(InputStream stream) { private Properties properties;
propertyMap = loadProperties(stream);
public PropTools(InputStream stream) {
loadProperties(stream);
parseProperties();
} }
public PropertyTools(String filename) { public PropTools(String filename) {
this(ResourceUtils.getClassPathFileStream(filename)); this(ResUtils.getClassPathFileStream(filename));
} }
public Map<String, String> loadProperties(InputStream stream) { private void loadProperties(InputStream stream) {
try {
Properties properties = new Properties(); Properties properties = new Properties();
try {
properties.load(stream); properties.load(stream);
Map<String, String> result = new HashMap<>(); this.properties = properties;
properties.forEach((k, v) -> result.put((String) k, (String) v));
return result;
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
public void parseProperties() {
Map<String, String> result = new HashMap<>();
properties.forEach((k, v) -> result.put((String) k, (String) v));
propertyMap = result;
}
public String getString(String key) { public String getString(String key) {
return Optional.ofNullable(propertyMap.get(key)).orElse(""); return Optional.ofNullable(propertyMap.get(key)).orElse("");
} }
@ -72,4 +74,12 @@ public class PropertyTools {
return result; return result;
} }
public Map<String, String> getPropertyMap() {
return propertyMap;
}
public Properties getProperties() {
return properties;
}
} }

@ -0,0 +1,89 @@
package space.caoshd.otone.util;
public class CfgConsts {
private CfgConsts() {}
public static final String SCHEMA_NAMES = "database.schemas";
public static final String TABLE_NAMES = "table.names";
public static final String TABLE_PREFIXIES = "table.prefixes";
public static final String BEAN_SUFFIX = "bean.suffix";
public static final String OUTPUT_BASE = "output.base";
public static final String OUTPUT_PO = "output.po";
public static final String PROJECT_PATH_BASE = "project.path.base";
public static final String PROJECT_PATH_JAVA = "project.path.java";
public static final String PROJECT_PATH_RESOURCES = "project.path.resources";
public static final String PACKAGE_BASE = "package.base";
public static final String PACKAGE_PO = "package.po";
public static final String PACKAGE_MAPPER = "package.mapper";
public static final String PACKAGE_SERVICE = "package.service";
public static final String PACKAGE_CONTROLLER = "package.controller";
public static final String PACKAGE_QUERY = "package.query";
public static final String PACKAGE_FORM = "package.form";
public static final String PACKAGE_VIEW = "package.view";
public static final String CLASS_NAME_SUFFIX_PO = "classname.suffix.po";
public static final String CLASS_NAME_SUFFIX_MAPPER = "classname.suffix.mapper";
public static final String CLASS_NAME_SUFFIX_SERVICE = "classname.suffix.service";
public static final String CLASS_NAME_SUFFIX_QUERY = "classname.suffix.query";
public static final String CLASS_NAME_SUFFIX_FORM = "classname.suffix.form";
public static final String CLASS_NAME_SUFFIX_VIEW = "classname.suffix.view";
public static final String CLASS_NAME_SUFFIX_CONTROLLER = "classname.suffix.controller";
public static final String VALUE_TABLE = "table";
public static final String DEFAULT_PROJECT_PATH_JAVA = "src/main/java";
public static final String DEFAULT_PROJECT_PATH_RESOURCES = "src/main/resources";
public static final String DEFAULT_PACKAGE_PO = "repository.po";
public static final String DEFAULT_PACKAGE_MAPPER = "repository.mapper";
public static final String DEFAULT_PACKAGE_SERVICE = "service";
public static final String DEFAULT_PACKAGE_QUERY = "controller.query";
public static final String DEFAULT_PACKAGE_FORM = "controller.form";
public static final String DEFAULT_PACKAGE_VIEW = "controller.view";
public static final String DEFAULT_PACKAGE_CONTROLLER = "controller";
public static final String DEFAULT_CLASS_NAME_SUFFIX_PO = "";
public static final String DEFAULT_CLASS_NAME_SUFFIX_MAPPER = "Mapper";
public static final String DEFAULT_CLASS_NAME_SUFFIX_SERVICE = "Service";
public static final String DEFAULT_CLASS_NAME_SUFFIX_QUERY = "Query";
public static final String DEFAULT_CLASS_NAME_SUFFIX_FORM = "Form";
public static final String DEFAULT_CLASS_NAME_SUFFIX_VIEW = "View";
public static final String DEFAULT_CLASS_NAME_SUFFIX_CONTROLLER = "Controller";
}

@ -1,16 +0,0 @@
package space.caoshd.otone.util;
public class ConfigConsts {
private ConfigConsts() {}
public static final String SCHEMA_NAMES = "database.schemas";
public static final String TABLE_NAMES = "table.names";
public static final String TABLE_PREFIXIES = "table.prefixes";
public static final String BEAN_SUFFIX = "bean.suffix";
public static final String VALUE_BEAN_SUFFIX = "Query";
}

@ -1,6 +1,6 @@
package space.caoshd.otone.util; package space.caoshd.otone.util;
import space.caoshd.otone.tool.PropertyTools; import space.caoshd.otone.tool.PropTools;
import java.io.InputStream; import java.io.InputStream;
import java.sql.Connection; import java.sql.Connection;
@ -13,19 +13,19 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class DatabaseUtils { public class DBUtils {
private DatabaseUtils() {} private DBUtils() {}
public static Connection getConnection() { public static Connection getConnection() {
try { try {
InputStream datasourcePropertiesStream = ResourceUtils.getClassPathFileStream( InputStream datasourcePropertiesStream = ResUtils.getClassPathFileStream(
PropertiesConsts.DATASOURCE_PROPERTIES_PATH); PathConsts.DATASOURCE_PROPERTIES_PATH);
PropertyTools propertyTools = new PropertyTools(datasourcePropertiesStream); PropTools propTools = new PropTools(datasourcePropertiesStream);
String username = propertyTools.getString("username"); String username = propTools.getString("username");
String password = propertyTools.getString("password"); String password = propTools.getString("password");
String driverClassName = propertyTools.getString("driver-class-name"); String driverClassName = propTools.getString("driver-class-name");
String url = propertyTools.getString("url"); String url = propTools.getString("url");
Class.forName(driverClassName); Class.forName(driverClassName);
return DriverManager.getConnection(url, username, password); return DriverManager.getConnection(url, username, password);
} catch (Exception e) { } catch (Exception e) {

@ -0,0 +1,20 @@
package space.caoshd.otone.util;
public class PathConsts {
public PathConsts() {}
public static final String DATASOURCE_PROPERTIES_PATH = "config/datasource.properties";
public static final String CONFIG_PROPERTIES_PATH = "config/config.properties";
public static final String MYSQL_PROPERTIES_PATH = "config/mysql.properties";
public static final String VELOCITY_PROPERTIES_PATH = "config/velocity.properties";
public static final String RESOURCES_PATH = "config/velocity.properties";
public static final String PO_TEMPLATE_PATH = "template/po.vm";
}

@ -0,0 +1,180 @@
package space.caoshd.otone.util;
import space.caoshd.otone.tool.PropTools;
import java.nio.file.Paths;
public class PathUtils {
private static final PropTools CONFIG = new PropTools(PathConsts.CONFIG_PROPERTIES_PATH);
private static final String JAVA_FILE_SUFFIX = ".java";
public static String getProjectPath() {
return CONFIG.getString(CfgConsts.PROJECT_PATH_BASE);
}
public static String getJavaSrcPath() {
String basePath = getProjectPath();
String configPath = CONFIG.getString(CfgConsts.PROJECT_PATH_JAVA);
String defaultPath = CfgConsts.DEFAULT_PROJECT_PATH_JAVA;
String path = StrUtils.defaultIfBlank(configPath, defaultPath);
return Paths.get(basePath, path).toString();
}
public static String getPackagePath() {
String basePath = getJavaSrcPath();
String name = CONFIG.getString(CfgConsts.PACKAGE_BASE);
String path = name.replaceAll("\\.", "/");
return Paths.get(basePath, path).toString();
}
public static String getPathBasedPackage(String configPath, String defaultPath) {
String basePath = getPackagePath();
String path = StrUtils.defaultIfBlank(configPath, defaultPath);
return Paths.get(basePath, path).toString();
}
public static String getServicePath(String className) {
String configPath = CONFIG.getString(CfgConsts.PACKAGE_SERVICE);
String defaultPath = CfgConsts.DEFAULT_PACKAGE_MAPPER;
return getPathBasedPackage(configPath, defaultPath);
}
public static String getControllerPath(String className) {
String configPath = CONFIG.getString(CfgConsts.PACKAGE_CONTROLLER);
String defaultPath = CfgConsts.DEFAULT_PACKAGE_CONTROLLER;
return getPathBasedPackage(configPath, defaultPath);
}
public static String getViewPath(String className) {
String configPath = CONFIG.getString(CfgConsts.PACKAGE_VIEW);
String defaultPath = CfgConsts.DEFAULT_PACKAGE_VIEW;
return getPathBasedPackage(configPath, defaultPath);
}
public static String getFormPath(String className) {
String configPath = CONFIG.getString(CfgConsts.PACKAGE_FORM);
String defaultPath = CfgConsts.DEFAULT_PACKAGE_FORM;
return getPathBasedPackage(configPath, defaultPath);
}
public static String getQueryPath(String className) {
String configPath = CONFIG.getString(CfgConsts.PACKAGE_QUERY);
String defaultPath = CfgConsts.DEFAULT_PACKAGE_QUERY;
return getPathBasedPackage(configPath, defaultPath);
}
public static String getPoPath() {
String configPath = CONFIG.getString(CfgConsts.PACKAGE_PO);
String defaultPath = CfgConsts.DEFAULT_PACKAGE_PO;
return getPathBasedPackage(configPath, defaultPath);
}
public static String getMapperPath() {
String configPath = CONFIG.getString(CfgConsts.PACKAGE_MAPPER);
String defaultPath = CfgConsts.DEFAULT_PACKAGE_MAPPER;
return getPathBasedPackage(configPath, defaultPath);
}
public static String getServicePath() {
String configPath = CONFIG.getString(CfgConsts.PACKAGE_SERVICE);
String defaultPath = CfgConsts.DEFAULT_PACKAGE_MAPPER;
return getPathBasedPackage(configPath, defaultPath);
}
public static String getControllerPath() {
String configPath = CONFIG.getString(CfgConsts.PACKAGE_CONTROLLER);
String defaultPath = CfgConsts.DEFAULT_PACKAGE_CONTROLLER;
return getPathBasedPackage(configPath, defaultPath);
}
public static String getViewPath() {
String configPath = CONFIG.getString(CfgConsts.PACKAGE_VIEW);
String defaultPath = CfgConsts.DEFAULT_PACKAGE_VIEW;
return getPathBasedPackage(configPath, defaultPath);
}
public static String getFormPath() {
String configPath = CONFIG.getString(CfgConsts.PACKAGE_FORM);
String defaultPath = CfgConsts.DEFAULT_PACKAGE_FORM;
return getPathBasedPackage(configPath, defaultPath);
}
public static String getQueryPath() {
String configPath = CONFIG.getString(CfgConsts.PACKAGE_QUERY);
String defaultPath = CfgConsts.DEFAULT_PACKAGE_QUERY;
return getPathBasedPackage(configPath, defaultPath);
}
private static String getFilename(String className, String suffix) {
return className + suffix + JAVA_FILE_SUFFIX;
}
public static String getPoJavaPath(String className) {
String basePath = getPoPath();
String configSuffix = CONFIG.getString(CfgConsts.CLASS_NAME_SUFFIX_PO);
String defaultSuffix = CfgConsts.DEFAULT_CLASS_NAME_SUFFIX_PO;
String suffix = StrUtils.defaultIfBlank(configSuffix, defaultSuffix);
String filename = getFilename(className, defaultSuffix);
return Paths.get(basePath, filename).toString();
}
public static String getMapperJavaPath(String className) {
String basePath = getMapperPath();
String configSuffix = CONFIG.getString(CfgConsts.CLASS_NAME_SUFFIX_MAPPER);
String defaultSuffix = CfgConsts.DEFAULT_CLASS_NAME_SUFFIX_MAPPER;
String suffix = StrUtils.defaultIfBlank(configSuffix, defaultSuffix);
String filename = getFilename(className, suffix);
return Paths.get(basePath, filename).toString();
}
public static String getServiceJavaPath(String className) {
String basePath = getServicePath();
String configSuffix = CONFIG.getString(CfgConsts.CLASS_NAME_SUFFIX_SERVICE);
String defaultSuffix = CfgConsts.DEFAULT_CLASS_NAME_SUFFIX_SERVICE;
String suffix = StrUtils.defaultIfBlank(configSuffix, defaultSuffix);
String filename = getFilename(className, suffix);
return Paths.get(basePath, filename).toString();
}
public static String getControllerJavaPath(String className) {
String basePath = getControllerPath();
String configSuffix = CONFIG.getString(CfgConsts.CLASS_NAME_SUFFIX_CONTROLLER);
String defaultSuffix = CfgConsts.DEFAULT_CLASS_NAME_SUFFIX_CONTROLLER;
String suffix = StrUtils.defaultIfBlank(configSuffix, defaultSuffix);
String filename = getFilename(className, suffix);
return Paths.get(basePath, filename).toString();
}
public static String getFormJavaPath(String className) {
String basePath = getFormPath();
String configSuffix = CONFIG.getString(CfgConsts.CLASS_NAME_SUFFIX_CONTROLLER);
String defaultSuffix = CfgConsts.DEFAULT_CLASS_NAME_SUFFIX_CONTROLLER;
String suffix = StrUtils.defaultIfBlank(configSuffix, defaultSuffix);
String filename = getFilename(className, suffix);
return Paths.get(basePath, filename).toString();
}
public static String getQueryJavaPath(String className) {
String basePath = getQueryPath();
String configSuffix = CONFIG.getString(CfgConsts.CLASS_NAME_SUFFIX_QUERY);
String defaultSuffix = CfgConsts.DEFAULT_CLASS_NAME_SUFFIX_QUERY;
String suffix = StrUtils.defaultIfBlank(configSuffix, defaultSuffix);
String filename = getFilename(className, suffix);
return Paths.get(basePath, filename).toString();
}
public static String getViewJavaPath(String className) {
String basePath = getViewPath();
String configSuffix = CONFIG.getString(CfgConsts.CLASS_NAME_SUFFIX_VIEW);
String defaultSuffix = CfgConsts.DEFAULT_CLASS_NAME_SUFFIX_VIEW;
String suffix = StrUtils.defaultIfBlank(configSuffix, defaultSuffix);
String filename = getFilename(className, suffix);
return Paths.get(basePath, filename).toString();
}
}

@ -1,13 +0,0 @@
package space.caoshd.otone.util;
public class PropertiesConsts {
public PropertiesConsts() {}
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";
}

@ -4,14 +4,16 @@ import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
public class ResourceUtils { public class ResUtils {
public static InputStream getClassPathFileStream(String filename) { public static InputStream getClassPathFileStream(String filename) {
return ResourceUtils.class.getClassLoader().getResourceAsStream(filename); return ResUtils.class.getClassLoader().getResourceAsStream(filename);
} }
public static File getClassPathFile(String filename) { public static File getClassPathFile(String filename) {
URL resource = ResourceUtils.class.getClassLoader().getResource(filename); URL resource = ResUtils.class.getClassLoader().getResource(filename);
assert resource != null; assert resource != null;
return new File(resource.getFile()); return new File(resource.getFile());
} }
} }

@ -1,8 +1,8 @@
package space.caoshd.otone.util; package space.caoshd.otone.util;
public class StringUtils { public class StrUtils {
private StringUtils() {} private StrUtils() {}
public static String last(String str, String regex) { public static String last(String str, String regex) {
String[] split = str.split(regex); String[] split = str.split(regex);

@ -4,7 +4,7 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
public class DataTypeUtils { public class TypeUtils {
public static final List<String> SQL_TYPE_DATE_TIME = Arrays.asList("datetime", "timestamp"); public static final List<String> SQL_TYPE_DATE_TIME = Arrays.asList("datetime", "timestamp");

@ -0,0 +1,32 @@
package space.caoshd.otone.util;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import space.caoshd.otone.tool.PropTools;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
public class VelocityUtils {
public static void render(
String beanName, Object bean, String templatePath, String outputPath
) {
PropTools propTools = new PropTools(PathConsts.VELOCITY_PROPERTIES_PATH);
Properties properties = propTools.getProperties();
Velocity.init(properties);
VelocityContext context = new VelocityContext();
context.put(beanName, bean);
Template template = Velocity.getTemplate(templatePath, StandardCharsets.UTF_8.name());
try (FileWriter fw = new FileWriter(outputPath)) {
template.merge(context, fw);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

@ -1,4 +0,0 @@
#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_
bean.suffix=Query

@ -1,13 +0,0 @@
table.sql=select * from information_schema.tables where TABLE_SCHEMA not in ('mysql', 'information_schema', 'performance_schema', 'sys')
table.sql.schema=select * from information_schema.tables where table_schema = ?
table.sql.schema.table=select * from information_schema.tables where table_schema = ? and table_name = ?
column.sql.schema.table=select * from information_schema.columns where TABLE_SCHEMA = ? and table_name = ?
label.table_schema=TABLE_SCHEMA
label.table_name=TABLE_NAME
label.table_comment=TABLE_COMMENT
label.column_name=COLUMN_NAME
label.column_comment=COLUMN_COMMENT
label.data_type=DATA_TYPE

@ -0,0 +1,17 @@
package space.caoshd.otone.builder;
import org.junit.jupiter.api.Test;
import space.caoshd.otone.entity.TableInfo;
import java.util.List;
class PoBuilderTest {
@Test
void build() {
TableBuilder tableBuilder = new TableBuilder();
List<TableInfo> tableInfoList = tableBuilder.build();
PoBuilder poBuilder = new PoBuilder(tableInfoList);
poBuilder.build();
}
}

@ -0,0 +1,40 @@
package space.caoshd.otone.tool;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import space.caoshd.otone.util.CfgConsts;
import space.caoshd.otone.util.PathConsts;
import space.caoshd.otone.util.ResUtils;
import java.io.*;
import java.util.List;
class PropToolsTest {
@Test
void loadProperties() {
InputStream fileStream = ResUtils.getClassPathFileStream(PathConsts.DATASOURCE_PROPERTIES_PATH);
PropTools propTools = new PropTools(fileStream);
Assertions.assertNotNull(propTools);
}
@Test
void getString() {
}
@Test
void getStrings() {
InputStream fileStream = ResUtils.getClassPathFileStream(PathConsts.CONFIG_PROPERTIES_PATH);
PropTools configProperties = new PropTools(fileStream);
List<String> strings = configProperties.getStrings(CfgConsts.TABLE_PREFIXIES);
System.out.println(strings);
}
@Test
void getInt() {
}
@Test
void getLong() {
}
}

@ -1,40 +0,0 @@
package space.caoshd.otone.tool;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import space.caoshd.otone.util.ConfigConsts;
import space.caoshd.otone.util.PropertiesConsts;
import space.caoshd.otone.util.ResourceUtils;
import java.io.*;
import java.util.List;
class PropertyToolsTest {
@Test
void loadProperties() {
InputStream fileStream = ResourceUtils.getClassPathFileStream(PropertiesConsts.DATASOURCE_PROPERTIES_PATH);
PropertyTools propertyTools = new PropertyTools(fileStream);
Assertions.assertNotNull(propertyTools);
}
@Test
void getString() {
}
@Test
void getStrings() {
InputStream fileStream = ResourceUtils.getClassPathFileStream(PropertiesConsts.CONFIG_PROPERTIES_PATH);
PropertyTools configProperties = new PropertyTools(fileStream);
List<String> strings = configProperties.getStrings(ConfigConsts.TABLE_PREFIXIES);
System.out.println(strings);
}
@Test
void getInt() {
}
@Test
void getLong() {
}
}

@ -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.PropTools;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
class DBUtilsTest {
@Test
void executeQuery() {
PropTools sqlProperties = new PropTools(PathConsts.MYSQL_PROPERTIES_PATH);
String sql = sqlProperties.getString(SqlConsts.TABLE_SQL_SCHEMA_TABLE);
PropTools configProperties =
new PropTools(PathConsts.CONFIG_PROPERTIES_PATH);
List<String> schemas = configProperties.getStrings(CfgConsts.SCHEMA_NAMES);
List<String> tables = configProperties.getStrings(CfgConsts.TABLE_NAMES);
List<Object> params = Arrays.asList(schemas.get(0), tables.get(0));
List<Map<String, String>> maps = DBUtils.list(sql, params);
Assertions.assertFalse(maps.isEmpty());
}
}

@ -1,25 +0,0 @@
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(PropertiesConsts.MYSQL_PROPERTIES_PATH);
String sql = sqlProperties.getString(SqlConsts.TABLE_SQL_SCHEMA_TABLE);
PropertyTools configProperties =
new PropertyTools(PropertiesConsts.CONFIG_PROPERTIES_PATH);
List<String> schemas = configProperties.getStrings(ConfigConsts.SCHEMA_NAMES);
List<String> tables = configProperties.getStrings(ConfigConsts.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,18 @@
package space.caoshd.otone.util;
import org.junit.jupiter.api.Test;
class PathUtilsTest {
@Test
void getProjectPath() {
String projectPath = PathUtils.getProjectPath();
System.out.println(projectPath);
}
@Test
void getPackagePath() {
String packagePath = PathUtils.getPackagePath();
System.out.println(packagePath);
}
}

@ -4,11 +4,11 @@ import org.junit.jupiter.api.Test;
import java.io.File; import java.io.File;
class ResourceUtilsTest { class ResUtilsTest {
@Test @Test
void getClassPathFile() { void getClassPathFile() {
File file = ResourceUtils.getClassPathFile("datasource.properties"); File file = ResUtils.getClassPathFile("datasource.properties");
System.out.println(file); System.out.println(file);
} }

@ -2,9 +2,7 @@ package space.caoshd.otone.util;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*; class StrUtilsTest {
class StringUtilsTest {
@Test @Test
void last() { void last() {
@ -16,13 +14,13 @@ class StringUtilsTest {
@Test @Test
void removePrefix(){ void removePrefix(){
String username = StringUtils.removePrefix("t_user_name", "t_"); String username = StrUtils.removePrefix("t_user_name", "t_");
System.out.println(username); System.out.println(username);
} }
@Test @Test
void toCamelCase() { void toCamelCase() {
String userName = StringUtils.toCamelCase("user_name", false); String userName = StrUtils.toCamelCase("user_name", false);
String className = StringUtils.toCamelCase("class_name", true); String className = StrUtils.toCamelCase("class_name", true);
System.out.println(userName); System.out.println(userName);
System.out.println(className); System.out.println(className);
} }

@ -1,4 +0,0 @@
#database.schemas=otone
#table.names=t_file_export_instance,t_file_export_path,t_file_export_type_text,t_file_export_workspace
#bean.suffix=Query
table.prefixes=t_,m_

@ -0,0 +1,23 @@
project.path.base=D:/workspace/otono-debug/
package.base=space.caoshd.navigator
#database.schemas=otone
#table.names=
#table.prefixes=
#project.path.java=src/main/java
#project.path.resources=src/main/resources
#project.path.mapper=
#package.po=repository.po
#package.mapper=repository.mapper
#package.service=service
#package.query=controller.query
#package.form=controller.form
#package.view=controller.view
#package.controller=controller
#classname.suffix.service=Service
#classname.suffix.Controller=Controller
#classname.suffix.view=View
#classname.suffix.form=Form
#classname.suffix.query=Query
#classname.suffix.mapper=Mapper
#classname.suffix.po=

@ -0,0 +1 @@
file.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader

@ -1,4 +0,0 @@
username=root
password=123456
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
Loading…
Cancel
Save