spring boot data jpa mysql无法创建数据库
Posted
技术标签:
【中文标题】spring boot data jpa mysql无法创建数据库【英文标题】:spring boot data jpa mysql could not create database 【发布时间】:2016-10-11 23:55:42 【问题描述】:我是春天的新人 我将发布我的代码, 那个application.properties
spring.datasource.url=jdbc:mysql://localhost/spring
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=update
这是我的实体
package model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Person
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
private String phone;
private String adresse;
public long getId()
return id;
public void setId(long id)
this.id = id;
public String getName()
return name;
public void setName(String name)
this.name = name;
public String getPhone()
return phone;
public void setPhone(String phone)
this.phone = phone;
public String getAdresse()
return adresse;
public void setAdresse(String adresse)
this.adresse = adresse;
public Person(long id, String name, String phone, String adresse)
super();
this.id = id;
this.name = name;
this.phone = phone;
this.adresse = adresse;
public Person()
super();
这是存储库
package repositry;
import org.springframework.data.jpa.repository.JpaRepository;
import model.Person;
public interface PersonRespositry extends JpaRepository<Person, Long>
和我的控制器
package contoller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import model.Person;
import repositry.PersonRespositry;
@RestController
public class PersonController
PersonRespositry rp;
@Autowired
public PersonController(PersonRespositry rp)
// TODO Auto-generated constructor stub
this.rp=rp;
@RequestMapping("/find")
public Person find(long id)
return rp.findOne(id);
@RequestMapping("/findall")
public List<Person> findall()
return rp.findAll();
@RequestMapping(value="/hello")
public String Demo()
return "Hello world !!";
@RequestMapping(value="/create", method=RequestMethod.GET)
public String create()
Person p=new Person();
p.setName("med");
p.setPhone("233888");
p.setAdresse("rue ");
rp.save(p);
return " success";
这是项目的架构师:
当我运行应用程序时,数据库不会生成,只有 localhost:8080 正在运行。
【问题讨论】:
您必须先手动创建架构(数据库)。然后运行应用程序,应用程序应该在模式中创建表 我创建了架构 【参考方案1】:您的问题是Application.java
的位置。
@ComponentScan
在带有注释的类的包中(@SpringBootApplication
包含@ComponentScan
)和该包的子包中查找 Spring bean。
我已经提供了一个非常相似的设置示例。
请看这里:https://***.com/a/27983870/2576531
此外,Robert Moskal 的暗示是正确的。数据库本身必须已经存在。只有表会自动创建。
【讨论】:
【参考方案2】:如果您想创建数据,您需要使用 create 或 create-drop。如果您使用的是 mysql 之类的东西,则至少需要创建数据库。将为您创建表格。
我在针对生产数据库实例执行此操作时非常小心。
【讨论】:
确保 spring.jpa.hibernate.ddl-auto=update 它是创建或更新数据库【参考方案3】:否则对我来说它不适用于@componentScan,但它现在可以在主类中使用@EntityScan(basePackages = "com.jwt.entites" ) 来扫描实体类..
【讨论】:
【参考方案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 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.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wisdom.spring.myjdbc</groupId>
<artifactId>spring_boot_jpa</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring_boot_jpa</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>
</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>
<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>
//application Config
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost/wisdom
spring.datasource.username=root
spring.datasource.password=8855
spring.jpa.generate-ddl=true
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
server.port = 8585
package com.wisdom.spring.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wisdom.spring.model.students;
import com.wisdom.spring.repo.students_repo;
@RestController
@RequestMapping("/students")
public class studentController
@Autowired
students_repo STDRepo;
@GetMapping(value = "/a")
public String a()
return "hello";
@GetMapping(value = "/save")
public List<students> getStudents()
System.out.println("Data returned");
return STDRepo.findAll();
@PostMapping(value = "/savestd")
public void saveBurger(students student)
STDRepo.save(student);
package com.wisdom.spring.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class students
@Id
@Column
private int s_id;
@Column
private String name;
@Column
private String address;
public students()
public students(int s_id, String name, String address)
this.s_id = s_id;
this.name = name;
this.address = address;
public int getS_id()
return s_id;
public void setS_id(int s_id)
this.s_id = s_id;
public String getName()
return name;
public void setName(String name)
this.name = name;
public String getAddress()
return address;
public void setAddress(String address)
this.address = address;
package com.wisdom.spring.repo;
import org.springframework.data.jpa.repository.JpaRepository;
import com.wisdom.spring.model.students;
public interface students_repo extends JpaRepository<students,String>
package com.wisdom.spring;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootJpaApplication
public static void main(String[] args)
SpringApplication.run(SpringBootJpaApplication.class, args);
【讨论】:
以上是关于spring boot data jpa mysql无法创建数据库的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot:在Spring Boot中使用Mysql和JPA
Spring Boot(17)——使用Spring Data JPA
[Spring Boot] Adding JPA and Spring Data JPA