ash*_*shu 6 postgresql jdbc gradle
我正在尝试使用SpringBoot和Postgres数据库开发Web应用程序。但是,在连接到应用程序时,出现错误“无法确定合适的驱动程序类”,按照较早的文章中的建议,我尝试使用不同版本的jdbc的驱动程序,还尝试手动为NamedParameterJdbcTemplate创建bean。我还验证了存在库并且可以从Java代码访问这些库,并且这些库也存在于classpath中。但是它仍然给同样的问题。我正在使用gradle将所有jar导入构建路径。
这是代码的git存储库:https : //github.com/ashubisht/sample-sbs.git
Gradle依赖代码:
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-websocket")
compile("org.springframework.boot:spring-boot-starter-jdbc")
//compile("org.postgresql:postgresql")
compile("org.postgresql:postgresql:9.4-1206-jdbc42")
testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile group: 'junit', name: 'junit', version: '4.12'
}
Run Code Online (Sandbox Code Playgroud)
用于构建Bean的代码
@Configuration
@PropertySource("classpath:application.properties")
public class Datasource {
@Value("${db.driverClassName}")
private String driverClass;
@Value("${db.url}")
private String url;
@Value("${db.username}")
private String username;
@Value("${db.password}")
private String password;
@Bean
public NamedParameterJdbcTemplate namedParameterJdbcTemplate() throws Exception{
System.out.println(driverClass+" "+ url+" "+username+" "+password);
DriverManagerDataSource source = new DriverManagerDataSource();
source.setDriverClassName(driverClass);
source.setUrl(url);
source.setUsername(username);
source.setPassword(password);
NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(source);
return namedParameterJdbcTemplate;
}
}
Run Code Online (Sandbox Code Playgroud)
这是application.properties
@Configuration
@PropertySource("classpath:application.properties")
public class Datasource {
@Value("${db.driverClassName}")
private String driverClass;
@Value("${db.url}")
private String url;
@Value("${db.username}")
private String username;
@Value("${db.password}")
private String password;
@Bean
public NamedParameterJdbcTemplate namedParameterJdbcTemplate() throws Exception{
System.out.println(driverClass+" "+ url+" "+username+" "+password);
DriverManagerDataSource source = new DriverManagerDataSource();
source.setDriverClassName(driverClass);
source.setUrl(url);
source.setUsername(username);
source.setPassword(password);
NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(source);
return namedParameterJdbcTemplate;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 9
对我来说,问题在于 postgresSql 的拼写错误
它唯一的一个,
代替
与
spring.datasource.url=jdbc: postgresql ://localhost:5432/databaseName
还要在休眠方言上检查同样的事情,
PostgresSQLDialect
用 。。。来代替PostgreSQLDialect
该问题已通过创建两个 bean 解决。为 DataSource 和 NamedParameterJdbcTemplate 创建单独的 bean。
@Bean
public DataSource dataSource(){
System.out.println(driverClass+" "+ url+" "+username+" "+password);
DriverManagerDataSource source = new DriverManagerDataSource();
source.setDriverClassName(driverClass);
source.setUrl(url);
source.setUsername(username);
source.setPassword(password);
return source;
}
@Bean
public NamedParameterJdbcTemplate namedParameterJdbcTemplate(){
NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(this.dataSource());
return namedParameterJdbcTemplate;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19118 次 |
| 最近记录: |