1.导坐标:
<dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <version>6.1.0.jre8</version> </dependency> 2.连接配置:
jdbc.userName=sa jdbc.password=123456 jdbc.url=jdbc:sqlserver://192.168.66.188;DatabaseName= jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver 3.配置bean
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"></property> <property name="url" value="${jdbc.url}"></property> <property name="password" value="${jdbc.password}"></property> <property name="name" value="${jdbc.userName}"></property> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="1"/> <property name="minIdle" value="1"/> <property name="maxActive" value="20"/> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait" value="60000"/> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000"/> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000"/> <property name="validationQuery" value="select 'x'"></property> <property name="testWhileIdle" value="true"/> <property name="testOnBorrow" value="false"/> <property name="testOnReturn" value="false"/> <!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> <property name="poolPreparedStatements" value="false"/> <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/> </bean> 4.可能出现的问题点及解决办法: (1)连接Sqlserver报错:the driver could not establish a secure connection to SQL Server.... Server chose TLSv1, but that protocol version is not enabled or not supported by the client when loading an SQL Server resource
解决办法:找到java安装的JDK目录下面:\JDK\conf\security\java.security文件,找到jdk.tls.disabledAlgorithms配置项,将其中的TLSv1, TLSv1.1,3DES_EDE_CBC删除即可;
(2)连接sqlserver时出现错误: validateConnection false
解决办法:sqlserver必须配置 <property name="validationQuery" value="select 'x'"></property>