我创建了一个springboot应用程序,它使用Spring Boot和Apache Camel JDBC组件在PostgreSQL中插入一条记录。
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jdbc</artifactId>
<version>${camel.version}</version>
<!-- use the same version as your Camel core version -->
</dependency>
对于数据库配置,我使用一个www.example.com文件创建了以下java类application.properties:
数据库配置java类:
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import org.apache.camel.support.SimpleRegistry;
import org.apache.commons.dbcp2.BasicDataSource;
public class DatabaseConfiguration {
public static SimpleRegistry createDatabaseConfiguration() throws IOException {
Properties properties = new Properties();
properties.load(new FileInputStream("src/main/resources/application.properties"));
BasicDataSource basic = new BasicDataSource();
basic.setDriverClassName(properties.getProperty("PostgresDBClassname"));
basic.setUsername(properties.getProperty("PostgresDBUsername"));
basic.setPassword(properties.getProperty("PostgresDBPassword"));
basic.setUrl(properties.getProperty("PostgresDBUrl"));
SimpleRegistry registry = new SimpleRegistry();
registry.bind("myDataSource", basic);
return registry;
}
}
application.properties 档案:
PostgresDBUsername = username
PostgresDBPassword = password
PostgresDBClassname = org.postgresql.Driver
PostgresDBUrl = jdbc:postgresql://localhost:5432/postgres
我以下面的方式编写了路由器,并注意到我试图用myDataSource替换dataSource:
@Component
public class InsertRestService extends RouteBuilder {
@Override
public void configure() throws Exception {
rest("/").produces("text/plain")
.get("insert")
.to("direct:hello");
from("direct:hello")
.transform().simple("INSERT INTO person (name, country) VALUES (DANY, LB)")
.to("jdbc:dataSource") //spring boot starter jdbc creates the bean in the registry
.transform().simple("Data inserted in Postgres successfully");
}
}
出现以下错误:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
我在www.example.com文件PostgresDBUrl中提供了URLapplication.properties
另外请注意,我下载了jdbc驱动程序jar文件,并将其添加到模块路径中,右键单击包后,构建路径,配置构建路径x1c 0d1x那么我该怎么做才能解决这个问题呢?谢谢!!
2条答案
按热度按时间eblbsuwk1#
我删除了配置文件(DatabaseConfiguration),并将www.example.com文件的内容替换application.properties为:
而且成功了!
pnwntuvh2#
检查您的pom.xml文件,并将其打包为war。这对我很有效。