Commit 7d6f4e3c by yuwei

项目初始化

parent cc3a7120
package cn.datax.common.database.annotation;
import cn.datax.common.database.config.AutoConfiguration;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({AutoConfiguration.class})
public @interface EnableDatabase {
}
package cn.datax.common.database.config;
import cn.datax.common.database.DataSourceFactory;
import cn.datax.common.database.datasource.CacheDataSourceFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
/**
* 扫描注入bean
*
* @author yuwei
* @since 2019/10/30
*/
@ComponentScan({"cn.datax.common.database"})
public class AutoConfiguration {
@Bean
public DataSourceFactory dataSourceFactory(){
return new CacheDataSourceFactoryBean();
}
}
package cn.datax.service.data.factory; package cn.datax.service.data.factory;
import cn.datax.common.database.annotation.EnableDatabase;
import cn.datax.common.log.annotation.EnableDataLog; import cn.datax.common.log.annotation.EnableDataLog;
import cn.datax.common.mybatis.annotation.EnableDataMybatis; import cn.datax.common.mybatis.annotation.EnableDataMybatis;
import cn.datax.common.redis.annotation.EnableDataRedis; import cn.datax.common.redis.annotation.EnableDataRedis;
...@@ -13,6 +14,7 @@ import org.springframework.cloud.client.SpringCloudApplication; ...@@ -13,6 +14,7 @@ import org.springframework.cloud.client.SpringCloudApplication;
@EnableDataRedis @EnableDataRedis
@EnableDataLog @EnableDataLog
@EnableDataFeignClients @EnableDataFeignClients
@EnableDatabase
@SpringCloudApplication @SpringCloudApplication
public class DataFactoryApplication { public class DataFactoryApplication {
......
...@@ -2,6 +2,7 @@ package cn.datax.service.data.factory.controller; ...@@ -2,6 +2,7 @@ package cn.datax.service.data.factory.controller;
import cn.datax.common.core.JsonPage; import cn.datax.common.core.JsonPage;
import cn.datax.common.core.R; import cn.datax.common.core.R;
import cn.datax.common.database.DbQuery;
import cn.datax.common.validate.ValidationGroups; import cn.datax.common.validate.ValidationGroups;
import cn.datax.service.data.factory.api.dto.DataSourceDto; import cn.datax.service.data.factory.api.dto.DataSourceDto;
import cn.datax.service.data.factory.api.entity.DataSourceEntity; import cn.datax.service.data.factory.api.entity.DataSourceEntity;
...@@ -22,6 +23,7 @@ import org.springframework.web.bind.annotation.*; ...@@ -22,6 +23,7 @@ import org.springframework.web.bind.annotation.*;
import cn.datax.common.base.BaseController; import cn.datax.common.base.BaseController;
import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -140,7 +142,13 @@ public class DataSourceController extends BaseController { ...@@ -140,7 +142,13 @@ public class DataSourceController extends BaseController {
@ApiImplicitParam(name = "dataSource", value = "详细实体dataSource", required = true, dataType = "DataSourceDto") @ApiImplicitParam(name = "dataSource", value = "详细实体dataSource", required = true, dataType = "DataSourceDto")
@PostMapping("/checkConnection") @PostMapping("/checkConnection")
public R checkConnection(@RequestBody @Validated({ValidationGroups.Insert.class}) DataSourceDto dataSource) { public R checkConnection(@RequestBody @Validated({ValidationGroups.Insert.class}) DataSourceDto dataSource) {
Boolean valid = dataSourceService.checkConnection(dataSource); DbQuery dbQuery = dataSourceService.checkConnection(dataSource);
boolean valid = false;
try {
valid = dbQuery.valid();
} catch (SQLException e) {
e.printStackTrace();
}
return valid ? R.ok() : R.error("数据库连接有误,请检查数据库配置是否正确"); return valid ? R.ok() : R.error("数据库连接有误,请检查数据库配置是否正确");
} }
} }
package cn.datax.service.data.factory.service; package cn.datax.service.data.factory.service;
import cn.datax.common.database.DbQuery;
import cn.datax.service.data.factory.api.dto.DataSourceDto; import cn.datax.service.data.factory.api.dto.DataSourceDto;
import cn.datax.service.data.factory.api.entity.DataSourceEntity; import cn.datax.service.data.factory.api.entity.DataSourceEntity;
import cn.datax.common.base.BaseService; import cn.datax.common.base.BaseService;
...@@ -23,7 +24,7 @@ public interface DataSourceService extends BaseService<DataSourceEntity> { ...@@ -23,7 +24,7 @@ public interface DataSourceService extends BaseService<DataSourceEntity> {
void deleteDataSourceById(String id); void deleteDataSourceById(String id);
Boolean checkConnection(DataSourceDto dataSource); DbQuery checkConnection(DataSourceDto dataSource);
List<Map<String, Object>> getDbTypes(); List<Map<String, Object>> getDbTypes();
} }
...@@ -4,7 +4,6 @@ import cn.datax.common.database.DataSourceFactory; ...@@ -4,7 +4,6 @@ import cn.datax.common.database.DataSourceFactory;
import cn.datax.common.database.DbQuery; import cn.datax.common.database.DbQuery;
import cn.datax.common.database.constants.DbQueryProperty; import cn.datax.common.database.constants.DbQueryProperty;
import cn.datax.common.database.constants.DbType; import cn.datax.common.database.constants.DbType;
import cn.datax.common.database.datasource.CacheDataSourceFactoryBean;
import cn.datax.service.data.factory.api.dto.DataSourceDto; import cn.datax.service.data.factory.api.dto.DataSourceDto;
import cn.datax.service.data.factory.api.entity.DbSchema; import cn.datax.service.data.factory.api.entity.DbSchema;
import cn.datax.service.data.factory.api.entity.DataSourceEntity; import cn.datax.service.data.factory.api.entity.DataSourceEntity;
...@@ -17,7 +16,6 @@ import org.springframework.stereotype.Service; ...@@ -17,7 +16,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.sql.SQLException;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
...@@ -42,6 +40,9 @@ public class DataSourceServiceImpl extends BaseServiceImpl<DataSourceDao, DataSo ...@@ -42,6 +40,9 @@ public class DataSourceServiceImpl extends BaseServiceImpl<DataSourceDao, DataSo
@Autowired @Autowired
private DataSourceMapper dataSourceMapper; private DataSourceMapper dataSourceMapper;
@Autowired
private DataSourceFactory dataSourceFactory;
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void saveDataSource(DataSourceDto dataSourceDto) { public void saveDataSource(DataSourceDto dataSourceDto) {
...@@ -63,19 +64,12 @@ public class DataSourceServiceImpl extends BaseServiceImpl<DataSourceDao, DataSo ...@@ -63,19 +64,12 @@ public class DataSourceServiceImpl extends BaseServiceImpl<DataSourceDao, DataSo
} }
@Override @Override
public Boolean checkConnection(DataSourceDto dataSourceDto) { public DbQuery checkConnection(DataSourceDto dataSourceDto) {
DataSourceEntity dataSource = dataSourceMapper.toEntity(dataSourceDto); DataSourceEntity dataSource = dataSourceMapper.toEntity(dataSourceDto);
DbSchema dbSchema = dataSource.getDbSchema(); DbSchema dbSchema = dataSource.getDbSchema();
DataSourceFactory dataSourceFactory = new CacheDataSourceFactoryBean();
DbQueryProperty dbQueryProperty = new DbQueryProperty(dataSource.getDbType(), dbSchema.getJdbcUrl(), dbSchema.getUsername(), dbSchema.getPassword()); DbQueryProperty dbQueryProperty = new DbQueryProperty(dataSource.getDbType(), dbSchema.getJdbcUrl(), dbSchema.getUsername(), dbSchema.getPassword());
DbQuery dbQuery = dataSourceFactory.createDbQuery(dbQueryProperty); DbQuery dbQuery = dataSourceFactory.createDbQuery(dbQueryProperty);
boolean valid = false; return dbQuery;
try {
valid = dbQuery.valid();
} catch (SQLException e) {
e.printStackTrace();
}
return valid;
} }
@Override @Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment