无法执行目标org.springframework.boot:spring-boot-maven-plugin:1.3.3.RELEASE:run

时间:2016-04-12 02:26:46

标签: java eclipse spring hibernate maven

我是java spring和hibernate的新手。我使用maven来管理我的项目,我无法理解我有什么问题。这很奇怪。我在这里呆了一个星期。 这是完整错误

 String update = "INSERT INTO help("name","area","date","message") VALUES(?, ?, ?, ?)";
    statement.setString(1, name);
    statement.setString(2, area);
    statement.setString(3, date);
    statement.setString(4, message);

这是我的项目树: project tree Config.java文件:

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.3.3.RELEASE:run (default-cli) on project demo: An exception occurred while running. null: InvocationTargetException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active). -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

的pom.xml:

package com.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.dao.DepartmentDAO;
import com.daoimpl.DepartmentDAOImpl;


@Configuration
public class Config {

    @Bean
    public DepartmentDAO departmentDAO(){
    //      DepartmentDAOImpl departmentDaoImpl = new DepartmentDAOImpl();
            return new DepartmentDAOImpl();
    }
}

    http://maven.apache.org/xsd/maven-4.0.0.xsd">     4.0.0

<?xml version="1.0" encoding="UTF-8"?>

MainController.java

<groupId>test.component</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>DemoHibernate</name>
<description>Demo project for Spring Boot</description>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.6.3.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.1</version>
    </dependency>
            <dependency>
         <groupId>org.eclipse.persistence</groupId>
         <artifactId>eclipselink</artifactId>
         <version>2.5.1</version>
    </dependency>
    <dependency>
         <groupId>org.eclipse.persistence</groupId>
         <artifactId>javax.persistence</artifactId>
         <version>2.0.0</version>
         <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.36</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
     <dependency>
         <groupId>com.h2database</groupId>
         <artifactId>h2</artifactId>
         <version>1.3.156</version>
     </dependency>

     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hsqldb</groupId>
       <artifactId>hsqldb</artifactId>
       <scope>runtime</scope>
    </dependency>
</dependencies>

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

DepartmentDAO.java

package com.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dao.DepartmentDAO;
import com.entities.Department;

//@Controller
@RestController
public class MainController {
    @Autowired
    private DepartmentDAO departmentDAO;

    @RequestMapping({"/","/home","/index"})
    public String home(){
    return "index";
}

@RequestMapping({"/deptList"})
 public String deptList() {
     departmentDAO.createDepartment("Dept Name", "Dept Location");

     List<Department> list = departmentDAO.listDepartment();
     for (Department dept : list) {
         System.out.println("Dept No " + dept.getDeptNo());
     }
     //      model.addAttribute("departments", list);
     return "deptList";
 }
}

DepartmentDAOImpl.java

package com.dao;

import java.util.List;
//import org.springframework.stereotype.Component;
//import org.springframework.stereotype.Repository;

import com.entities.*;

//@Repository
//@Component
public interface DepartmentDAO {
    public List<Department> listDepartment() ;
    public Integer getMaxDeptId();
    public void createDepartment(String name,String location);  
}

Department.java

package com.daoimpl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import com.dao.DepartmentDAO;
import com.entities.Department;


@Transactional
public class DepartmentDAOImpl implements DepartmentDAO{

private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}

@SuppressWarnings("unchecked")
@Override
public List<Department> listDepartment() {
    Session session = this.sessionFactory.getCurrentSession();
    List<Department> list = session.createQuery("from Department").list();
    return list;
}

@Override
public Integer getMaxDeptId() {
    Session session = this.sessionFactory.getCurrentSession();
    String sql = "Select max(d.deptId) from Department d";
    Query query = session.createQuery(sql);
    Integer maxDeptId = (Integer)query.uniqueResult();
    if (maxDeptId == null){
        return 0;
    }
    return maxDeptId;
}

@Override
public void createDepartment(String name, String location) {
    Integer deptId = getMaxDeptId() + 1;
    Department dept = new Department();
    dept.setDeptId(deptId);
    dept.setDeptNo("D" + deptId);
    dept.setDeptName(name);
    dept.setLocation(location);
    Session session = this.sessionFactory.getCurrentSession();
    session.persist(dept);
}

}

Department.hbm.xml

package com.entities;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;


@Entity
@Table(name = "DEPARTMENT", uniqueConstraints = {
@UniqueConstraint(columnNames = { "DEPT_NO" }) })
public class Department {
private int deptId;
private String deptNo;
private String deptName;
private String location;

public Department(){

}

public Department(Integer deptId, String deptName, String location){ 
    this.deptId = deptId;
    this.deptNo = "D" + this.deptId;
    this.deptName = deptName;
    this.location = location;
}

@Id
@Column(name = "DEPT_ID")
public int getDeptId() {
    return deptId;
}

public void setDeptId(int deptId) {
    this.deptId = deptId;
}

@Column(name = "DEPT_NO", length = 20, nullable = false)
public String getDeptNo() {
    return deptNo;
}

public void setDeptNo(String deptNo) {
    this.deptNo = deptNo;
}

@Column(name = "DEPT_NAME", nullable = false)
public String getDeptName() {
    return deptName;
}

public void setDeptName(String deptName) {
    this.deptName = deptName;
}

@Column(name = "LOCATION")
public String getLocation() {
    return location;
}

public void setLocation(String location) {
    this.location = location;
}   


}

的hibernate.cfg.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 12, 2016 12:33:11 AM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="com.entities.Department" table="DEPARTMENT">
    <id name="deptId" type="int">
        <column name="DEPTID" />
        <generator class="assigned" />
    </id>
    <property name="deptNo" type="java.lang.String">
        <column name="DEPTNO" />
    </property>
    <property name="deptName" type="java.lang.String">
        <column name="DEPTNAME" />
    </property>
    <property name="location" type="java.lang.String">
        <column name="LOCATION" />
    </property>
</class>
</hibernate-mapping>

很抱歉,如果这是一个愚蠢的问题,我是一个新手,我无法解决它一个星期。感谢您的支持。 :)

2 个答案:

答案 0 :(得分:0)

该错误表明无法确定数据库驱动程序。

  

无法确定数据库类型为NONE的嵌入式数据库驱动程序类。

尝试显式设置连接URL:

spring.datasource.url=jdbc:mysql://localhost:3306/testdkt

我在https://www.journaldev.com/13830/spring-boot-cannot-determine-embedded-database-driver-class-for-database-type-none

找到了

希望这会有所帮助

答案 1 :(得分:-1)

即使我也遇到过同样的问题,但是一旦我将以下类包含在src / main / java包中,我就可以继续。

<?php
require 'varDx.php';
$dx = new \varDx\cDX; //create an object
$dx->def('file.dat'); //define data file

$val1 = "this is a string";
$dx->write('data1', $val1); //writes key to file
echo $dx->read('data1'); //returns key value from file