如何使用SpringBoot使用Spring Data JPA和MYSQas DB创建一个简单的CRUD应用程序?

时间:2017-01-29 12:19:54

标签: java spring jpa spring-boot

我尝试使用Spring Boot V1.4.4创建一个简单的虚拟应用程序。 以下是我pom.xml的样子:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.bootstrap</groupId>
<artifactId>bootstrap</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>tsqln</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

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

<dependencies>
    <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>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>
    </dependency>
</dependencies>

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

我的应用程序类看起来像:

package com.bootstrap;

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

@SpringBootApplication
public class TsqlnApplication {

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

我的application.properties看起来像:

spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/lostandfound
spring.datasource.username=root
spring.datasource.password=root

但是当我运行应用程序时,我收到以下错误说明:

*****************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration required a bean of type 'javax.sql.DataSource' that could not be found.
    - Bean method 'dataSource' not loaded because @ConditionalOnClass did not find required class 'org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType'
    - Bean method 'dataSource' not loaded because @ConditionalOnClass did not find required class 'org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType'
Action:
Consider revisiting the conditions above or defining a bean of type 'javax.sql.DataSource' in your configuration.
2017-01-29 17:32:44.645 ERROR 4316 --- [           main] o.s.test.context.TestContextManager      : Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@516be40f] to prepare test instance [com.bootstrap.TsqlnApplicationTests@9573b3b]**

我的项目目录结构是:

enter image description here

然而,采用相同的方法,上述应用程序适用于SpringBoot 1.3版。有关如何使其与1.4.4版一起使用的任何建议吗?

3 个答案:

答案 0 :(得分:1)

确保你的mysql-connector实际上在运行时的类路径中(如果你使用的是Tomcat,那么它应该在lib文件夹中)。

如果没有,请在pom.xml中添加编译依赖项

<dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <version>5.1.36</version>
</dependency>

您还忘记了application.properties中的这一行:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

答案 1 :(得分:1)

你的application.properties中的

添加了这行

{{1}}

答案 2 :(得分:0)

您可以通过创建自己的数据源bean来恢复该行为:

@Bean
public DataSource dataSource() {
    return DataSourceBuilder
        .create()
        .username("")
        .password("")
        .url("")
        .driverClassName("")
        .build();
}