Search in sources :

Example 1 with DbInfo

use of com.github.mengweijin.generator.entity.DbInfo in project code-generator-maven-plugin by mengweijin.

the class DefaultAutoGenerator method dataSourceConfigBuilder.

private DataSourceConfig.Builder dataSourceConfigBuilder() {
    DbInfo dbInfo = DbInfoUtils.getDbInfo(projectInfo);
    // 自定义 CustomerDataSource, 使用自定义的 ClassLoader 加载类获取连接。
    CustomerDataSource dataSource = new CustomerDataSource(dbInfo.getUrl(), dbInfo.getUsername(), dbInfo.getPassword());
    return new DataSourceConfig.Builder(dataSource);
}
Also used : CustomerDataSource(com.github.mengweijin.generator.config.CustomerDataSource) DbInfo(com.github.mengweijin.generator.entity.DbInfo)

Example 2 with DbInfo

use of com.github.mengweijin.generator.entity.DbInfo in project code-generator-maven-plugin by mengweijin.

the class PropertiesBootFileReader method getDbInfo.

@Override
public DbInfo getDbInfo(File file) {
    try {
        Props props = new Props(file.toURI().toURL(), StandardCharsets.UTF_8);
        String url = props.getStr(SPRING_DATASOURCE_URL);
        if (StrUtil.isBlank(url)) {
            return null;
        }
        DbInfo dbInfo = new DbInfo();
        dbInfo.setUrl(url);
        dbInfo.setUsername(props.getStr(SPRING_DATASOURCE_USERNAME));
        dbInfo.setPassword(props.getStr(SPRING_DATASOURCE_PASSWORD));
        return dbInfo;
    } catch (MalformedURLException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Props(cn.hutool.setting.dialect.Props) DbInfo(com.github.mengweijin.generator.entity.DbInfo)

Example 3 with DbInfo

use of com.github.mengweijin.generator.entity.DbInfo in project code-generator-maven-plugin by mengweijin.

the class DbInfoUtils method generateDefaultDbInfo.

private static DbInfo generateDefaultDbInfo(ProjectInfo projectInfo) {
    List<Resource> resourceList = projectInfo.getResourceList();
    Resource resource = resourceList.stream().filter(res -> res.getDirectory().endsWith("\\resources")).findFirst().get();
    File applicationFile = getBootFile(resource, APPLICATION_FILE);
    if (applicationFile == null) {
        throw new RuntimeException("Can't find any file " + JSON.toJSONString(APPLICATION_FILE));
    }
    String activeProfilesEnv = BootFileReaderFactory.getActiveProfilesEnv(applicationFile);
    DbInfo dbInfo = null;
    if (StrUtil.isNotBlank(activeProfilesEnv)) {
        String activeBootFilePath = resource.getDirectory() + File.separator + "application-" + activeProfilesEnv + StrUtil.DOT + FileNameUtil.getSuffix(applicationFile);
        File activeBootFile = FileUtil.file(activeBootFilePath);
        dbInfo = BootFileReaderFactory.getDbInfo(activeBootFile);
    }
    if (dbInfo == null) {
        dbInfo = BootFileReaderFactory.getDbInfo(applicationFile);
    }
    return dbInfo;
}
Also used : Resource(org.apache.maven.model.Resource) File(java.io.File) DbInfo(com.github.mengweijin.generator.entity.DbInfo)

Example 4 with DbInfo

use of com.github.mengweijin.generator.entity.DbInfo in project code-generator-maven-plugin by mengweijin.

the class YamlBootFileReader method getDbInfo.

@Override
public DbInfo getDbInfo(File file) {
    LinkedHashMap map = YamlUtils.loadAs(file, LinkedHashMap.class);
    Object datasource = JSONPath.eval(map, "$.spring.datasource");
    if (datasource == null) {
        return null;
    }
    DbInfo dbInfo = new DbInfo();
    Object url = JSONPath.eval(map, "$." + SPRING_DATASOURCE_URL);
    Object driverName = JSONPath.eval(map, "$." + SPRING_DATASOURCE_DRIVERCLASSNAME);
    if (driverName == null) {
        // JSONPath中,中划线是特殊字符
        driverName = JSONPath.eval(map, "$.spring.datasource['driver-class-name']");
    }
    Object username = JSONPath.eval(map, "$." + SPRING_DATASOURCE_USERNAME);
    Object password = JSONPath.eval(map, "$." + SPRING_DATASOURCE_PASSWORD);
    dbInfo.setUrl(url == null ? null : String.valueOf(url));
    dbInfo.setUsername(username == null ? null : String.valueOf(username));
    dbInfo.setPassword(password == null ? null : String.valueOf(password));
    return dbInfo;
}
Also used : DbInfo(com.github.mengweijin.generator.entity.DbInfo) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

DbInfo (com.github.mengweijin.generator.entity.DbInfo)4 Props (cn.hutool.setting.dialect.Props)1 CustomerDataSource (com.github.mengweijin.generator.config.CustomerDataSource)1 File (java.io.File)1 MalformedURLException (java.net.MalformedURLException)1 LinkedHashMap (java.util.LinkedHashMap)1 Resource (org.apache.maven.model.Resource)1