我是设计图案的新手. 我正在实施一个工具,可以连接到不同的数据库根据用户的需要. 这是我的代码 struct .
在控制器中,我有我的API调用.下面我粘贴了获取服务器中所有数据库的POST API调用
@PostMapping("/allDatabases")
public List<String> getDatabases(@RequestBody DatabaseModel db)
throws IOException, SQLException {
return migrationInterface.getAllDatabases(db);
}
目前,我通过调用服务包内部接口中的方法来获得响应. 但当数据库服务器发生变化时(例如:Postgres、MySQL),我必须使用不同的查询.
例:
public class PostgresPreparedStatements {
public PreparedStatement getAllDbs(Connection con) throws SQLException {
return con.prepareStatement(
"SELECT datname FROM pg_database
WHERE datistemplate = false;");
}
}
此查询在MySQL数据库中不起作用.因此,我将为不同的数据库保留不同的预准备语句.我的 idea 是从控制器调用BaseAdapter并判断服务器类型,如下所示.
public class BaseAdapter {
public void checkServerType(String server) {
switch(server) {
case "postgres" :
// postgres functions
break;
case "mysql" :
// mysql functions
break;
default:
break;
}
}
}
如果服务器是Postgres,我想调用PostgresConnector.java.在连接器中,我想调用Facade来调用函数和相关查询.
你知道怎么做吗?
请注意:目前我正在为Postgres和MySQL实现这一点,但在future ,这应该适用于任何数据库.