繁体   English   中英

使用Derby配置Spring-Boot Autowired JdbcTemplate

[英]Configuring Spring-Boot Autowired JdbcTemplate with Derby

我已将gs-relational-data-access-complete的副本克隆到我的工作区,下载了Apache Derby的独立副本并将其启动为“./bin/startNetworkServer”。

我想指出我的Spring Boot项目使用Apache Derby的外部实例,而不是Spring Boot使用的默认嵌入式H2数据库。 为了节省时间,我想使用Spring Boot @Autowired注释。

当我发布Derby的新副本时,没有问题:

./bin/startNetworkServer

11月21日星期六11:11:06 GMT 2015:使用基本服务器安全策略安装安全管理器。

11月21日星期六11:11:06 GMT 2015:Apache Derby网络服务器 - 10.12.1.1 - (1704137)启动并准备接受端口1527上的连接

当我启动Spring Boot应用程序时出现问题:

2015-11-21 11:55:07.312 ERROR 11361 --- [main] oatomcat.jdbc.pool.ConnectionPool:无法创建池的初始连接。

java.sql.SQLException:驱动程序:org.apache.derby.jdbc.EmbeddedDriver@db9d03fc为URL返回null:jdbc:derby://10.12.1.1:1527 / example1; create = true; user = test1; password = pass1

在org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:326)〜[tomcat-jdbc-8.0.28.jar:na]

如何在仍然利用Spring Boot @Autowired注释的同时解决此问题的任何帮助将不胜感激。

我的工作区如下:

pom.xml
src/main/java/hello
----Application.java
----Customer.java
src/main/resources
----application.properties

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.springframework</groupId>
<artifactId>gs-relational-data-access</artifactId>
<version>0.1.0</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.0.RELEASE</version>
</parent>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

application.properties:

spring.datasource.url=jdbc:derby://10.12.1.1:1527/example1;create=true;user=test1;password=pass1
spring.datasource.username=test1
spring.datasource.password=pass1

Application.java:

package hello;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@SpringBootApplication
public class Application implements CommandLineRunner {

    private static final Logger log = LoggerFactory.getLogger(Application.class);

    public static void main(String args[]) {
        SpringApplication.run(Application.class, args);
    }

    @Autowired
    JdbcTemplate jdbcTemplate;

这是不匹配的:

Driver:org.apache.derby.jdbc.EmbeddedDriver@db9d03fc returned null 
for URL:jdbc:derby://10.12.1.1:1527/example1;create=true;user=test1;password=pass1

您指定使用Derby 嵌入式驱动程序,但您已为Derby提供了数据库的客户端 - 服务器 URL。

Derby检测到不匹配并拒绝您的连接尝试。

如果要使用客户端 - 服务器 URL,则需要使用Derby ClientDriver类,并且需要在CLASSPATH中使用derbyclient.jar。

如果要使用嵌入式驱动程序,则需要使用嵌入式驱动程序URL,例如

jdbc:derby:example1;create=true;user=test1;password=pass1

更改

<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derby</artifactId>
</dependency>

通过

<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derbyclient</artifactId>
    <version>10.13.1.1</version>
</dependency>

并在配置中:

spring.datasource.url=jdbc:derby://10.12.1.1:1527/example1;create=true
spring.datasource.username=test1
spring.datasource.password=pass1
spring.datasource.driver-class-name=org.apache.derby.jdbc.ClientDriver

问题是derby依赖是针对嵌入式数据库的,需要客户端依赖。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM