当前位置:   article > 正文

mybatisplus连接sqlserver配置_mybatisplus sqlserver

mybatisplus sqlserver

目录

前言:

一:所需依赖:

二:application.yml

三:分页插件


前言:

本文主要讲解连接sqlserver的配置

一:所需依赖:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.3.4.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.dmdd</groupId>
  12. <artifactId>yifang</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>yifang</name>
  15. <description>yifang</description>
  16. <properties>
  17. <java.version>8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-test</artifactId>
  31. <scope>test</scope>
  32. </dependency>
  33. <dependency>
  34. <groupId>com.baomidou</groupId>
  35. <artifactId>mybatis-plus-boot-starter</artifactId>
  36. <version>3.5.3</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>com.baomidou</groupId>
  40. <artifactId>mybatis-plus-generator</artifactId>
  41. <version>3.5.3</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>com.microsoft.sqlserver</groupId>
  45. <artifactId>sqljdbc4</artifactId>
  46. <scope>system</scope>
  47. <version>4.0</version>
  48. <systemPath>${project.basedir}/src/main/java/com/lib/sqljdbc4-4.0.jar</systemPath>
  49. </dependency>
  50. <!-- &lt;!&ndash; https://mvnrepository.com/artifact/com.microsoft.sqlserver/sqljdbc4 &ndash;&gt;-->
  51. <!-- <dependency>-->
  52. <!-- <groupId>com.microsoft.sqlserver</groupId>-->
  53. <!-- <artifactId>sqljdbc4</artifactId>-->
  54. <!-- <version>4.0</version>-->
  55. <!-- </dependency>-->
  56. </dependencies>
  57. <build>
  58. <plugins>
  59. <plugin>
  60. <groupId>org.springframework.boot</groupId>
  61. <artifactId>spring-boot-maven-plugin</artifactId>
  62. </plugin>
  63. </plugins>
  64. </build>
  65. </project>

注:引用sqlserver比mysql多了如下依赖,并且跟mybatis plus的版本一致

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.5.3</version>
</dependency> 

注:SqlServer连接所需依赖需要以jar包的形式引入

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>sqljdbc4</artifactId>
    <scope>system</scope>
    <version>4.0</version>
    <systemPath>${project.basedir}/src/main/java/com/lib/sqljdbc4-4.0.jar</systemPath>
</dependency>

二:application.yml

  1. server:
  2. port: 8099
  3. # 配置数据源
  4. spring:
  5. datasource:
  6. driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
  7. url: jdbc:sqlserver://localhost:1433;databaseName=HLDB_Dev_012
  8. username: sa
  9. password: jly720609
  10. # 开启驼峰命名的实体类
  11. mybatis-plus:
  12. configuration:
  13. map-underscore-to-camel-case: true
  14. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  15. mapper-locations: classpath:mapper/*.xml
  16. type-aliases-package: com/hl/hlkj/entity

application.properties

  1. spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
  2. spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=HLDB_Dev_012
  3. spring.datasource.username=sa
  4. spring.datasource.password=jly720609

 

三:分页插件

  1. package com.dmdd.yifang.config;
  2. import com.baomidou.mybatisplus.annotation.DbType;
  3. import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
  4. import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
  5. import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. @Configuration
  9. //@MapperScan("scan.your.mapper.package")
  10. public class MybatisPlusConfig {
  11. /**
  12. * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
  13. */
  14. @Bean
  15. public MybatisPlusInterceptor mybatisPlusInterceptor() {
  16. MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
  17. interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.SQL_SERVER2005));
  18. return interceptor;
  19. }
  20. }

 注:需要改为对应数据库的名称,默认的是mysql使用的,sqlserver用不了

interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.SQL_SERVER2005));

四:sqlserver仍然连接不上看这步

 1.打开sqlserver代理

win+r打开

输入services.msc 

 开启代理

二开启数据库远程连接

 右键属性,选择连接,然后开启远程连接

三:启用TCP/IP

alt+s开启搜索,搜索sqlserver配置管理器

 

如果有两个MSSQLSEVER协议代表sqlserver重复安装,需要卸载一个 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/520960
推荐阅读
  

闽ICP备14008679号