使用Oracle 10g的Springboot应用程序中的JdbcTemplate为空

iswrvxsc  于 2023-01-08  发布在  Oracle
关注(0)|答案(1)|浏览(161)

使用spring-boot 2.7.2错误- *java.lang.空指针异常:无法调用“组织.Spring框架.jdbc.核心.jdbcTemplate.查询(字符串,组织.Spring框架.jdbc.核心.行Map器)”,因为“此.jdbcTemplate”为空 *

@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
uujelgoq

uujelgoq1#

只是意味着您没有定义JdbcTemplate的@Bean,例如

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

相关问题