From 219400614a78ede5c143752d8e64d9876fff39ee Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 16 Mar 2021 16:30:11 +0400 Subject: [PATCH] #4 -- Add database connection and repository example --- build.gradle | 2 + .../ulstu/extractor/db/CommitRepository.java | 42 +++++++++++++++++++ src/main/resources/application.properties | 7 +++- 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/main/java/ru/ulstu/extractor/db/CommitRepository.java diff --git a/build.gradle b/build.gradle index 227b6d5..cd5160e 100644 --- a/build.gradle +++ b/build.gradle @@ -50,6 +50,8 @@ dependencies { compile group: 'org.springframework.boot', name: 'spring-boot-starter-jetty' compile group: 'com.fasterxml.jackson.module', name: 'jackson-module-afterburner' compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5' + compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc' + compile group: 'org.postgresql', name: 'postgresql', version: '9.4.1212' compile group: 'io.springfox', name: 'springfox-boot-starter', version: '3.0.0' diff --git a/src/main/java/ru/ulstu/extractor/db/CommitRepository.java b/src/main/java/ru/ulstu/extractor/db/CommitRepository.java new file mode 100644 index 0000000..71b100d --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/db/CommitRepository.java @@ -0,0 +1,42 @@ +package ru.ulstu.extractor.db; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.stereotype.Repository; +import ru.ulstu.extractor.Commit; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; + +@Repository +public class CommitRepository { + private final static String SQL_SELECT_COMMITS = "SELECT * FROM commit"; + private final static String SQL_INSERT_COMMITS = "INSERT INTO commit (hash, date, author, message) " + + "VALUES (?, ?, ?, ?)"; + private final JdbcTemplate jdbcTemplate; + + public CommitRepository(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + private static class CommitRowMapper implements RowMapper { + + @Override + public Commit mapRow(ResultSet rs, int rowNum) throws SQLException { + return new Commit(rs.getString("message"), + rs.getString("author"), + rs.getDate("date")); + } + } + + public List getCommits() { + return jdbcTemplate.query(SQL_SELECT_COMMITS, new CommitRowMapper()); + } + + public void saveCommit(Commit commit) { + jdbcTemplate.update(SQL_INSERT_COMMITS, "", commit.getDate(), + commit.getAuthor(), + commit.getMessage()); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index c5d4c5d..7b5190f 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -2,4 +2,9 @@ spring.main.banner-mode=off server.port=8080 # Available levels are: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF logging.level.ru.ulstu=DEBUG -extractor.custom-projects-dir= \ No newline at end of file +extractor.custom-projects-dir= +# JPA Settings +spring.datasource.url=jdbc:postgresql://localhost:5432/repo +spring.datasource.username=postgres +spring.datasource.password=postgres +spring.datasource.driverclassName=org.postgresql.Driver \ No newline at end of file