gpt4 book ai didi

java - 无法让spring boot在gradle项目中自动创建数据库模式

转载 作者:行者123 更新时间:2023-12-02 08:57:15 25 4
gpt4 key购买 nike

我已在 application.yml 文件中完成与所有数据库相关的设置,但日志中看不到数据库创建消息,并且在调用实际 api 时会生成与数据库表相关的错误。

application.yml

server:
port: 8080
spring:
datasource:
drive-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/sys?serverTimezone=UTC&characterEncoding=UTF-8
username: root
password: ####
jpa:
database: mysql
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
general-ddl: true
show-sql: true
hibernate:
format_sql: true
ddl-auto: create
security:
oauth:
authorization:
check-token-access: isAuthenticated()
oauth2:
jwt:
signkey: 123@#$

Application.java:

@EnableAutoConfiguration
@SpringBootApplication
@EnableConfigurationProperties
@EntityScan(basePackages = {"com.springboard.backend"})
public class Application {

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

}

Users.java

@Getter
@Setter
@Entity
@ToString
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(of = "uid")
public class Users implements UserDetails {


@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(unique = true)
private Integer id;

@Column
private String username;

@Column
private String phonenumber;

@Column
private String address;

@Column
private String address2;

public boolean isAddress(String address) {
return address == "우리집";
}

@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="user_id")
@ElementCollection(fetch = FetchType.EAGER)
private List<UserRole> userRoles = new ArrayList<>();

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return this.userRoles.stream().map( role -> new SimpleGrantedAuthority(role.toString())).collect(Collectors.toList());
}

@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
@Override
public String getUsername() {
return this.username;
}

@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
@Override
public boolean isAccountNonExpired() {
return true;
}

@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
@Override
public boolean isAccountNonLocked() {
return true;
}

@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
@Override
public boolean isCredentialsNonExpired() {
return true;
}

@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
@Override
public boolean isEnabled() {
return true;
}

@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
@Override
public String getPassword() {
return this.phonenumber;
}

}

UserRole.java

@Data
@Entity
public class UserRole {

public enum Role {
ADMIN, USER
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column
@Enumerated(EnumType.STRING)
private Role rolename;

public UserRole() {

}

public UserRole(Role rolename) {
this.rolename = rolename;
}
}

最佳答案

您的配置似乎不正确。确保 jpa 位于 spring 下:

spring:
jpa:
database: mysql
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
general-ddl: true
show-sql: true
hibernate:
format_sql: true
ddl-auto: create

关于java - 无法让spring boot在gradle项目中自动创建数据库模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60417844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com