💾 Setup SQLite database (#3)
* Add SQLite setup * Add basic user entity and repository * Adjust DB naming * Consolidate properties
This commit is contained in:
parent
254f2f09ec
commit
e75ac48743
7 changed files with 141 additions and 0 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -35,3 +35,6 @@ out/
|
||||||
|
|
||||||
### VS Code ###
|
### VS Code ###
|
||||||
.vscode/
|
.vscode/
|
||||||
|
|
||||||
|
### SQLite DB ###
|
||||||
|
sqlLiteDb
|
|
@ -24,14 +24,25 @@ 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")
|
||||||
|
testImplementation("org.assertj:assertj-core")
|
||||||
|
|
||||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,62 @@
|
||||||
|
package de.mlessmann.certassist.config;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
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")
|
||||||
|
public class DatabaseConfig {
|
||||||
|
|
||||||
|
private static final String HIBERNATE_DIALECT_PROPERTY = "hibernate.dialect";
|
||||||
|
private static final String HIBERNATE_HBM_2_DDL_AUTO_PROPERTY = "hibernate.hbm2ddl.auto";
|
||||||
|
private static final String HIBERNATE_SHOW_SQL_PROPERTY = "hibernate.show_sql";
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Properties additionalProperties() {
|
||||||
|
final Properties hibernateProperties = new Properties();
|
||||||
|
if (env.getProperty(HIBERNATE_HBM_2_DDL_AUTO_PROPERTY) != null) {
|
||||||
|
hibernateProperties.setProperty(HIBERNATE_HBM_2_DDL_AUTO_PROPERTY, env.getProperty(HIBERNATE_HBM_2_DDL_AUTO_PROPERTY));
|
||||||
|
}
|
||||||
|
if (env.getProperty(HIBERNATE_DIALECT_PROPERTY) != null) {
|
||||||
|
hibernateProperties.setProperty(HIBERNATE_DIALECT_PROPERTY, env.getProperty(HIBERNATE_DIALECT_PROPERTY));
|
||||||
|
}
|
||||||
|
if (env.getProperty(HIBERNATE_SHOW_SQL_PROPERTY) != null) {
|
||||||
|
hibernateProperties.setProperty(HIBERNATE_SHOW_SQL_PROPERTY, env.getProperty(HIBERNATE_SHOW_SQL_PROPERTY));
|
||||||
|
}
|
||||||
|
|
||||||
|
return hibernateProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
20
src/main/java/de/mlessmann/certassist/models/User.java
Normal file
20
src/main/java/de/mlessmann/certassist/models/User.java
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
package de.mlessmann.certassist.models;
|
||||||
|
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.GeneratedValue;
|
||||||
|
import jakarta.persistence.GenerationType;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Getter
|
||||||
|
public class User {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.SEQUENCE)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@Setter
|
||||||
|
private String username;
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package de.mlessmann.certassist.repositories;
|
||||||
|
|
||||||
|
import de.mlessmann.certassist.models.User;
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface UserRepository extends CrudRepository<User, Long> {
|
||||||
|
|
||||||
|
User findUserById(long id);
|
||||||
|
}
|
|
@ -1 +1,11 @@
|
||||||
|
# General
|
||||||
spring.application.name=home-cert-assistant
|
spring.application.name=home-cert-assistant
|
||||||
|
# Database
|
||||||
|
driverClassName=org.sqlite.JDBC
|
||||||
|
url=jdbc:sqlite:sqlLiteDb:database?cache=shared
|
||||||
|
username=admin
|
||||||
|
password=admin
|
||||||
|
spring.jpa.database-platform=org.hibernate.community.dialect.SQLiteDialect
|
||||||
|
#TODO: Use flyway for db setup
|
||||||
|
hibernate.hbm2ddl.auto=create-drop
|
||||||
|
hibernate.show_sql=true
|
|
@ -0,0 +1,24 @@
|
||||||
|
package de.mlessmann.certassist.repositories;
|
||||||
|
|
||||||
|
import de.mlessmann.certassist.models.User;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
class UserRepositoryTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void findUserById() {
|
||||||
|
final User user = new User();
|
||||||
|
user.setUsername("test");
|
||||||
|
userRepository.save(user);
|
||||||
|
|
||||||
|
assertThat(userRepository.findUserById(user.getId()).getUsername()).isEqualTo("test");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue