【问题标题】:JDBC Template issues Nullpointer ExceptionJDBCTemplate 问题 Nullpointerexception
【发布时间】:2021-09-03 02:34:21
【问题描述】:

我正在尝试在我的 Spring Boot 项目中使用 JDBCTemplate 连接到 postgresql 数据库,但是当我尝试进行查询时遇到空指针异常

应用程序属性

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://url/test
spring.datasource.username=postgres
spring.datasource.password=pass

Pom.xml

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>               
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
    </dependencies>

代码

public class TestDaoImpl implements TestDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public void testconnection() throws Exception {
        // TODO Auto-generated method stub
  
        int x=jdbcTemplate
        .queryForObject("select count(*) from table", Integer.class);
        System.out.println(x);
        
    
    }

我无法检测出问题所在...请您帮帮我...提前致谢!

【问题讨论】:

    标签: spring spring-boot jdbctemplate


    【解决方案1】:
    @Bean
    public JdbcTemplate jdbcTemplate(Datasource datasource){
        JdbcTemplate jdbcTemplate = new JdbcTemplate(datasource);
        return jdbcTemplate;
    }
    

    您可以使用数据源配置 jdbcTemplate,然后您可以自动装配它。

    你可以把代码块放到配置类中

    【讨论】:

    • according to this Spring Boot 自动为我们配置数据源
    • 是的,自动配置了数据源,但是没有配置jdbctemplate。
    • 我已将此代码块添加到配置类,但它导致了这个.. ``` 否定匹配:----------------- JdbcTemplateConfiguration : 不匹配: - @ConditionalOnMissingBean (types: org.springframework.jdbc.core.JdbcOperations; SearchStrategy: all) 找到类型为 'org.springframework.jdbc.core.JdbcOperations' jdbcTemplate (OnBeanCondition) ``` 我没有'不明白的是大部分教程都没有配置,也没有datasource,也没有jdbctemplate。
    猜你喜欢
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    • 2016-09-07
    • 1970-01-01
    • 1970-01-01
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多