I have checked all the dependencies and added SQL Driver as well. I am using the MYSQL database. After running the Spring Boot application DB is not created. I am using the Data Management tool in STS4.
I expect that the CitizenDB
database and database table Citizen
will be created because I have added all necessary properties to the application.yml
configuration file
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.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>CitizenService</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>CitizenService</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>16</java.version>
<spring-cloud.version>2021.0.3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-apache-client4</artifactId>
<version>1.19.4</version>
</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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.yml:
server:
port: 8081
spring:
datasource:
url: jdbc:mysql://localhost:3306/CitizenDB
username: root
password: March@031996
driver-class-name: com.mysql.cj.jdbc.Driver
application:
name: CITIZEN-SERVICE
jpa:
show-sql: true
database-platform: org.hibernate.dialect.MYSQLInnoDBDialect
generate-ddl: true
hibernate:
ddl-auto: update
Entity class:
package com.example.demo.CitizenService.Entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Citizen {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Column
private String name;
@Column
private int vaccinationId;
}
Main class:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableDiscoveryClient
public class CitizenServiceApplication {
public static void main(String[] args) {
SpringApplication.run(CitizenServiceApplication.class, args);
}
}
CodePudding user response:
You just add createDatabaseIfNotExist=true like this
spring
datasource
url=jdbc:mysql://localhost:3306/CitizenDB?createDatabaseIfNotExist=true
to your application.yml file
CodePudding user response:
First of all, please include well-formatted code in your question, which may be easily copy-pasted for to reproduce your issue.
Use separated code blocks for different files and types of files especially. Your application.yml
configuration is completely unreadable.
Remove any text lines like enter code here package com.example.demo.CitizenService;
or ENTITY CLASS:
. They make your code uncompilable. Use comments and javadocs instead. However, if you don't want to do that, you may add the flag
Use code formatting in your IDE. Example for IntelliJ IDEA.
Second, please clearly specify your issue and what exactly you have done to resolve it. I'm assuming you expect that your Spring Boot application should create the database CitizenDB
and the Citizen
table in it.
Spring (more specifically Hibernate) doesn't create a database by default and it's a bad practice. It assumes that you already have a created database and better to create it yourself. However, if you still want to create the database automatically you may specify such flag createDatabaseIfNotExist=true
in your data source URL. Example for your case:
spring:
datasource:
url: jdbc:mysql://localhost:3306/CitizenDB?createDatabaseIfNotExist=true
You have to improve excluding of the DataSourceAutoConfiguration
class since you want the Spring to configure everything for you. Your main class should look like this:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class CitizenServiceApplication {
public static void main(String[] args) {
SpringApplication.run(CitizenServiceApplication.class, args);
}
}