Spring Boot通过实体类自动创建表时出现创建不上的情况

         Spring Boot通过实体类自动创建表时出现创建不上的情况 


  原因1:

        启动类和实体类的包命名问题,但是在2.0版本以上的初始化中,一般根目录和与启动类类名相同的包下会有一个启动类(也就是两个启动类)

  解决方案1:

           启动类的包名是com.lihao(根)

          实体类包名的命名必须是com.lihao.domain或者com.lihao.entity,这样就可以了

    

    原因2:

        mysql的可视化工具的问题,例如Navicat、SQLyog的问题

    

    解决方案2:

        现在cmd命令行中创建同名的表,判断是否是这个原因,然后 重启mysql的工具 即可

本人代码示例:

 1、application.yml代码:

spring:
  thymeleaf:
    mode: HTML

  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/spring_boot?useUnicode=true&characterEncoding=utf-8
    username: root
    password: 123456
  jpa:
    hibernate:
      ddl-auto: create
    show-sql: true
logging:
  level:
     root: info
     com.atlihao: debug
  file: log/my.log

2、User类的代码:

    

package com.lihao.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String username;
    private String password;
    private int phone;
    private String email;

    public User() {
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getPhone() {
        return phone;
    }

    public void setPhone(int phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", phone=" + phone +
                ", email='" + email + '\'' +
                '}';
    }
}

成功结果:

  只要run的过程中能出现类似以下的的代码,即表示为成功!

Hibernate: drop table if exists hibernate_sequence
Hibernate: drop table if exists user
Hibernate: create table hibernate_sequence (next_val bigint) engine=MyISAM
Hibernate: insert into hibernate_sequence values ( 1 )

Hibernate: create table user (id bigint not null, email varchar(255), password varchar(255), phone integer not null, username varchar(255), primary key (id)) engine=MyISAM


你可能感兴趣的:(SpringBoot)