2025年3月13日 星期四 甲辰(龙)年 月十二 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > Spring Boot

springboot连接sqlserver数据库(包对)

时间:08-14来源:作者:点击数:46
城东书院 www.cdsy.xyz

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一样,想使用哪种方式都可以。我就不一一概述了,其中有误解的地方还望指教。

城东书院 www.cdsy.xyz
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐