要部署到客戶的服務(wù)器上,需支持多種系統(tǒng),主要是windows、linux
解決思路為解決分隔符問(wèn)題,先找方法區(qū)分操作系統(tǒng)
// 獲取操作系統(tǒng)名稱
String osName = System.getProperty("os.name");
// 區(qū)分操作系統(tǒng)
osName.startsWith("Windows");//Windows
osName.startsWith("Linux");//Linux
osName.startsWith("Mac");//Mac
需求二支持jar包外的配置文件配置數(shù)據(jù)源信息
解決思路private Properties getProperties() {Properties properties = new Properties();
try {String path = System.getProperty("user.dir");
String sp = "/";
if (osName.startsWith("Windows")) {sp = "\\";
}
log.info("外部配置文件全路徑:{}", path + sp + "datasource.properties");
FileInputStream inputStream = new FileInputStream(path + sp + "datasource.properties");
properties.load(inputStream);
inputStream.close();
} catch (IOException e) {log.error("獲取外部配置失?。簕}", e.getMessage());
throw new RuntimeException(e);
}
return properties;
}
加載配置文件完成就可以加載數(shù)據(jù)源了
@Configuration
public class DataSourceConfig {@Bean
public DataSource primaryDataSource() {Properties properties = getProperties();
HikariConfig config = new HikariConfig();
config.setDriverClassName(properties.getProperty("spring.datasource.database"));
config.setJdbcUrl(properties.getProperty("spring.datasource.url")); //數(shù)據(jù)源url
config.setUsername(properties.getProperty("spring.datasource.username")); //用戶名
config.setPassword(properties.getProperty("spring.datasource.password")); //密碼
config.addDataSourceProperty("cachePrepStmts", "true"); //是否自定義配置,為true時(shí)下面兩個(gè)參數(shù)才生效
config.addDataSourceProperty("prepStmtCacheSize", "250"); //連接池大小默認(rèn)25,官方推薦250-500
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); //單條語(yǔ)句大長(zhǎng)度默認(rèn)256,官方推薦2048
// config.addDataSourceProperty("useServerPrepStmts", "true"); //新版本MySQL支持服務(wù)器端準(zhǔn)備,開啟能夠得到顯著性能提升
config.addDataSourceProperty("useLocalSessionState", "true");
config.addDataSourceProperty("useLocalTransactionState", "true");
config.addDataSourceProperty("rewriteBatchedStatements", "true");
config.addDataSourceProperty("cacheResultSetMetadata", "true");
config.addDataSourceProperty("cacheServerConfiguration", "true");
config.addDataSourceProperty("elideSetAutoCommits", "true");
config.addDataSourceProperty("maintainTimeStats", "false");
HikariDataSource ds = new HikariDataSource(config);
return ds;
}
}
需求三數(shù)據(jù)源支持多種數(shù)據(jù)庫(kù),主要是mysql、oracle、sqlserver
解決思路com.oracle.database.jdbc ojdbc8 21.1.0.0 com.oracle.database.nls orai18n 21.1.0.0 com.microsoft.sqlserver mssql-jdbc 11.2.0.jre8 mysql mysql-connector-java 8.0.28
spring.datasource.database=oracle #sqlserver、mysql
spring.datasource.url=jdbc:oracle:thin:@ip:port/xxx
spring.datasource.username=aaa
spring.datasource.password=bbb
需要注意的是數(shù)據(jù)庫(kù)url的格式是不同的
oracle:jdbc:oracle:thin:@ip:port/sid
mysql:jdbc:mysql://ip:port/databaseName
sqlserver:jdbc:sqlserver://ip:port;Database=databaseName
@Bean
public DataSource primaryDataSource() {Properties properties = getProperties();
String driver;
if ("oracle".equalsIgnoreCase(properties.getProperty("spring.datasource.database"))) {driver = "oracle.jdbc.OracleDriver";
} else if ("sqlserver".equalsIgnoreCase(properties.getProperty("spring.datasource.database"))) {driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
} else {driver = "com.mysql.cj.jdbc.Driver";
}
HikariConfig config = new HikariConfig();
config.setDriverClassName(driver);
config.setJdbcUrl(properties.getProperty("spring.datasource.url")); //數(shù)據(jù)源url
config.setUsername(properties.getProperty("spring.datasource.username")); //用戶名
config.setPassword(properties.getProperty("spring.datasource.password")); //密碼
HikariDataSource ds = new HikariDataSource(config);
return ds;
}
注意若連接SQL server報(bào)如下錯(cuò):
com.microsoft.sqlserver.jdbc.SQLServerException: 驅(qū)動(dòng)程序無(wú)法通過(guò)使用安全套接字層(SSL)加密與 SQL Server 建立 安全連接
首先,查一下SQL server版本與驅(qū)動(dòng)版本是否對(duì)應(yīng),【微笑】SQL server就是好用
其次,根據(jù)此鏈接,在url后添加trustServerCertificate=true即可,為了方便實(shí)施人員,DataSourceConfig.java修改如下
@Bean
public DataSource primaryDataSource() {Properties properties = getProperties();
String driver;
String url = properties.getProperty("spring.datasource.url");
if ("oracle".equalsIgnoreCase(properties.getProperty("spring.datasource.database"))) {driver = "oracle.jdbc.OracleDriver";
} else if ("sqlserver".equalsIgnoreCase(properties.getProperty("spring.datasource.database"))) {driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
url += ";encrypt=true;trustServerCertificate=true";
} else {driver = "com.mysql.cj.jdbc.Driver";
}
HikariConfig config = new HikariConfig();
config.setDriverClassName(driver);
config.setJdbcUrl(url); //數(shù)據(jù)源url
config.setUsername(properties.getProperty("spring.datasource.username")); //用戶名
config.setPassword(properties.getProperty("spring.datasource.password")); //密碼
HikariDataSource ds = new HikariDataSource(config);
return ds;
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧