最近在做数据相关的平台,并且需要支持多数据源,所以把用到的sql记录一下
三种数据源的链接方式都是不同的,所以抽象了几个方法用于获取DB链接
/**
* @program: common-info
* @description: 数据源属性封装类
* @author: lvqiushi
* @create: 2019-03-28 13:51
**/
@Data
public class DataSourceAttrDTO implements Serializable {
/**
* 数据库类型,1mysql,2oracle
*/
private Integer sourceType;
/**
* 数据库地址
*/
private String sourceAddress;
/**
* 数据库端口号
*/
private String sourcePort;
/**
* 用户名
*/
private String userName;
/**
* 密码
*/
private String userPassword;
/**
* 数据库名称
*/
private String dbName;
}
/**
* @program: common-info
* @description: 数据库类型枚举类
* @author: lvqiushi
* @create: 2019-04-01 19:13
**/
@Getter
@AllArgsConstructor
public enum DataBaseTypeEnum {
MYSQL(1, "mysql类型数据库", "com.mysql.jdbc.Driver", "jdbc:mysql://"),
ORACLE(2, "oracle类型数据库", "oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@"),
SQL_SERVER(3, "sql server", "com.microsoft.sqlserver.jdbc.SQLServerDriver", "jdbc:sqlserver://");
/**
* 类型
*/
private Integer type;
/**
* 描述
*/
private String desc;
/**
* 数据库驱动器名称
*/
private String driver;
/**
* 数据库url前缀
*/
private String urlPrefix;
}
/**
* @Description: 根据链接参数拼接
* @param dataSourceAttrDTO 数据源参数
* @return: java.lang.String
* @Author: lvqiushi
* @Date: 2019-04-01
*/
private String getDBUrl(DataSourceAttrDTO dataSourceAttrDTO) {
if (dataSourceAttrDTO.getJdbcUrl() != null) {
return dataSourceAttrDTO.getJdbcUrl();
}
// dbName有可能为null 要注意判断
String dbName = "";
if (StringUtils.isNotEmpty(dataSourceAttrDTO.getDbName())) {
if (DataBaseTypeEnum.SQL_SERVER.getType().equals(dataSourceAttrDTO.getSourceType())) {
dbName = ";databaseName=" + dataSourceAttrDTO.getDbName();
} else {
dbName = "/" + dataSourceAttrDTO.getDbName();
}
}
if (DataBaseTypeEnum.MYSQL.getType().equals(dataSourceAttrDTO.getSourceType())) {
return DataBaseTypeEnum.MYSQL.getUrlPrefix() +
dataSourceAttrDTO.getSourceAddress() + ":" +
dataSourceAttrDTO.getSourcePort() + dbName;
} else if (DataBaseTypeEnum.ORACLE.getType().equals(dataSourceAttrDTO.getSourceType())) {
return DataBaseTypeEnum.ORACLE.getUrlPrefix() +
dataSourceAttrDTO.getSourceAddress() + ":" +
dataSourceAttrDTO.getSourcePort() + dbName;
} else if (DataBaseTypeEnum.SQL_SERVER.getType().equals(dataSourceAttrDTO.getSourceType())) {
return DataBaseTypeEnum.SQL_SERVER.getUrlPrefix() +
dataSourceAttrDTO.getSourceAddress() + ":" +
dataSourceAttrDTO.getSourcePort() + dbName;
}
return null;
}
/**
* @Description: 获取数据库驱动管理器
* @param dataSource
* @return: org.springframework.jdbc.datasource.DriverManagerDataSource
* @Author: lvqiushi
* @Date: 2019-04-02
*/
private DriverManagerDataSource getDriverManager(DataSourceAttrDTO dataSource) {
DriverManagerDataSource dataSourceManager = new DriverManagerDataSource();
dataSourceManager.setDriverClassName(this.getDBDriverName(dataSource.getSourceType()));
dataSourceManager.setUrl(this.getDBUrl(dataSource));
dataSourceManager.setUsername(dataSource.getUserName());
dataSourceManager.setPassword(dataSource.getUserPassword());
return dataSourceManager;
}
show databases
select table_name tableName, table_comment tableComment from information_schema.tables where table_schema = (select database())
select column_name columnName, data_type dataType, column_type, column_comment columnComment,column_default, is_nullable, column_key columnKey, extra
from information_schema.columns
where table_name = ? and table_schema = (select database())
order by ordinal_position
select table_name, comments from all_tab_comments
where TABLE_TYPE = 'TABLE'
and owner not in ('SYS','SYSTEM', 'MDSYS', 'APEX_030200','XDB','OLAPSYS','EXFSYS','CTXSYS')
select t1.table_name,t1.column_name,data_type,data_length,comments,
case
when nullable = 'Y' then 1
when nullable = 'N' then 0
else 0
end as nullable,
case
when t3.column_name is null then 0
else 1
end as is_primary ,data_scale
from user_col_comments t2
left join user_tab_cols t1 on t1.column_name = t2.column_name and t1.table_name = t2.table_name
left join (
select col.column_name as column_name,con.table_name as table_name from user_constraints con,user_cons_columns col
where con.constraint_name = col.constraint_name
and con.constraint_type = 'P'
) t3 on t3.table_name = t1.table_name and t1.column_name = t3.column_name
where t1.table_name = ?
select Name FROM Master.dbo.SysDatabases ORDER BY Name
select tbs.name , CAST(ds.value AS NVARCHAR(128)) AS comment
FROM sysObjects tbs
left join sys.extended_properties ds on ds.major_id=tbs.id and ds.minor_id=0
Where XType='U'
select
obj.name as tableName,
col.name as columnName,
t.name as dataType,
col.max_length as dataLength,
CAST(ep.value AS NVARCHAR(128)) AS comment,
col.is_nullable as isNullable,
(
select top 1 ind.is_primary_key from sys.index_columns ic
left join sys.indexes ind
on ic.object_id=ind.object_id
and ic.index_id=ind.index_id
and ind.name like 'PK_%'
where ic.object_id=obj.object_id
and ic.column_id=col.column_id
) as isPrimaryKey
from sys.objects obj
inner join sys.columns col on obj.object_id=col.object_id
left join sys.types t on t.user_type_id=col.user_type_id
left join sys.extended_properties ep on ep.major_id=obj.object_id
and ep.minor_id=col.column_id
and ep.name='MS_Description'
where obj.name = ?