SpringBoot连接SQL Server数据库

一、POM文件中加入SQL Server JDBC依赖

注意:很多博客中说引入的jar是Sqljdbc4,如果只引入该jar,程序启动会报错。经过多次测试是 mssql-jdbc。

<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>7.4.1.jre8</version>
</dependency>

 

二、配置yml文件

说明:在mysql中,url后面可以拼接参数,

jdbc:mysql://IP地址:端口/数据库名称?useSSL=false&useUnicode=true&characterEncoding=UTF-8
但是,经过测试,在sql server中不能这样用,关于sql server如何配置这些参数,百度也没有太多的文章。

# spring配置
spring:
  # Druid连接池配置
  datasource:
    druid:
      driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
      url: jdbc:sqlserver://ip地址:端口;DatabaseName=数据库名
      username: 用户名
      password: 密码
      initial-size: 1
      min-idle: 1
      max-active: 2
      maxWait: 60000
      minEvictableIdleTimeMillis: 300000
      poolPreparedStatements: true
      share-prepared-statements: true
      maxPoolPreparedStatementPerConnectionSize: 50
      testOnBorrow: false
      testOnReturn: false
      testWhileIdle: true
      timeBetweenEvictionRunsMillis: 60000
      validationQuery: select 1

猜你喜欢

转载自blog.csdn.net/qq_41057885/article/details/109290474