工作小记:外置配置文件+多种数据库支持-创新互联

工作小记:外置配置文件+多种数据库支持
  • 需求
  • 解决方案
    • 需求一
      • 解决思路
      • 涉及代码
    • 需求二
      • 解决思路
      • 涉及代码
    • 需求三
      • 解决思路
      • 涉及代码
  • 注意

创新互联公司服务项目包括漾濞网站建设、漾濞网站制作、漾濞网页制作以及漾濞网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,漾濞网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到漾濞省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!需求
  1. 要部署到客户的服务器上,需支持多种系统,主要是windows、linux
  2. 支持jar包外的配置文件配置数据源信息
  3. 数据源支持多种数据库,主要是mysql、oracle、sqlserver
解决方案 需求一

要部署到客户的服务器上,需支持多种系统,主要是windows、linux

解决思路
  • 关注到win和linux的区别,其他都好说,就是路径的分隔符需要区分一下
  • linux部署简单,但win需要部署成服务,需要借助winsw,参考用winsw将jar包做成window后台服务
涉及代码

为解决分隔符问题,先找方法区分操作系统

// 获取操作系统名称
String osName = System.getProperty("os.name");
// 区分操作系统
osName.startsWith("Windows");//Windows
osName.startsWith("Linux");//Linux
osName.startsWith("Mac");//Mac
需求二

支持jar包外的配置文件配置数据源信息

解决思路
  • 外置配置文件置于jar包相同目录下,配置文件名称定为datasource.properties,简单点
  • 获取项目绝对路径(注意此处就用到区分分隔符)
涉及代码
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

解决思路
  • 首先要引入依赖,数据库驱动
  • 为了方便实施人员,配置文件上只配置数据库类型
涉及代码
  1. 引入依赖
com.oracle.database.jdbcojdbc821.1.0.0com.oracle.database.nlsorai18n21.1.0.0com.microsoft.sqlservermssql-jdbc11.2.0.jre8mysqlmysql-connector-java8.0.28
  1. datasource.properties
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
  1. DataSourceConfig.java修改如下
@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元起,快前往官网查看详情吧

当前题目:工作小记:外置配置文件+多种数据库支持-创新互联
转载来于:https://www.cdcxhl.com/article34/dcsipe.html

成都网站建设公司_创新互联,为您提供网站策划网站设计公司网站收录网站建设电子商务营销型网站建设

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

外贸网站建设