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:
- You excluded
DataSourceAutoConfiguration.classin your startup class, which disables Spring Boot's automatic data source configuration. - You manually created a
LocalSessionFactoryBeanbut didn't configure a data source for it—so Hibernate has no way to connect to your MySQL database. - You're using Spring Data JPA (with
CrudRepository), but your manualLocalSessionFactoryBean(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, andyour_mysql_passwordwith your actual MySQL details. ddl-auto=updatetells Hibernate to automatically create/update database tables based on yourUserentity.
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

