获取数据库下的所有表名
parent
7a1dfe1f90
commit
e4d2d5db6d
File diff suppressed because one or more lines are too long
|
@ -3,6 +3,8 @@ package com.etl.cleaning.controller;
|
||||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||||
import com.etl.cleaning.domian.pojo.DataPlace;
|
import com.etl.cleaning.domian.pojo.DataPlace;
|
||||||
import com.etl.cleaning.domian.request.TableRequest;
|
import com.etl.cleaning.domian.request.TableRequest;
|
||||||
|
import com.etl.cleaning.enums.EnumMsg;
|
||||||
|
import com.etl.cleaning.server.GetTableServerTest;
|
||||||
import com.etl.cleaning.service.PlaceService;
|
import com.etl.cleaning.service.PlaceService;
|
||||||
import com.etl.common.result.Result;
|
import com.etl.common.result.Result;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
@ -10,6 +12,9 @@ import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.validation.Valid;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取表名控制器
|
* 获取表名控制器
|
||||||
*/
|
*/
|
||||||
|
@ -17,12 +22,12 @@ import org.springframework.web.bind.annotation.RestController;
|
||||||
@RequestMapping("/dataTable")
|
@RequestMapping("/dataTable")
|
||||||
public class GetTableController {
|
public class GetTableController {
|
||||||
|
|
||||||
|
|
||||||
private final PlaceService placeService;
|
private final PlaceService placeService;
|
||||||
|
|
||||||
public GetTableController(PlaceService placeService) {
|
public GetTableController(PlaceService placeService) {
|
||||||
this.placeService = placeService;
|
this.placeService = placeService;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取所有表名
|
* 获取所有表名
|
||||||
*
|
*
|
||||||
|
@ -30,12 +35,18 @@ public class GetTableController {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@PostMapping("/getTableName")
|
@PostMapping("/getTableName")
|
||||||
public Result getTableName(@RequestBody TableRequest tableRequest) {
|
public Result getTableName(@RequestBody @Valid TableRequest tableRequest) {
|
||||||
LambdaUpdateWrapper<DataPlace> eqed = new LambdaUpdateWrapper<DataPlace>().eq(DataPlace::getName, tableRequest.getName()).or().eq(DataPlace::getTypeId, tableRequest.getTypeId());
|
LambdaUpdateWrapper<DataPlace> eqed = new LambdaUpdateWrapper<DataPlace>().eq(DataPlace::getName, tableRequest.getName()).or().eq(DataPlace::getTypeId, tableRequest.getTypeId());
|
||||||
DataPlace dataPlace = placeService.getOne(eqed);
|
DataPlace dataPlace = placeService.getOne(eqed);
|
||||||
if (dataPlace == null) {
|
if (dataPlace == null) {
|
||||||
throw new RuntimeException("您所选中的数据源不存在");
|
throw new RuntimeException("您所选中的数据源不存在");
|
||||||
}
|
}
|
||||||
return Result.success();
|
String dataUrl = dataPlace.getDataUrl();
|
||||||
|
String dataName = dataPlace.getDataName();
|
||||||
|
String password = dataPlace.getPassword();
|
||||||
|
String username = dataPlace.getUsername();
|
||||||
|
String url = EnumMsg.URL_PRE.getMessage() + "://" + dataUrl + "/" + dataName;
|
||||||
|
List<String> tableNames = GetTableServerTest.getTableNames(url, dataName, username, password);
|
||||||
|
return Result.success(tableNames);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package com.etl.cleaning.domian.request;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import javax.validation.constraints.NotBlank;
|
import javax.validation.constraints.NotBlank;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
|
@ -11,6 +12,7 @@ import javax.validation.constraints.NotNull;
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
public class TableRequest {
|
public class TableRequest {
|
||||||
/**
|
/**
|
||||||
* 数据源类型ID
|
* 数据源类型ID
|
||||||
|
@ -25,5 +27,6 @@ public class TableRequest {
|
||||||
@NotBlank(message = "数据源描述不能为空")
|
@NotBlank(message = "数据源描述不能为空")
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package com.etl.cleaning.server;
|
package com.etl.cleaning.server;
|
||||||
|
|
||||||
|
|
||||||
|
import com.etl.cleaning.enums.EnumMsg;
|
||||||
|
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -9,14 +11,14 @@ import java.util.List;
|
||||||
* 获取数据库下的所有表名
|
* 获取数据库下的所有表名
|
||||||
*/
|
*/
|
||||||
public class GetTableServerTest {
|
public class GetTableServerTest {
|
||||||
public static List<String> getTableNames(String url,String username,String password){
|
public static List<String> getTableNames(String url,String dataName,String username,String password){
|
||||||
ArrayList<String> tableNames = new ArrayList<>();
|
ArrayList<String> tableNames = new ArrayList<>();
|
||||||
Connection connection = null;
|
Connection connection = null;
|
||||||
try {
|
try {
|
||||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
Class.forName(EnumMsg.DRIVER_CLASS_NAME.getMessage());
|
||||||
connection = DriverManager.getConnection(url, username, password);
|
connection = DriverManager.getConnection(url, username, password);
|
||||||
DatabaseMetaData metaData = connection.getMetaData();
|
DatabaseMetaData metaData = connection.getMetaData();
|
||||||
ResultSet tables = metaData.getTables(null, null, "%", new String[]{"TABLE"});
|
ResultSet tables = metaData.getTables(dataName, null, "%", new String[]{"TABLE"});
|
||||||
while (tables.next()){
|
while (tables.next()){
|
||||||
tableNames.add(tables.getString(3));
|
tableNames.add(tables.getString(3));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue