[英]JdbcTemplate is null in Springboot application using Oracle 10g
Using spring-boot 2.7.2 Error - java.lang.NullPointerException: Cannot invoke "org.springframework.jdbc.core.JdbcTemplate.query(String, org.springframework.jdbc.core.RowMapper)" because "this.jdbcTemplate" is null使用 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
Just means that you don't have a @Bean
defining the JdbcTemplate, eg只是意味着您没有定义 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.