简体   繁体   English

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

[英]How to create a simple CRUD application using Spring Data JPA and MYSQas DB using SpringBoot?

I'm trying to create a simple dummy application using Spring Boot Version 1.4.4. 我正在尝试使用Spring Boot版本1.4.4创建一个简单的虚拟应用程序。 Here is how my pom.xml looks like : 这是我的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>

My application class looks like: 我的应用程序类如下:

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);
    }
}

And my application.properties looks like : 我的application.properties看起来像:

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

But when I run the application I get the following error stating : 但是,当我运行该应用程序时,出现以下错误说明:

*****************************
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]**

My directory structure for project is : 我的项目目录结构是:

在此处输入图片说明

However with the same approach the above application works with SpringBoot version 1.3. 但是,使用相同的方法,上述应用程序可用于SpringBoot 1.3版。 Any suggestions how to make it work with version 1.4.4? 关于如何使其与1.4.4版兼容的任何建议?

Make sure your mysql-connector is actually there in the classpath at runtime (if your using Tomcat, then it should be in the lib folder). 确保您的mysql-connector实际上在运行时位于类路径中(如果使用的是Tomcat,则它应位于lib文件夹中)。

If you do not have it there add a compile dependency in your pom.xml 如果没有,则在pom.xml中添加一个编译依赖项

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

Also you forgot about this line in your application.properties : 您也忘记了application.properties这一行:

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

in your application.properties add this lines 在您的application.properties中添加以下行

spring.datasource.url = jdbc:mysql://localhost:3306/yourdatabase
# Username and password
spring.datasource.username = root
spring.datasource.password = 
# Show or not log for each sql query
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = create
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

You can restore that behaviour by creating your own datasource bean: 您可以通过创建自己的数据源bean来恢复该行为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM