java - 在 Spring Boot 中 - 应用程序数据库未创建

标签 java mysql spring spring-boot

我正在创建一个 Spring Boot 应用程序。当我运行应用程序时,数据库表没有创建。

这是我的application.properties 文件

spring.datasource.url=jdbc:mysql://localhost/aymuws
spring.datasource.username=root
spring.datasource.password=1101289217

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop

User 存储库类

package com.technostack.aymuws.entities;

@Entity
@Table(name="user")
public class User {

    @Id
    @Email
    @NotEmpty
    @Column(unique=true)
    private String email;

    @Column(name="_name",unique=false,nullable=false,length=100)
    @NotEmpty
    private String name;

    @Column(name="_password",unique=true,nullable=false)
    @NotEmpty
    @Size(max=12,min=6)
    private String password;

    @Column(name="_task")
    @OneToMany(mappedBy="user",cascade=CascadeType.ALL)
    private List<Task> tasks;

    @Column(name="_roles")
    @ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(name="USER_ROLES",joinColumns= {
            @JoinColumn(name="USER_EMAIL",referencedColumnName="email")
    },inverseJoinColumns= {
            @JoinColumn(name="ROLE_NAME",referencedColumnName="name")
    })
    private List<Roles> roles;

    public String getEmail() {
        return email;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

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

    public List<Task> getTasks() {
        return tasks;
    }

    public void setTasks(List<Task> tasks) {
        this.tasks = tasks;
    }

    public List<Roles> getRoles() {
        return roles;
    }

    public void setRoles(List<Roles> roles) {
        this.roles = roles;
    }

    public User(String email, String name, String password) {
        this.email = email;
        this.name = name;
        this.password = password;
    }

    public User() {
    }
}

Task 存储库类

package com.technostack.aymuws.entities;

@Entity
public class Task {

    @Id
    @GeneratedValue
    private Long task_id;
    @NotEmpty
    private String date;
    @NotEmpty
    private String starttime;
    @NotEmpty
    private String stoptime;
    @NotEmpty
    private String description;
    @ManyToOne
    @JoinColumn(name="USER_EMAIL",referencedColumnName="email")
    private User user;

    // Constructors, setters & getters
}

以及Role存储库类

package com.technostack.aymuws.entities;

@Entity
public class Roles {

    @Id
    private String name;
    @ManyToMany(mappedBy="roles")
    private List<User> users;

    // Constructor, getters and setters
}

服务类如下。

package com.technostack.aymuws.service;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public void createUser(User user) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        user.setPassword(encoder.encode(user.getPassword()));
        Roles user_role = new Roles("USER");
        List<Roles> create_role = new ArrayList<>();
        create_role.add(user_role);
        user.setRoles(create_role);
        userRepository.save(user);
    }

    public void createAdmin(User user) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        user.setPassword(encoder.encode(user.getPassword()));
        Roles user_role = new Roles("ADMIN");
        List<Roles> create_role = new ArrayList<>();
        create_role.add(user_role);
        user.setRoles(create_role);
        userRepository.save(user);
    }

    public User findone(String email) {
        return userRepository.findOne(email);
    }
}

这是我运行应用程序时的控制台。

00:33:34.875 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Included patterns for restart : []
00:33:34.881 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Excluded patterns for restart : [/spring-boot-starter/target/classes/, /spring-boot-autoconfigure/target/classes/, /spring-boot-starter-[\w-]+/, /spring-boot/target/classes/, /spring-boot-actuator/target/classes/, /spring-boot-devtools/target/classes/]
00:33:34.882 [main] DEBUG org.springframework.boot.devtools.restart.ChangeableUrls - Matching URLs for reloading : [file:/D:/STS%204%20Project%20Workspace/Spring/AymuwsManagementSystem/target/classes/]

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v1.5.20.RELEASE)

2019-04-22 00:33:35.898  INFO 6448 --- [  restartedMain] .t.a.i.AymuwsManagementSystemApplication : Starting AymuwsManagementSystemApplication on ADMIN-PC with PID 6448 (started by ADMIN in D:\STS 4 Project Workspace\Spring\AymuwsManagementSystem)
2019-04-22 00:33:35.901  INFO 6448 --- [  restartedMain] .t.a.i.AymuwsManagementSystemApplication : No active profile set, falling back to default profiles: default
2019-04-22 00:33:36.877  INFO 6448 --- [  restartedMain] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@143eb1f4: startup date [Mon Apr 22 00:33:36 IST 2019]; root of context hierarchy
2019-04-22 00:33:41.963  INFO 6448 --- [  restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$f40d0ceb] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-04-22 00:33:43.478  INFO 6448 --- [  restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2019-04-22 00:33:43.619  INFO 6448 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-04-22 00:33:43.620  INFO 6448 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.39
2019-04-22 00:33:43.986  INFO 6448 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-04-22 00:33:43.987  INFO 6448 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 7119 ms
2019-04-22 00:33:44.552  INFO 6448 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2019-04-22 00:33:44.553  INFO 6448 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-04-22 00:33:44.554  INFO 6448 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2019-04-22 00:33:44.554  INFO 6448 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2019-04-22 00:33:44.556  INFO 6448 --- [ost-startStop-1] .s.DelegatingFilterProxyRegistrationBean : Mapping filter: 'springSecurityFilterChain' to: [/*]
2019-04-22 00:33:44.557  INFO 6448 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
Mon Apr 22 00:33:45 IST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Mon Apr 22 00:33:47 IST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Mon Apr 22 00:33:47 IST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Mon Apr 22 00:33:47 IST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Mon Apr 22 00:33:47 IST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Mon Apr 22 00:33:47 IST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Mon Apr 22 00:33:47 IST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Mon Apr 22 00:33:47 IST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Mon Apr 22 00:33:47 IST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Mon Apr 22 00:33:47 IST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
2019-04-22 00:33:48.182  INFO 6448 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2019-04-22 00:33:48.246  INFO 6448 --- [  restartedMain] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2019-04-22 00:33:48.501  INFO 6448 --- [  restartedMain] org.hibernate.Version                    : HHH000412: Hibernate Core {5.0.12.Final}
2019-04-22 00:33:48.504  INFO 6448 --- [  restartedMain] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2019-04-22 00:33:48.508  INFO 6448 --- [  restartedMain] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2019-04-22 00:33:48.622  INFO 6448 --- [  restartedMain] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2019-04-22 00:33:49.004  INFO 6448 --- [  restartedMain] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2019-04-22 00:33:49.642  INFO 6448 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000227: Running hbm2ddl schema export
2019-04-22 00:33:49.664  INFO 6448 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000230: Schema export complete
2019-04-22 00:33:49.758  INFO 6448 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-04-22 00:33:50.844  INFO 6448 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@143eb1f4: startup date [Mon Apr 22 00:33:36 IST 2019]; root of context hierarchy
2019-04-22 00:33:51.194  INFO 6448 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2019-04-22 00:33:51.198  INFO 6448 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2019-04-22 00:33:51.343  INFO 6448 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-04-22 00:33:51.344  INFO 6448 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-04-22 00:33:51.518  INFO 6448 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-04-22 00:33:51.654  WARN 6448 --- [  restartedMain] .t.AbstractTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
2019-04-22 00:33:53.669  INFO 6448 --- [  restartedMain] b.a.s.AuthenticationManagerConfiguration : 

Using default security password: 0ca7d39a-a92e-4e5b-a0e5-8ac4530d5849

2019-04-22 00:33:53.850  INFO 6448 --- [  restartedMain] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: OrRequestMatcher [requestMatchers=[Ant [pattern='/css/**'], Ant [pattern='/js/**'], Ant [pattern='/images/**'], Ant [pattern='/webjars/**'], Ant [pattern='/**/favicon.ico'], Ant [pattern='/error']]], []
2019-04-22 00:33:54.145  INFO 6448 --- [  restartedMain] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: OrRequestMatcher [requestMatchers=[Ant [pattern='/**']]], [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@27ef81f6, org.springframework.security.web.context.SecurityContextPersistenceFilter@66a10a07, org.springframework.security.web.header.HeaderWriterFilter@3b89247c, org.springframework.security.web.authentication.logout.LogoutFilter@aa4c06d, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@13b8cd35, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@77d4b0f1, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@31591127, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@1245fcf2, org.springframework.security.web.session.SessionManagementFilter@15098ea6, org.springframework.security.web.access.ExceptionTranslationFilter@5e21e0fd, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@7ce8e89c]
2019-04-22 00:33:54.683  WARN 6448 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : Unable to start LiveReload server
2019-04-22 00:33:54.981  INFO 6448 --- [  restartedMain] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2019-04-22 00:33:55.109  INFO 6448 --- [  restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2019-04-22 00:33:55.135  INFO 6448 --- [  restartedMain] .t.a.i.AymuwsManagementSystemApplication : Started AymuwsManagementSystemApplication in 20.199 seconds (JVM running for 21.509)

我正在属性文件中配置所有数据库信息。但是,执行应用程序后,我无法创建数据库。

最佳答案

application.properties 中更改此配置并查看其是否正常工作

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/aymuws?autoReconnect=true&verifyServerCertificate=false&useSSL=false&requireSSL=false

关于java - 在 Spring Boot 中 - 应用程序数据库未创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55786273/

相关文章:

java - 父类(super class)变量的子类引用?

php - 使用 MySQL/PDO 时,表名是否应该以数据库名称和句点为前缀?

php - 一种存储开放时间以确定某个地方现在是否开放的方法

mysql - 编写此 SQL 查询的更好方法是什么?

spring - 如何在spring mvc项目中使用jSTL获取requestURI?

java从php设置类路径

Java 8 : Better to extend class of static methods or better to call static methods directly?

java - 在哪里放置虚拟 Web 服务实现?

java - REST API 在带有 H2 数据库的 Spring Boot Maven 多模块项目中总是抛出 404 错误

java - 方法参数的继承不起作用