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);
}
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);
}
}
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;
}
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;
}
Aggregations