增加数据源测试

This commit is contained in:
暮光:城中城
2020-05-17 21:39:59 +08:00
parent 33229dd85a
commit adadffa0cd
13 changed files with 171 additions and 129 deletions

View File

@@ -12,6 +12,7 @@ import com.zyplayer.doc.db.framework.db.bean.DatabaseFactoryBean;
import com.zyplayer.doc.db.framework.db.bean.DatabaseRegistrationBean;
import com.zyplayer.doc.db.framework.json.DocDbResponseJson;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -46,6 +47,29 @@ public class DbDatasourceController {
return DocDbResponseJson.ok(datasourceList);
}
@PostMapping(value = "/test")
public ResponseJson test(DbDatasource dbDatasource) {
// 验证新的数据源
try {
// 获取原始密码
if (Objects.equals("***", dbDatasource.getSourcePassword()) && dbDatasource.getId() != null) {
DbDatasource dbDatasourceSel = dbDatasourceService.getById(dbDatasource.getId());
if (dbDatasourceSel != null) {
dbDatasource.setSourcePassword(dbDatasourceSel.getSourcePassword());
}
}
DatabaseFactoryBean databaseFactoryBean = DatasourceUtil.createDatabaseFactoryBean(dbDatasource);
if (databaseFactoryBean == null) {
return DocDbResponseJson.warn("获取数据源失败,请检查配置是否正确");
}
databaseFactoryBean.getDataSource().close();
} catch (Exception e) {
e.printStackTrace();
return DocDbResponseJson.warn(ExceptionUtils.getFullStackTrace(e));
}
return DocDbResponseJson.ok();
}
@PostMapping(value = "/update")
public ResponseJson update(DbDatasource dbDatasource) {
if (StringUtils.isBlank(dbDatasource.getName())) {
@@ -69,6 +93,8 @@ public class DbDatasourceController {
Long sourceId = Optional.ofNullable(dbDatasource.getId()).orElse(0L);
if (sourceId > 0) {
dbDatasourceService.updateById(dbDatasource);
// 关闭旧数据源
databaseRegistrationBean.closeDatasource(dbDatasource.getId());
} else {
DocUserDetails currentUser = DocUserUtil.getCurrentUser();
dbDatasource.setCreateTime(new Date());
@@ -77,18 +103,6 @@ public class DbDatasourceController {
dbDatasource.setYn(1);
dbDatasourceService.save(dbDatasource);
}
// 关闭数据源
databaseRegistrationBean.closeDatasource(dbDatasource.getId());
// 验证新的数据源
DbDatasource dbDatasourceSel = dbDatasourceService.getById(dbDatasource.getId());
if (Objects.equals(dbDatasourceSel.getYn(), 1)) {
// 创建新的数据源
DatabaseFactoryBean databaseFactoryBean = DatasourceUtil.createDatabaseFactoryBean(dbDatasourceSel);
if (databaseFactoryBean == null) {
return DocDbResponseJson.warn("创建数据源失败,请检查配置是否正确");
}
databaseFactoryBean.getDataSource().close();
}
return DocDbResponseJson.ok();
}
}

View File

@@ -11,73 +11,70 @@ import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import java.io.IOException;
public class DatasourceUtil {
private static SqlLogInterceptor sqlLogInterceptor = new SqlLogInterceptor();
public static DatabaseFactoryBean createDatabaseFactoryBean(DbDatasource dbDatasource){
try {
Resource[] resources = null;
// 描述连接信息的对象
DatabaseFactoryBean databaseFactoryBean = new DatabaseFactoryBean();
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
String dbUrl = dbDatasource.getSourceUrl();
if (dbUrl.contains("mysql")) {
// jdbc:mysql://192.168.0.1:3306/user_info?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&autoReconnect=true
String[] urlParamArr = dbUrl.split("\\?");
String[] urlDbNameArr = urlParamArr[0].split("/");
if (urlDbNameArr.length >= 2) {
databaseFactoryBean.setDbName(urlDbNameArr[urlDbNameArr.length - 1]);
//databaseFactoryBean.setHost(urlDbNameArr[urlDbNameArr.length - 2]);
}
databaseFactoryBean.setDatabaseProduct(DatabaseFactoryBean.DatabaseProduct.MYSQL);
resources = resolver.getResources("classpath:com/zyplayer/doc/db/framework/db/mapper/mysql/*.xml");
} else if (dbUrl.contains("sqlserver")) {
// jdbc:jtds:sqlserver://192.168.0.1:33434;socketTimeout=60;DatabaseName=user_info;
String[] urlParamArr = dbUrl.split(";");
//String[] urlDbNameArr = urlParamArr[0].split("/");
//databaseFactoryBean.setHost(urlDbNameArr[urlDbNameArr.length - 1]);
for (String urlParam : urlParamArr) {
String[] keyValArr = urlParam.split("=");
if (keyValArr.length >= 2 && keyValArr[0].equalsIgnoreCase("DatabaseName")) {
databaseFactoryBean.setDbName(keyValArr[1]);
}
}
databaseFactoryBean.setDatabaseProduct(DatabaseFactoryBean.DatabaseProduct.SQLSERVER);
resources = resolver.getResources("classpath:com/zyplayer/doc/db/framework/db/mapper/sqlserver/*.xml");
} else if (dbUrl.contains("oracle")) {
// jdbc:oracle:thin:@127.0.0.1:1521:user_info
// jdbc:oracle:thin:@127.0.0.1:1521/user_info
// 代码是写好的但还没有oracle的库让我测试过~
String[] urlParamArr = dbUrl.split("\\?")[0].split("@");
String[] urlDbNameArr = urlParamArr[0].split("/");
if (urlDbNameArr.length <= 1) {
urlDbNameArr = urlParamArr[0].split(":");
}
public static DatabaseFactoryBean createDatabaseFactoryBean(DbDatasource dbDatasource) throws Exception {
Resource[] resources = null;
// 描述连接信息的对象
DatabaseFactoryBean databaseFactoryBean = new DatabaseFactoryBean();
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
String dbUrl = dbDatasource.getSourceUrl();
if (dbUrl.contains("mysql")) {
// jdbc:mysql://192.168.0.1:3306/user_info?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&autoReconnect=true
String[] urlParamArr = dbUrl.split("\\?");
String[] urlDbNameArr = urlParamArr[0].split("/");
if (urlDbNameArr.length >= 2) {
databaseFactoryBean.setDbName(urlDbNameArr[urlDbNameArr.length - 1]);
databaseFactoryBean.setDatabaseProduct(DatabaseFactoryBean.DatabaseProduct.ORACLE);
resources = resolver.getResources("classpath:com/zyplayer/doc/db/framework/db/mapper/oracle/*.xml");
//databaseFactoryBean.setHost(urlDbNameArr[urlDbNameArr.length - 2]);
}
if (resources == null) {
return null;
databaseFactoryBean.setDatabaseProduct(DatabaseFactoryBean.DatabaseProduct.MYSQL);
resources = resolver.getResources("classpath:com/zyplayer/doc/db/framework/db/mapper/mysql/*.xml");
} else if (dbUrl.contains("sqlserver")) {
// jdbc:jtds:sqlserver://192.168.0.1:33434;socketTimeout=60;DatabaseName=user_info;
String[] urlParamArr = dbUrl.split(";");
//String[] urlDbNameArr = urlParamArr[0].split("/");
//databaseFactoryBean.setHost(urlDbNameArr[urlDbNameArr.length - 1]);
for (String urlParam : urlParamArr) {
String[] keyValArr = urlParam.split("=");
if (keyValArr.length >= 2 && keyValArr[0].equalsIgnoreCase("DatabaseName")) {
databaseFactoryBean.setDbName(keyValArr[1]);
}
}
// 数据源配置
DruidDataSource dataSource = DruidDataSourceUtil.createDataSource(dbDatasource.getDriverClassName(), dbDatasource.getSourceUrl(), dbDatasource.getSourceName(), dbDatasource.getSourcePassword());
// 创建sqlSessionTemplate
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setMapperLocations(resources);
sqlSessionFactoryBean.setPlugins(new Interceptor[]{sqlLogInterceptor});
SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactoryBean.getObject());
// 组装自定义的bean
databaseFactoryBean.setId(dbDatasource.getId());
databaseFactoryBean.setCnName(dbDatasource.getName());
databaseFactoryBean.setDataSource(dataSource);
databaseFactoryBean.setSqlSessionTemplate(sqlSessionTemplate);
databaseFactoryBean.setUrl(dbUrl);
return databaseFactoryBean;
} catch (Throwable e) {
e.printStackTrace();
databaseFactoryBean.setDatabaseProduct(DatabaseFactoryBean.DatabaseProduct.SQLSERVER);
resources = resolver.getResources("classpath:com/zyplayer/doc/db/framework/db/mapper/sqlserver/*.xml");
} else if (dbUrl.contains("oracle")) {
// jdbc:oracle:thin:@127.0.0.1:1521:user_info
// jdbc:oracle:thin:@127.0.0.1:1521/user_info
// 代码是写好的但还没有oracle的库让我测试过~
String[] urlParamArr = dbUrl.split("\\?")[0].split("@");
String[] urlDbNameArr = urlParamArr[0].split("/");
if (urlDbNameArr.length <= 1) {
urlDbNameArr = urlParamArr[0].split(":");
}
databaseFactoryBean.setDbName(urlDbNameArr[urlDbNameArr.length - 1]);
databaseFactoryBean.setDatabaseProduct(DatabaseFactoryBean.DatabaseProduct.ORACLE);
resources = resolver.getResources("classpath:com/zyplayer/doc/db/framework/db/mapper/oracle/*.xml");
}
return null;
if (resources == null) {
return null;
}
// 数据源配置
DruidDataSource dataSource = DruidDataSourceUtil.createDataSource(dbDatasource.getDriverClassName(), dbDatasource.getSourceUrl(), dbDatasource.getSourceName(), dbDatasource.getSourcePassword());
// 创建sqlSessionTemplate
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setMapperLocations(resources);
sqlSessionFactoryBean.setPlugins(new Interceptor[]{sqlLogInterceptor});
SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactoryBean.getObject());
// 组装自定义的bean
databaseFactoryBean.setId(dbDatasource.getId());
databaseFactoryBean.setCnName(dbDatasource.getName());
databaseFactoryBean.setDataSource(dataSource);
databaseFactoryBean.setSqlSessionTemplate(sqlSessionTemplate);
databaseFactoryBean.setUrl(dbUrl);
return databaseFactoryBean;
}
}

View File

@@ -107,13 +107,17 @@ public class DatabaseRegistrationBean {
if (factoryBean != null) return factoryBean;
DbDatasource dbDatasource = dbDatasourceService.getById(sourceId);
if (dbDatasource == null) {
throw new ConfirmException("未找到指定数据源配置信息" + sourceId);
throw new ConfirmException("未找到指定数据源配置信息");
}
DatabaseFactoryBean databaseFactoryBean = DatasourceUtil.createDatabaseFactoryBean(dbDatasource);
if (databaseFactoryBean == null) {
throw new ConfirmException("创建数据源失败:" + sourceId);
try {
DatabaseFactoryBean databaseFactoryBean = DatasourceUtil.createDatabaseFactoryBean(dbDatasource);
if (databaseFactoryBean == null) {
throw new ConfirmException("获取数据源失败");
}
databaseFactoryBeanMap.put(sourceId, databaseFactoryBean);
return databaseFactoryBean;
} catch (Exception e) {
throw new ConfirmException("创建数据源失败", e);
}
databaseFactoryBeanMap.put(sourceId, databaseFactoryBean);
return databaseFactoryBean;
}
}

File diff suppressed because one or more lines are too long

View File

@@ -1,2 +1,2 @@
!function(e){function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}var r=window.webpackJsonp;window.webpackJsonp=function(t,c,u){for(var a,i,f,l=0,s=[];l<t.length;l++)i=t[l],o[i]&&s.push(o[i][0]),o[i]=0;for(a in c)Object.prototype.hasOwnProperty.call(c,a)&&(e[a]=c[a]);for(r&&r(t,c,u);s.length;)s.shift()();if(u)for(l=0;l<u.length;l++)f=n(n.s=u[l]);return f};var t={},o={2:0};n.e=function(e){function r(){a.onerror=a.onload=null,clearTimeout(i);var n=o[e];0!==n&&(n&&n[1](new Error("Loading chunk "+e+" failed.")),o[e]=void 0)}var t=o[e];if(0===t)return new Promise(function(e){e()});if(t)return t[2];var c=new Promise(function(n,r){t=o[e]=[n,r]});t[2]=c;var u=document.getElementsByTagName("head")[0],a=document.createElement("script");a.type="text/javascript",a.charset="utf-8",a.async=!0,a.timeout=12e4,n.nc&&a.setAttribute("nonce",n.nc),a.src=n.p+""+e+".js?"+{0:"3138591b4af9fb40e531",1:"1ecabc6d379552a7c3f7"}[e];var i=setTimeout(r,12e4);return a.onerror=a.onload=r,u.appendChild(a),c},n.m=e,n.c=t,n.i=function(e){return e},n.d=function(e,r,t){n.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get:t})},n.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(r,"a",r),r},n.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},n.p="",n.oe=function(e){throw console.error(e),e}}([]);
//# sourceMappingURL=doc-db-manifest.js.map?075412feecc8b570efae
!function(e){function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}var r=window.webpackJsonp;window.webpackJsonp=function(t,c,u){for(var i,a,f,l=0,s=[];l<t.length;l++)a=t[l],o[a]&&s.push(o[a][0]),o[a]=0;for(i in c)Object.prototype.hasOwnProperty.call(c,i)&&(e[i]=c[i]);for(r&&r(t,c,u);s.length;)s.shift()();if(u)for(l=0;l<u.length;l++)f=n(n.s=u[l]);return f};var t={},o={2:0};n.e=function(e){function r(){i.onerror=i.onload=null,clearTimeout(a);var n=o[e];0!==n&&(n&&n[1](new Error("Loading chunk "+e+" failed.")),o[e]=void 0)}var t=o[e];if(0===t)return new Promise(function(e){e()});if(t)return t[2];var c=new Promise(function(n,r){t=o[e]=[n,r]});t[2]=c;var u=document.getElementsByTagName("head")[0],i=document.createElement("script");i.type="text/javascript",i.charset="utf-8",i.async=!0,i.timeout=12e4,n.nc&&i.setAttribute("nonce",n.nc),i.src=n.p+""+e+".js?"+{0:"e961123b6de8434b2cc2",1:"93ce942e3bdbc7dd3435"}[e];var a=setTimeout(r,12e4);return i.onerror=i.onload=r,u.appendChild(i),c},n.m=e,n.c=t,n.i=function(e){return e},n.d=function(e,r,t){n.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get:t})},n.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(r,"a",r),r},n.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},n.p="",n.oe=function(e){throw console.error(e),e}}([]);
//# sourceMappingURL=doc-db-manifest.js.map?df9e1a3cc72f1a225176

File diff suppressed because one or more lines are too long

View File

@@ -8,7 +8,7 @@
<body>
<div id="app"></div>
<script type="text/javascript" src="doc-db-manifest.js?075412feecc8b570efae"></script><script type="text/javascript" src="doc-db-vendor.js?1ecabc6d379552a7c3f7"></script><script type="text/javascript" src="doc-db-index.js?3138591b4af9fb40e531"></script></body>
<script type="text/javascript" src="doc-db-manifest.js?df9e1a3cc72f1a225176"></script><script type="text/javascript" src="doc-db-vendor.js?93ce942e3bdbc7dd3435"></script><script type="text/javascript" src="doc-db-index.js?e961123b6de8434b2cc2"></script></body>
</html>