簡體   English   中英

JdbcTemplate在Springboot應用中是null使用Oracle 10g

[英]JdbcTemplate is null in Springboot application using Oracle 10g

使用 spring-boot 2.7.2 錯誤 - java.lang.NullPointerException:無法調用“org.springframework.jdbc.core.JdbcTemplate.query(String, org.springframework.jdbc.core.RowMapper)” 因為“this is”.8185Template14


@Service
public class UserServiceImpl implements UserService {
.....
    @Autowired
    private UserDao userDao;

}

@Component
public class UserDaoImpl implements UserDao {
    
    private JdbcTemplate jdbcTemplate;

    @Autowired
    public UserDaoImpl(JdbcTemplate jdbcTemplatn) {
        this.jdbcTemplate = jdbcTemplate;
    }
    
    @Override
    public  List<AttributeData> getAttributes() {
        String query = "select attributeid, ....";
        List<AttributeData> attributes = jdbcTemplate.query(query, new AttributeMapper());
        return attributes;
    }
}

application.properties :
# Oracle db related
spring.datasource.url=jdbc:oracle:thin:@localhost....
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

# JPA related
oracle.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none

只是意味着您沒有定義 JdbcTemplate 的@Bean ,例如

@Bean
public JdbcTemplate jdbcTemplate() throws SQLException {
    final JdbcTemplate template = new JdbcTemplate();
    template.setDataSource(dataSource());
    template.afterPropertiesSet();
    return template;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM