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;
import cn.datax.common.database.annotation.EnableDatabase;
import cn.datax.common.log.annotation.EnableDataLog;
import cn.datax.common.mybatis.annotation.EnableDataMybatis;
import cn.datax.common.redis.annotation.EnableDataRedis;
......@@ -13,6 +14,7 @@ import org.springframework.cloud.client.SpringCloudApplication;
@EnableDataRedis
@EnableDataLog
@EnableDataFeignClients
@EnableDatabase
@SpringCloudApplication
public class DataFactoryApplication {
......
......@@ -2,6 +2,7 @@ package cn.datax.service.data.factory.controller;
import cn.datax.common.core.JsonPage;
import cn.datax.common.core.R;
import cn.datax.common.database.DbQuery;
import cn.datax.common.validate.ValidationGroups;
import cn.datax.service.data.factory.api.dto.DataSourceDto;
import cn.datax.service.data.factory.api.entity.DataSourceEntity;
......@@ -22,6 +23,7 @@ import org.springframework.web.bind.annotation.*;
import cn.datax.common.base.BaseController;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
......@@ -140,7 +142,13 @@ public class DataSourceController extends BaseController {
@ApiImplicitParam(name = "dataSource", value = "详细实体dataSource", required = true, dataType = "DataSourceDto")
@PostMapping("/checkConnection")
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("数据库连接有误,请检查数据库配置是否正确");
}
}
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.entity.DataSourceEntity;
import cn.datax.common.base.BaseService;
......@@ -23,7 +24,7 @@ public interface DataSourceService extends BaseService<DataSourceEntity> {
void deleteDataSourceById(String id);
Boolean checkConnection(DataSourceDto dataSource);
DbQuery checkConnection(DataSourceDto dataSource);
List<Map<String, Object>> getDbTypes();
}
......@@ -4,7 +4,6 @@ import cn.datax.common.database.DataSourceFactory;
import cn.datax.common.database.DbQuery;
import cn.datax.common.database.constants.DbQueryProperty;
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.entity.DbSchema;
import cn.datax.service.data.factory.api.entity.DataSourceEntity;
......@@ -17,7 +16,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
......@@ -42,6 +40,9 @@ public class DataSourceServiceImpl extends BaseServiceImpl<DataSourceDao, DataSo
@Autowired
private DataSourceMapper dataSourceMapper;
@Autowired
private DataSourceFactory dataSourceFactory;
@Override
@Transactional(rollbackFor = Exception.class)
public void saveDataSource(DataSourceDto dataSourceDto) {
......@@ -63,19 +64,12 @@ public class DataSourceServiceImpl extends BaseServiceImpl<DataSourceDao, DataSo
}
@Override
public Boolean checkConnection(DataSourceDto dataSourceDto) {
public DbQuery checkConnection(DataSourceDto dataSourceDto) {
DataSourceEntity dataSource = dataSourceMapper.toEntity(dataSourceDto);
DbSchema dbSchema = dataSource.getDbSchema();
DataSourceFactory dataSourceFactory = new CacheDataSourceFactoryBean();
DbQueryProperty dbQueryProperty = new DbQueryProperty(dataSource.getDbType(), dbSchema.getJdbcUrl(), dbSchema.getUsername(), dbSchema.getPassword());
DbQuery dbQuery = dataSourceFactory.createDbQuery(dbQueryProperty);
boolean valid = false;
try {
valid = dbQuery.valid();
} catch (SQLException e) {
e.printStackTrace();
}
return valid;
return dbQuery;
}
@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