com.github.xiaoyuge5201.config.DataSourceClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datasource-spring-boot-starter Show documentation
Show all versions of datasource-spring-boot-starter Show documentation
数据库连接工具,查询mysql、oracle、sqlserver、postgresql的数据表以及字段信息;同时支持导出数据库设计文档
package com.github.xiaoyuge5201.config;
import com.alibaba.fastjson.JSONArray;
import com.github.xiaoyuge5201.entity.ColumnEntity;
import com.github.xiaoyuge5201.util.ExportDatabaseDocument;
import com.github.xiaoyuge5201.util.QuerySqlUtil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;
/**
* 数据源工具类
*
* @author yugb
*/
public class DataSourceClient implements Serializable {
private static DataSourceClient INSTANCE;
private static MyDataSourceProperties properties;
private DataSourceClient() {
}
public static DataSourceClient getInstance() {
return INSTANCE;
}
/**
* 重写一个方法,使用桥接模式;使用readResolve方法防止单例模式被序列化破坏
*
* @return 实体对象
*/
private Object readResolve() {
return INSTANCE;
}
public MyDataSourceProperties getProperties() {
return properties;
}
private void setProperties(MyDataSourceProperties properties) {
DataSourceClient.properties = properties;
}
private DataSourceClient(MyDataSourceProperties properties) {
DataSourceClient.properties = properties;
}
/**
* 获取对象
*
* @param properties 属性
* @return 对象
*/
static DataSourceClient create(MyDataSourceProperties properties) {
if (INSTANCE == null) {
synchronized (DataSourceClient.class) {
if (INSTANCE == null) {
INSTANCE = new DataSourceClient();
INSTANCE.setProperties(properties);
}
}
}
return INSTANCE;
}
/**
* 查询所有的表结构信息
*
* @return 表结构列表
*/
public List findAllTables() {
return QuerySqlUtil.findAllTables(properties.getDriverClassName(), properties.getUrl(), properties.getUsername(), properties.getPassword(), properties.getDatabase());
}
/**
* 查詢數據庫表的字段信息
*
* @param table 数据表
* @return 表字段列表
*/
public List queryTableFieldsEntity(String table) {
return QuerySqlUtil.queryTableFieldsToColumnEntity(properties.getDriverClassName(), properties.getUrl(), properties.getUsername(), properties.getPassword(), properties.getDatabase(), table);
}
/**
* 查詢數據庫表的字段信息
*
* @param table 数据表
* @return 表字段列表
*/
public List queryTableFields(String table) {
return QuerySqlUtil.queryTableFields(properties.getDriverClassName(), properties.getUrl(), properties.getUsername(), properties.getPassword(), properties.getDatabase(), table);
}
/**
* 查询对应库下所有字段 信息
*
* @return 结果
*/
public List listColumnsByDatasourceParams() {
return QuerySqlUtil.listColumnsByDatasourceParams(properties.getDriverClassName(), properties.getUrl(), properties.getUsername(), properties.getPassword(), properties.getDatabase());
}
/**
* 分页查询数据表数据
*
* @param table 数据表
* @param pageNo 页码
* @param limit 页容量
* @param columns 字段列表
* @throws Exception 异常信息
* @return 结果
*/
public JSONArray queryPageData(String table, List columns, Integer pageNo, Integer limit) throws Exception {
return QuerySqlUtil.queryPageData(properties.getDriverClassName(), properties.getDatabase(), table, properties.getUrl(), properties.getUsername(), properties.getPassword(), columns, pageNo, limit);
}
/**
* 导出数据库设计文档
*
* @param response 返回对象
* @param request 请求对象
*/
public void exportDatabaseDocument(HttpServletResponse response, HttpServletRequest request) {
ExportDatabaseDocument.export(response, request, properties.getDriverClassName(), properties.getUrl(), properties.getUsername(), properties.getPassword(), properties.getDatabase());
}
}