测试连接成功
parent
df0c5f403f
commit
17f1ec6357
File diff suppressed because one or more lines are too long
|
@ -1,24 +1,18 @@
|
|||
package com.etl.cleaning.config;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 连接数据源所需参数
|
||||
* mysql链接数据源测试
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
public class DatabaseConfig {
|
||||
/**
|
||||
* 数据源描述
|
||||
*/
|
||||
private String driverClassName;
|
||||
/**
|
||||
* 连接地址
|
||||
* jdbc路径
|
||||
*/
|
||||
private String url;
|
||||
/**
|
||||
* 数据库名称
|
||||
*/
|
||||
private String databaseName;
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
|
@ -31,16 +25,4 @@ public class DatabaseConfig {
|
|||
* 端口
|
||||
*/
|
||||
private String port;
|
||||
/**
|
||||
* 额外配置
|
||||
*/
|
||||
private String extraConfig;
|
||||
/**
|
||||
* 状态
|
||||
*/
|
||||
private Boolean status;
|
||||
/**
|
||||
* 备注
|
||||
*/
|
||||
private String remark;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
package com.etl.cleaning.controller;
|
||||
|
||||
import com.etl.cleaning.domian.request.InsertItemRequest;
|
||||
import com.etl.cleaning.service.ItemService;
|
||||
import com.etl.common.result.Result;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
|
@ -9,5 +14,20 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
@RestController
|
||||
@RequestMapping("/item")
|
||||
public class ItemController {
|
||||
private final ItemService itemService;
|
||||
|
||||
public ItemController(ItemService itemService) {
|
||||
this.itemService = itemService;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加数据源详情
|
||||
* @param insertItemRequest
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/insertItem")
|
||||
public Result insertItem(@RequestBody InsertItemRequest insertItemRequest) {
|
||||
itemService.insertItem(insertItemRequest);
|
||||
return Result.success();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -57,5 +57,5 @@ public class DataItem {
|
|||
/**
|
||||
* 连接地址
|
||||
*/
|
||||
private String place;
|
||||
private String itemPlace;
|
||||
}
|
||||
|
|
|
@ -10,14 +10,14 @@ import lombok.Getter;
|
|||
public enum EnumMsg {
|
||||
// 定义一个带有错误码和描述性字符串的枚举常量
|
||||
NO_TYPE(401, "暂无类型"),
|
||||
NO_TY_NAME(401, "暂无类型");
|
||||
DRIVER_CLASS_NAME(402,"com.mysql.cj.jdbc.Driver");
|
||||
|
||||
private final int code;
|
||||
private final String message;
|
||||
|
||||
|
||||
private final int errorCode;
|
||||
private final String errorMessage;
|
||||
|
||||
private EnumMsg(int errorCode, String errorMessage) {
|
||||
this.errorCode = errorCode;
|
||||
this.errorMessage = errorMessage;
|
||||
EnumMsg(int code, String message) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
package com.etl.cleaning.server;
|
||||
|
||||
import com.etl.cleaning.config.DatabaseConfig;
|
||||
import com.etl.cleaning.enums.EnumMsg;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
/**
|
||||
* 测试连接
|
||||
* mysql测试连接
|
||||
*/
|
||||
public class ServerTest {
|
||||
public static String testDatabaseConnection(@RequestBody DatabaseConfig databaseConfig) {
|
||||
public static String testDatabaseConnection(DatabaseConfig databaseConfig) {
|
||||
try {
|
||||
//创建数据源
|
||||
DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||
//设置数据源的驱动类名,URL,用户名,密码
|
||||
dataSource.setDriverClassName(databaseConfig.getDriverClassName());
|
||||
dataSource.setDriverClassName(EnumMsg.DRIVER_CLASS_NAME.getMessage());
|
||||
dataSource.setUrl(databaseConfig.getUrl());
|
||||
dataSource.setUsername(databaseConfig.getUsername());
|
||||
dataSource.setPassword(databaseConfig.getPassword());
|
||||
|
|
|
@ -2,10 +2,11 @@ package com.etl.cleaning.service;
|
|||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.etl.cleaning.domian.pojo.DataItem;
|
||||
import com.etl.cleaning.domian.request.InsertItemRequest;
|
||||
|
||||
/**
|
||||
* 数据源详情
|
||||
*/
|
||||
public interface ItemService extends IService<DataItem> {
|
||||
|
||||
void insertItem(InsertItemRequest insertItemRequest);
|
||||
}
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
package com.etl.cleaning.serviceimpl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.etl.cleaning.config.DatabaseConfig;
|
||||
import com.etl.cleaning.domian.pojo.DataItem;
|
||||
import com.etl.cleaning.domian.request.InsertItemRequest;
|
||||
import com.etl.cleaning.mapper.ItemMapper;
|
||||
import com.etl.cleaning.server.ServerTest;
|
||||
import com.etl.cleaning.service.ItemService;
|
||||
import org.springframework.stereotype.Service;
|
||||
/**
|
||||
|
@ -10,4 +13,17 @@ import org.springframework.stereotype.Service;
|
|||
*/
|
||||
@Service
|
||||
public class ItemServiceImpl extends ServiceImpl<ItemMapper, DataItem> implements ItemService {
|
||||
@Override
|
||||
public void insertItem(InsertItemRequest insertItemRequest) {
|
||||
//拼接路径
|
||||
String url = insertItemRequest.getPlace() + "/" +insertItemRequest.getItemName();
|
||||
DatabaseConfig databaseConfig = DatabaseConfig.builder()
|
||||
.url(url)
|
||||
.port(insertItemRequest.getItemPort())
|
||||
.username(insertItemRequest.getItemUsername())
|
||||
.password(insertItemRequest.getItemPass())
|
||||
.build();
|
||||
String result = ServerTest.testDatabaseConnection(databaseConfig);
|
||||
System.out.println("测试结果"+result);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -53,7 +53,7 @@ public class PlaceServiceImpl extends ServiceImpl<PlaceMapper, DataPlace> implem
|
|||
Long typeId = item.getTypeId();
|
||||
DateType dateType = typeMap.get(typeId);
|
||||
if (dateType == null) {
|
||||
item.setTypeName(EnumMsg.NO_TYPE.getErrorMessage());
|
||||
item.setTypeName(EnumMsg.NO_TYPE.getMessage());
|
||||
}else{
|
||||
item.setTypeName(dateType.getName());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue