要部署到客户的服务器上,需支持多种系统,主要是windows、linux
解决思路为解决分隔符问题,先找方法区分操作系统
// 获取操作系统名称
String osName = System.getProperty("os.name");
// 区分操作系统
osName.startsWith("Windows");//Windows
osName.startsWith("Linux");//Linux
osName.startsWith("Mac");//Mac
需求二支持jar包外的配置文件配置数据源信息
解决思路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;
}
加载配置文件完成就可以加载数据源了
@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")); //数据源url
config.setUsername(properties.getProperty("spring.datasource.username")); //用户名
config.setPassword(properties.getProperty("spring.datasource.password")); //密码
config.addDataSourceProperty("cachePrepStmts", "true"); //是否自定义配置,为true时下面两个参数才生效
config.addDataSourceProperty("prepStmtCacheSize", "250"); //连接池大小默认25,官方推荐250-500
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); //单条语句大长度默认256,官方推荐2048
// config.addDataSourceProperty("useServerPrepStmts", "true"); //新版本MySQL支持服务器端准备,开启能够得到显著性能提升
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;
}
}
需求三数据源支持多种数据库,主要是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
需要注意的是数据库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")); //数据源url
config.setUsername(properties.getProperty("spring.datasource.username")); //用户名
config.setPassword(properties.getProperty("spring.datasource.password")); //密码
HikariDataSource ds = new HikariDataSource(config);
return ds;
}
注意若连接SQL server报如下错:
com.microsoft.sqlserver.jdbc.SQLServerException: 驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立 安全连接
首先,查一下SQL server版本与驱动版本是否对应,【微笑】SQL server就是好用
其次,根据此链接,在url后添加trustServerCertificate=true即可,为了方便实施人员,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); //数据源url
config.setUsername(properties.getProperty("spring.datasource.username")); //用户名
config.setPassword(properties.getProperty("spring.datasource.password")); //密码
HikariDataSource ds = new HikariDataSource(config);
return ds;
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧