Java Code Examples for com.gitee.hengboy.builder.common.util.StringUtil

The following examples show how to use com.gitee.hengboy.builder.common.util.StringUtil. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: code-builder   Source File: AbstractEngineTemplate.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 获取生成目标目录 + package目录格式化后的根地址
 *
 * @return 存在包名的目录根路径
 */
protected String getBasePackageTargetDir() {
    // 获取builder配置信息
    BuilderConfiguration builderConfiguration = codeBuilderProperties.getBuilder();
    StringBuffer basePackageDir = new StringBuffer();
    // 生成文件的目标根路径
    basePackageDir.append(getBaseTargetDir());

    // 是否存在自定义的package前缀
    // 存在前缀添加到路径内
    if (StringUtil.isNotEmpty(builderConfiguration.getDiffSysPackagePrefix())) {
        basePackageDir.append(builderConfiguration.getDiffSysPackagePrefix());
    }
    // 系统分隔符
    basePackageDir.append(SEPARATOR);
    return basePackageDir.toString();
}
 
Example 2
Source Project: code-builder   Source File: AbstractEngineTemplate.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 获取新文件的全名称
 * 如:XxxEntity.java
 *
 * @param templateConfiguration 模板配置对象
 * @param entityName            数据表对应的实体名称
 * @return 新文件名称
 */
protected String getTemplateNewFileName(TemplateConfiguration templateConfiguration, String entityName) {
    StringBuffer fileName = new StringBuffer();
    // 目标package的根目录
    fileName.append(getBasePackageTargetDir());

    // 是否配置了模板创建文件后所属的package目录
    if (StringUtil.isNotEmpty(templateConfiguration.getPackageName())) {
        fileName.append(templateConfiguration.getPackageName());
        fileName.append(SEPARATOR);
    }
    // 类名
    fileName.append(getTemplateClassName(templateConfiguration, entityName));
    // 文件扩展名
    fileName.append(FILE_SUFFIX);
    return fileName.toString();
}
 
Example 3
Source Project: code-builder   Source File: Table.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 构建对象后设置是否存在特殊类型的字段
 * 如:java.math.BigDecimal、java.sql.TimeStamp等
 *
 * @param codeBuilderProperties 参数对象
 * @return Table实例
 */
public Table buildAfterSetting(CodeBuilderProperties codeBuilderProperties) {
    for (Column column : columns) {
        // 是否存在bigDecimal的列
        if (JavaTypeEnum.TYPE_BIG_DECIMAL.getFullName().equals(column.getFullJavaType())) {
            this.hasBigDecimal = true;
        }
        // 是否存在timeStamp的列
        if (JavaTypeEnum.TYPE_TIMESTAMP.getFullName().equals(column.getFullJavaType())) {
            this.hasTimeStamp = true;
        }
        // 是否存在java.sql.Date的列
        if (JavaTypeEnum.TYPE_DATE.getFullName().equals(column.getFullJavaType())) {
            this.hasSqlDate = true;
        }
    }
    // 自动忽略前缀
    if (StringUtil.isNotEmpty(codeBuilderProperties.getIgnoreClassPrefix())) {
        entityName = entityName.replaceFirst(codeBuilderProperties.getIgnoreClassPrefix(), "");
    }
    return this;
}
 
Example 4
Source Project: code-builder   Source File: CodeBuilderInvoke.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 获取需要自动生成的表列表
 *
 * @param dataBase 数据库对象实例
 * @return
 */
private static List<String> getTables(DataBase dataBase, CodeBuilderProperties codeBuilderProperties) {
    List<String> tables = codeBuilderProperties.getTables();
    String generatorByPattern = codeBuilderProperties.getGeneratorByPattern();
    /*
     * 根据配置tables参数表名进行构建生成
     * 优先级高于generatorByPattern
     */
    if (null != tables && tables.size() > 0) {
        logger.info("Using table name to generate code automatically, please wait...");
        return tables;
    }
    /*
     * 如果配置generatorByPattern参数,优先级高于tables
     */
    else if (StringUtil.isNotEmpty(generatorByPattern)) {
        logger.info("Using expression method to generate code automatically, please wait...");
        return dataBase.getTableNames(generatorByPattern);
    }
    throw new CodeBuilderException(ErrorEnum.NO_BUILDER_TABLE);
}
 
Example 5
Source Project: beihu-boot   Source File: AbstractClassBuilder.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Name of field corresponding to formatted column
 *
 * @param columnName column name
 * @return
 */
protected String formatterJavaPropertyName(String columnName) {
    try {
        if (getWrapper().isIgnoreColumnPrefix()) {
            columnName = columnName.substring(columnName.indexOf(UNDERLINE) + 1);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        columnName = StringUtil.getCamelCaseString(columnName, false);
    }

    return columnName;
}
 
Example 6
Source Project: api-boot   Source File: AbstractClassBuilder.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Name of field corresponding to formatted column
 *
 * @param columnName column name
 * @return
 */
protected String formatterJavaPropertyName(String columnName) {
    try {
        if (getWrapper().isIgnoreColumnPrefix()) {
            columnName = columnName.substring(columnName.indexOf(UNDERLINE) + 1);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        columnName = StringUtil.getCamelCaseString(columnName, false);
    }

    return columnName;
}
 
Example 7
Source Project: code-builder   Source File: AbstractEngineTemplate.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获取模板创建文件后的包名
 *
 * @param templateConfiguration 模板配置对象
 * @return 包名
 */
protected String getTemplatePackageName(TemplateConfiguration templateConfiguration) {
    // 包名前缀
    StringBuffer packageName = new StringBuffer(codeBuilderProperties.getBuilder().getPackagePrefix());
    // 默认包名
    if (StringUtil.isNotEmpty(templateConfiguration.getPackageName())) {
        packageName.append(PACKAGE_SPLIT);
        // 转换为小写
        packageName.append(templateConfiguration.getPackageName().toLowerCase());
    }
    return packageName.toString();
}
 
Example 8
Source Project: code-builder   Source File: AbstractEngineTemplate.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获取模板生成文件后的类名
 *
 * @param templateConfiguration 默认配置信息
 * @param entityName            数据表对应的实体名称
 * @return Class 名称
 */
protected String getTemplateClassName(TemplateConfiguration templateConfiguration, String entityName) {
    StringBuffer className = new StringBuffer();
    // 追加文件前缀名
    if (StringUtil.isNotEmpty(templateConfiguration.getFilePrefix())) {
        className.append(StringUtil.getCamelCaseString(templateConfiguration.getFilePrefix(), true));
    }
    // 实体类名称
    className.append(entityName);
    // 追加文件后缀名
    if (StringUtil.isNotEmpty(templateConfiguration.getFileSuffix())) {
        className.append(StringUtil.getCamelCaseString(templateConfiguration.getFileSuffix(), true));
    }
    return className.toString();
}
 
Example 9
Source Project: code-builder   Source File: AbstractEngineTemplate.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 循环创建package
 * 根据builder.yml配置文件内的参数packagePrefix进行自动创建package包名的文件夹
 * 如:com.code.builder
 * 则自动创建com/code/builder文件夹
 *
 * @param templateConfiguration 模板配置
 */
protected void loopCreatePackage(TemplateConfiguration templateConfiguration) {
    // 目录地址
    StringBuffer basePackagePath = new StringBuffer();
    // 目标根地址
    basePackagePath.append(getBasePackageTargetDir());
    // 模板生成文件目标独有的package
    if (StringUtil.isNotEmpty(templateConfiguration.getPackageName())) {
        basePackagePath.append(templateConfiguration.getPackageName());
    }
    // 执行创建目录
    File basePackage = new File(basePackagePath.toString());
    basePackage.mkdirs();
}
 
Example 10
Source Project: code-builder   Source File: AbstractDataBase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获取数据库连接对象
 *
 * @return 数据库连接对象
 */
public Connection getConnection() {
    try {
        /*
         * 连接不存在 || 连接失效
         * 重新获取连接
         */
        if (connection == null || connection.isClosed()) {
            // spring-boot-starter方式获取数据库连接
            if (codeBuilderProperties.getDataSource() != null) {
                connection = codeBuilderProperties.getDataSource().getConnection();
            }
            // maven-plugin插件形式获取数据库连接
            else {
                // 默认使用数据库驱动类型内的限定类名
                String driverClassName = codeBuilderProperties.getDbType().getValue();
                // 存在自定义的驱动限定类名时使用自定义来实例化驱动对象
                if (StringUtil.isNotEmpty(codeBuilderProperties.getDbDriverClassName())) {
                    driverClassName = codeBuilderProperties.getDbDriverClassName();
                }
                //加载驱动程序
                Class.forName(driverClassName);
                // 获取数据库连接
                connection = DriverManager.getConnection(codeBuilderProperties.getDbUrl(), codeBuilderProperties.getDbUserName(), codeBuilderProperties.getDbPassword());
            }
        }
        return connection;
    } catch (Exception e) {
        e.printStackTrace();
    }
    throw new CodeBuilderException(ErrorEnum.NOT_GET_CONNECTION);
}
 
Example 11
Source Project: code-builder   Source File: AbstractDataBase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获取数据表基本信息
 *
 * @param tableName 表名
 * @return 数据表对象实例
 */
public Table getTable(String tableName) {
    // 构建数据表对象
    Table.TableBuilder tableBuilder = Table.builder();
    try {
        ResultSet resultSet = getConnection().getMetaData().getTables(null, null, tableName, new String[]{"TABLE"});
        if (resultSet.next()) {
            tableBuilder
                    // 表名
                    .tableName(tableName)
                    // 实体类名称
                    .entityName(StringUtil.getCamelCaseString(tableName, true))
                    // 表类别
                    .catalog(resultSet.getString(TableMetaDataEnum.TABLE_CAT.getValue()))
                    // 表模式
                    .schema(resultSet.getString(TableMetaDataEnum.TABLE_SCHEMA.getValue()))
                    // 表类型
                    .tableType(resultSet.getString(TableMetaDataEnum.TABLE_TYPE.getValue()))
                    // 获取备注信息
                    .remark(getTableComment(tableName))
                    // 所有列,排除主键
                    .columns(getColumns(tableName))
                    // 主键列表
                    .primaryKeys(getPrimaryKeys(tableName));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return tableBuilder.build().buildAfterSetting(codeBuilderProperties);
}
 
Example 12
Source Project: code-builder   Source File: BuilderConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获取不同系统的package转换目录分割后的路径
 *
 * @return 获取不同系统格式化后的包名路径
 */
public String getDiffSysPackagePrefix() {
    if (!StringUtil.isNotEmpty(packagePrefix)) {
        return packagePrefix;
    }
    return packagePrefix.replace(".", File.separator);
}
 
Example 13
Source Project: code-builder   Source File: AbstractDataBase.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 根据结果集获取列的详细信息
 * 根据不同的columnPattern获取到的列元数据
 *
 * @param resultSet       结果集
 * @param isPrimaryColumn true:主键列,false:普通列
 * @return 列基本信息对象
 */
private Column getColumn(ResultSet resultSet, boolean isPrimaryColumn) {
    try {
        // 数据库字段类型
        int jdbcType = resultSet.getInt(TableMetaDataEnum.DATA_TYPE.getValue());
        // 列名
        String columnName = resultSet.getString(TableMetaDataEnum.COLUMN_NAME.getValue());
        // 表名
        String tableName = resultSet.getString(TableMetaDataEnum.TABLE_NAME.getValue());

        return Column.builder()
                // 列名
                .columnName(columnName)
                // 列长度
                .size(resultSet.getInt(TableMetaDataEnum.COLUMN_SIZE.getValue()))
                // 是否为空
                .nullable(resultSet.getBoolean(TableMetaDataEnum.NULLABLE.getValue()))
                // 默认值
                .defaultValue(resultSet.getString(TableMetaDataEnum.COLUMN_DEF.getValue()))
                // 数据库列类型
                .jdbcType(jdbcType)
                // 是否自增
                .autoincrement(hasColumn(resultSet, TableMetaDataEnum.IS_AUTOINCREMENT.getValue()) ? resultSet.getBoolean(TableMetaDataEnum.IS_AUTOINCREMENT.getValue()) : false)
                // 列备注信息
                .remark(resultSet.getString(TableMetaDataEnum.REMARKS.getValue()))
                // 精度
                .decimalDigits(resultSet.getInt(TableMetaDataEnum.DECIMAL_DIGITS.getValue()))
                // jdbc类型名称
                .jdbcTypeName(JdbcTypeResolver.getJdbcTypeName(jdbcType))
                // 格式化后的java field
                .javaProperty(StringUtil.getCamelCaseString(columnName, false))
                // 对应的java数据类型
                .javaType(JavaTypeResolver.getJavaType(jdbcType, false))
                // java数据类型
                .fullJavaType(JavaTypeResolver.getJavaType(jdbcType, true))
                // 是否为主键列
                // 如果是主键获取列信息,直接返回true
                .primaryKey(!isPrimaryColumn ? isPrimaryKey(tableName, columnName) : isPrimaryColumn)
                // 是否为外键列
                // 如果是主键获取列信息,直接返回false
                .foreignKey(!isPrimaryColumn ? isForeignKey(tableName, columnName) : false)
                .build();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}