【问题标题】:Spring:Connect Sql server from spring boot serviceSpring:从 Spring Boot 服务连接 Sql 服务器
【发布时间】:2018-01-30 13:13:45
【问题描述】:

我有 rest crud 服务,这将帮助我发布和获取请求并从 sql server 接收响应,在我的 application.properties 中我使用类似的数据:

server.port=9004
spring.datasource.url=jdbc:sqlserver://localhost/1433;databaseName=test1
spring.datasource.username=sa
spring.datasource.password=*****
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.show-sql=true
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2014Dialect
spring.jpa.hibernate.ddl-auto =ddl-auto

这是我的 POM:

<maven.compiler.source>1.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>
</properties>

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.6.RELEASE</version>
    </parent>
 <dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>4.0</version>
        </dependency>
        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
      </dependency>    

        </dependencies>

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

当我将它作为 java 项目运行时,我总是遇到这个错误:

引起:org.springframework.beans.factory.BeanCreationException: 无法自动装配字段:私有 org.springframework.boot.autoconfigure.web.HttpMessageConverters org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.messageConverters; 嵌套异常是

和:

在类路径中定义了名称“mappingJackson2HttpMessageConverter” 资源 [org/springframework/boot/autoconfigure/web/JacksonHttpMessageConvertersConfiguration$MappingJackson2HttpMessageConverterConfiguration.class]: 通过构造函数参数表达的不满足的依赖关系 [com.fasterxml.jackson.databind.ObjectMapper] 类型的索引 0:错误 创建在类路径资源中定义的名称为“objectMapper”的bean [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class]: 通过工厂方法实例化 Bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:失败 实例化 [com.fasterxml.jackson.databind.ObjectMapper]:工厂 方法“objectMapper”抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 使用在类路径资源中定义的名称“config”创建 bean [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class]: 通过工厂方法实例化 Bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:失败 实例化 [org.springframework.data.rest.core.config.RepositoryRestConfiguration]: 工厂方法 'config' 抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 使用在类路径中定义的名称“resourceMappings”创建 bean 资源 [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class]: 通过工厂方法实例化 Bean 失败;嵌套异常是

我应该在我的 pom 或属性文件中进行哪些更改才能使我的程序正常工作?

【问题讨论】:

    标签: java spring hibernate maven


    【解决方案1】:

    SQL Server 依赖已弃用,使用以下方式与 SQL Server 交互

    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <version>6.1.0.jre8</version>
    </dependency> 
    

    点击链接了解更多信息

    Configuring Spring Boot for Microsoft SQL Server

    Open sourcing the Microsoft JDBC Driver and Maven support

    【讨论】:

      【解决方案2】:

      由于是REST应用,需要在pom.xml中添加spring-boot-starter-web依赖

          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-web</artifactId>
          </dependency>
          <dependency>
             <groupId>com.microsoft.sqlserver</groupId>
             <artifactId>sqljdbc4</artifactId>
             <version>4.0</version>
           </dependency>  
      

      还在配置中添加连接属性(默认为application.properties):

      spring.datasource.url=jdbc:sqlserver://localhost;databaseName=springbootdb
      spring.datasource.username=sa
      spring.datasource.password=replace_value
      spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
      spring.jpa.show-sql=true
      spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
      spring.jpa.hibernate.ddl-auto = create-drop
      

      试试那个,看看你是否可以构建/运行项目。

      【讨论】:

      • 首先感谢您的回复,但不幸的是它不起作用我的意思是我现在无法启动我的服务我有这个错误:org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null当 'hibernate.dialect' 未设置时
      • 是否需要将持久化xml或其他配置文件添加到meca数据库连接更方便和更适合spring boot应用程序?
      • 是的,需要添加持久化配置。请参阅更新的答案。另请参阅此链接以供参考:springframework.guru/…
      • persistence.xml insiede src/main/resource/META-INF
      • 现在我有这个错误:在类路径资源 [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class] 中定义名称为“config”的 bean 创建错误:通过工厂进行 Bean 实例化方法失败;嵌套异常是 org.springframework.beans.BeanInstantiationException: 无法实例化
      【解决方案3】:

      将您的 url 参数更改为:

      spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=test1
      

      端口号前面必须有冒号(“:”),而不是斜线(“/”)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-04-05
        • 1970-01-01
        • 1970-01-01
        • 2018-07-05
        • 2014-05-10
        • 2019-10-15
        • 2021-07-11
        相关资源
        最近更新 更多