💾 Setup SQLite database #3
3 changed files with 76 additions and 0 deletions
|
@ -24,14 +24,24 @@ repositories {
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
implementation("org.springframework.boot:spring-boot-autoconfigure")
|
||||||
|
|
||||||
implementation("org.springframework.boot:spring-boot-starter-jdbc")
|
implementation("org.springframework.boot:spring-boot-starter-jdbc")
|
||||||
|
implementation("org.xerial:sqlite-jdbc")
|
||||||
|
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
|
||||||
|
implementation("org.hibernate.orm:hibernate-community-dialects")
|
||||||
|
|
||||||
implementation("org.springframework.boot:spring-boot-starter-security")
|
implementation("org.springframework.boot:spring-boot-starter-security")
|
||||||
implementation("org.springframework.boot:spring-boot-starter-web")
|
implementation("org.springframework.boot:spring-boot-starter-web")
|
||||||
|
|
||||||
implementation("org.flywaydb:flyway-core")
|
implementation("org.flywaydb:flyway-core")
|
||||||
|
|
||||||
compileOnly("org.projectlombok:lombok")
|
compileOnly("org.projectlombok:lombok")
|
||||||
annotationProcessor("org.projectlombok:lombok")
|
annotationProcessor("org.projectlombok:lombok")
|
||||||
|
|
||||||
testImplementation("org.springframework.boot:spring-boot-starter-test")
|
testImplementation("org.springframework.boot:spring-boot-starter-test")
|
||||||
testImplementation("org.springframework.security:spring-security-test")
|
testImplementation("org.springframework.security:spring-security-test")
|
||||||
|
|
||||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,59 @@
|
||||||
|
package de.mlessmann.certassist.config;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
|
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||||
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||||
|
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
//@EnableJpaRepositories(basePackages = "de.mlessmann.certassist.repositories")
|
||||||
|
@PropertySource("classpath:persistence.properties")
|
||||||
|
public class DatabaseConfig {
|
||||||
|
|
||||||
|
final Environment env;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public DataSource dataSource() {
|
||||||
|
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||||
|
dataSource.setDriverClassName(Objects.requireNonNull(env.getProperty("driverClassName")));
|
||||||
|
dataSource.setUrl(Objects.requireNonNull(env.getProperty("url")));
|
||||||
|
dataSource.setUsername(Objects.requireNonNull(env.getProperty("username")));
|
||||||
|
dataSource.setPassword(Objects.requireNonNull(env.getProperty("password")));
|
||||||
|
return dataSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
||||||
|
final LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
|
||||||
|
entityManagerFactory.setDataSource(dataSource());
|
||||||
|
//entityManagerFactory.setPackagesToScan("de.mlessmann.certassist.models");
|
||||||
|
entityManagerFactory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
|
||||||
|
entityManagerFactory.setJpaProperties(additionalProperties());
|
||||||
|
return entityManagerFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
final Properties additionalProperties() {
|
||||||
|
final Properties hibernateProperties = new Properties();
|
||||||
|
if (env.getProperty("hibernate.hbm2ddl.auto") != null) {
|
||||||
|
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
||||||
|
}
|
||||||
|
if (env.getProperty("hibernate.dialect") != null) {
|
||||||
|
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
||||||
|
}
|
||||||
|
if (env.getProperty("hibernate.show_sql") != null) {
|
||||||
|
hibernateProperties.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
|
||||||
|
}
|
||||||
|
return hibernateProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
7
src/main/resources/persistence.properties
Normal file
7
src/main/resources/persistence.properties
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
driverClassName=org.sqlite.JDBC
|
||||||
|
url=jdbc:sqlite:memory:myDb?cache=shared
|
||||||
|
username=admin
|
||||||
|
password=admin
|
||||||
|
spring.jpa.database-platform=org.hibernate.community.dialect.SQLiteDialect
|
||||||
|
hibernate.hbm2ddl.auto=create-drop
|
||||||
|
hibernate.show_sql=true
|
Loading…
Add table
Reference in a new issue