You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot新手遇java.lang.UnsupportedOperationException:应用程序必须提供JDBC连接问题求助

Hey there! Let's work through that JDBC connection error you're hitting in your Spring Boot app. I can spot a couple of key issues in your setup that are causing this problem.

What's Causing the Error?

The "java.lang.UnsupportedOperationException: The application must supply JDBC connection" error happens because:

  1. You excluded DataSourceAutoConfiguration.class in your startup class, which disables Spring Boot's automatic data source configuration.
  2. You manually created a LocalSessionFactoryBean but didn't configure a data source for it—so Hibernate has no way to connect to your MySQL database.
  3. You're using Spring Data JPA (with CrudRepository), but your manual LocalSessionFactoryBean (a Hibernate-native component) conflicts with Spring's JPA auto-configuration.

Fix Steps to Resolve the Issue

1. Clean Up Your Startup Class

Remove the unnecessary exclusions and manual bean setup. Spring Boot handles JPA and data sources automatically for you:

@SpringBootApplication
@EnableJpaRepositories("com.example.spring.repository")
@EntityScan("com.example.spring.model")
public class ReqAppApplication {
    public static void main(String[] args) {
        SpringApplication.run(ReqAppApplication.class, args);
    }
}
  • We removed exclude={DataSourceAutoConfiguration.class} to let Spring Boot configure your data source.
  • We deleted the sessionFactory() bean—Spring Data JPA will automatically create the correct EntityManagerFactory for you.

2. Verify Your application.properties Config

Make sure your MySQL connection details are correctly set (here's a standard template you can use if your current config is off):

# MySQL Data Source Config
spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name?useSSL=false&serverTimezone=UTC
spring.datasource.username=your_mysql_username
spring.datasource.password=your_mysql_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# JPA/Hibernate Config
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
  • Replace your_database_name, your_mysql_username, and your_mysql_password with your actual MySQL details.
  • ddl-auto=update tells Hibernate to automatically create/update database tables based on your User entity.

3. Check Your Dependencies

Ensure these critical dependencies are in your pom.xml (if using Maven) or build.gradle (if using Gradle):

<!-- Spring Data JPA -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<!-- MySQL Driver -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

<!-- Lombok (for @Getter in your User entity) -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

4. Test the App

Start your application again. You should be able to access /users/sample and /users/sample2 without errors, and Hibernate will automatically create the user table in your MySQL database.

内容的提问来源于stack exchange,提问作者WillUpdate

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 09:03:13