已经配置的JdbcConfiguration代码如下

@Configuration
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfiguration {

@Autowired
private JdbcProperties jdbcProperties;

@Bean
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(jdbcProperties.getUrl());
dataSource.setDriverClassName(jdbcProperties.getDriverClassName());
dataSource.setUsername(jdbcProperties.getUsername());
dataSource.setPassword(jdbcProperties.getPassword());
return dataSource;
}

}

使用通用mapper引入的依赖

<!--jdbc的启动器,默认使用HikariCP连接池-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--不要忘记数据库驱动,因为springboot不知道我们使用的什么数据库,这里选择mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

<!-- 通用mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>

 

 

在application.properties中


spring.datasource.url=jdbc:mysql://localhost:3306/heima
spring.datasource.username=root
spring.datasource.password=root

 

 

由于用@Configuration将JdbcConfiguration放入容器中,但是又没有配置文件,而通用mapper会优先利用容器中的dataSource等其他对象,这些对象是创建不出来的, 不会使用

启动器中的而对象, 因此出现空指针异常

相关文章:

  • 2021-08-15
  • 2022-01-01
  • 2021-11-23
  • 2021-06-11
  • 2021-09-07
  • 2022-01-28
  • 2021-07-11
猜你喜欢
  • 2022-01-13
  • 2021-12-11
  • 2022-12-23
  • 2022-12-23
  • 2022-01-15
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案