springboot连接sqlserver数据库(包对)

springboot连接sqlserver数据库(包对)

最近由于公司业务需要,我写的springboot项目要连接内网的sqlserver数据库。心想SQL server跟 mysql一样都是关系型数据库,配置也类似。到网上查找资料进行sqlserver的整合,结果出了一些错误。以下是我通过查找大量资料不断探索出的包对的方法。

一、在pom文件加入SQL server依赖

    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>sqljdbc4</artifactId>
        <version>4.0</version>
    </dependency>

如果中央仓库拉不下来jar包,点击这里https://pan.baidu.com/s/1CppB3vSAPQpK05_uHaKPjg
,提取码0fcq,我将整个sqlserver驱动包文件夹上传了,你只要按照对应的路径复制就好。

二、配置yml文件

spring:
  datasource:
    driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
    url: jdbc:sqlserver://ip地址:端口;DatabaseName=数据库名
    username: 用户名
    password: 密码
    maxActive: 20
    initialSize: 1
    maxWait: 60000
    minIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 1
    testWhileIdle: true
    testOnBorrow: true
    testOnReturn: true
    poolPreparedStatements: true
    maxOpenPreparedStatements: 20

记得将其中的ip地址、端口、数据库名、用户名、密码改为你对应的数据,还有有些同学用的是properties文件,其实是一样的配置到spring节点下的datasource就行。

三、操作sqlserver数据库

引入mybatis的依赖

    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.2</version>
    </dependency>

使用注解或者xml方式注入接口,如下:

@Mapper
public interface HdfsDownloadMapper {

@Select("select id,fPath as FPath,fName as FName,createTime as CreateTime from File_Attribute where fName=#{fileName}")
public ArrayList<FileAttribute> findFileDataByName(String fileName);

@Select("select id,fPath as FPath,fName as FName,createTime as CreateTime from File_Attribute")
public ArrayList<FileAttribute> findAll();

}
接下来的操作跟操作mysql一样,想使用哪种方式都可以。我就不一一概述了,其中有误解的地方还望指教。

发布了25 篇原创文章 · 获赞 58 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/student_zz/article/details/89963280