首页 > 解决方案 > 应用程序无法启动 :::“无法配置数据源:未指定 'url' 属性,并且无法配置嵌入式数据源。”

问题描述

我对 Spring Boot 还很陌生,并且正在从事一个项目,但是在我的控制台中没有指定 URL 属性的错误。

Application.properties 文件

spring.datasourse.url=jdbc:mysql://localhost:3306/employee_directory?useSSL=false&serverTimeZone=UTC
spring.datasourse.username=springstudent    
spring.datasourse.password=springstudent
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = update

我的 pom.xml 文件(~使用 spring.io 生成)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.luv2code.springboot</groupId>
    <artifactId>cruddemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cruddemo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
<!--  -->       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

我知道我的错误似乎在应用程序属性文件中,但我检查了这些文件,但似乎找不到任何错误。

我的应用

package com.luv2code.springboot.cruddemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CruddemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(CruddemoApplication.class, args);
    }
}

我的员工DAOImpl

package com.luv2code.springboot.cruddemo.dao;

import java.util.List;

import javax.persistence.EntityManager;

import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


import com.luv2code.springboot.cruddemo.entity.Employee;

@Repository
public class EmployeeDAOHibernateImpl implements EmployeeDAO {
    
    private EntityManager entityManager;
    
    @Autowired
    public EmployeeDAOHibernateImpl(EntityManager theEntityManager) {
        entityManager = theEntityManager;
    }
    
    @Override
    public List<Employee> findAll() {
        
        Session currentSession = entityManager.unwrap(Session.class);
        
        Query<Employee> theQuery = currentSession.createQuery("from Employee", Employee.class);
        
        List<Employee> employees = theQuery.getResultList();
        
        return employees;
    }

    @Override
    public Employee findById(int theId) {
        
        Session currentSession = entityManager.unwrap(Session.class);
        
        Employee theEmployee = currentSession.get(Employee.class, theId);
        
        return theEmployee;
    }

    @Override
    public void save(Employee theEmployee) {
        
        Session currentSession = entityManager.unwrap(Session.class);
        
        currentSession.saveOrUpdate(theEmployee);
        
    }

    @Override
    public void delete(int theId) {
        
        Session currentSession = entityManager.unwrap(Session.class);
        
        Query theQuery = currentSession.createQuery("delete from Employee where id=:employeeId");
        
        theQuery.setParameter("employeeId", theId);

        theQuery.executeUpdate();
    }

}

我的员工道

package com.luv2code.springboot.cruddemo.dao;

import java.util.List;

import com.luv2code.springboot.cruddemo.entity.Employee;

public interface EmployeeDAO {

    public List<Employee> findAll();
    public Employee findById(int theId);
    public void save(Employee theEmployee);
    public void delete(int theId);
}

我的错误屏幕


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.3.RELEASE)

2020-08-24 14:03:56.750  INFO 11608 --- [  restartedMain] c.l.s.cruddemo.CruddemoApplication       : Starting CruddemoApplication on DESKTOP-AR3MS4U with PID 11608 (D:\Eclipse && STS\eclipse\cruddemo\target\classes started by Abhijay in D:\Eclipse && STS\eclipse\cruddemo)
2020-08-24 14:03:56.763  INFO 11608 --- [  restartedMain] c.l.s.cruddemo.CruddemoApplication       : No active profile set, falling back to default profiles: default
2020-08-24 14:03:56.990  INFO 11608 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2020-08-24 14:03:56.991  INFO 11608 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2020-08-24 14:04:00.471  INFO 11608 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-08-24 14:04:00.493  INFO 11608 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-08-24 14:04:00.494  INFO 11608 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-08-24 14:04:00.957  INFO 11608 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-08-24 14:04:00.957  INFO 11608 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3965 ms
2020-08-24 14:04:01.323  WARN 11608 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeDAOHibernateImpl' defined in file [D:\Eclipse && STS\eclipse\cruddemo\target\classes\com\luv2code\springboot\cruddemo\dao\EmployeeDAOHibernateImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManager' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2020-08-24 14:04:01.331  INFO 11608 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-08-24 14:04:01.380  INFO 11608 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-08-24 14:04:02.111 ERROR 11608 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.luv2code.springboot.cruddemo.dao.EmployeeDAOHibernateImpl required a bean of type 'javax.persistence.EntityManager' that could not be found.


Action:

Consider defining a bean of type 'javax.persistence.EntityManager' in your configuration.


标签: mysqlspring-boothibernateurl

解决方案


推荐阅读