From ab71d13d2bd55685682f2e1d60f9b8d06ad83043 Mon Sep 17 00:00:00 2001 From: Joshua Sigona Date: Thu, 23 Jul 2020 04:09:08 +0900 Subject: [PATCH] Implement database connection --- build.gradle | 2 + .../java/com/example/demo/Controller.java | 16 +++++++ src/main/java/com/example/demo/Data.java | 43 +++++++++++++++++++ .../java/com/example/demo/DataRepository.java | 7 +++ src/main/resources/application.properties | 7 ++- 5 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/example/demo/Data.java create mode 100644 src/main/java/com/example/demo/DataRepository.java diff --git a/build.gradle b/build.gradle index c93211d..8218fef 100644 --- a/build.gradle +++ b/build.gradle @@ -17,6 +17,8 @@ dependencies { testImplementation('org.springframework.boot:spring-boot-starter-test') { exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' } + compile('org.springframework.boot:spring-boot-starter-data-jpa:2.0.0.RELEASE') + runtime('mysql:mysql-connector-java:8.0.21') } test { diff --git a/src/main/java/com/example/demo/Controller.java b/src/main/java/com/example/demo/Controller.java index 29d63e1..75bc630 100644 --- a/src/main/java/com/example/demo/Controller.java +++ b/src/main/java/com/example/demo/Controller.java @@ -3,6 +3,7 @@ package com.example.demo; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @@ -24,6 +25,21 @@ import javax.websocket.server.PathParam; @RestController public class Controller { + DataRepository database; + + public Controller(DataRepository database) { + this.database=database; + } + + @GetMapping("") + public Iterable _1(){ + return database.findAll(); + } + @PostMapping("") + public Data _2(@RequestBody Data data){ + return database.save(data); + } + public static void downloadFileFromUrl(String url, String file) throws IOException{ File filer = new File(file); filer.createNewFile(); diff --git a/src/main/java/com/example/demo/Data.java b/src/main/java/com/example/demo/Data.java new file mode 100644 index 0000000..c2cbecf --- /dev/null +++ b/src/main/java/com/example/demo/Data.java @@ -0,0 +1,43 @@ +package com.example.demo; + +import java.util.Date; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +import com.fasterxml.jackson.annotation.JsonFormat; + +@Entity +@Table(name="data") +public class Data { + @Id + @GeneratedValue(strategy=GenerationType.IDENTITY) + Long id; + + String name; + @Column(columnDefinition="date") + @JsonFormat(pattern="MM-dd-yyyy") + Date submittedOn; + public Long getId() { + return id; + } + public void setId(Long id) { + this.id = id; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public Date getSubmittedOn() { + return submittedOn; + } + public void setSubmittedOn(Date submittedOn) { + this.submittedOn = submittedOn; + } +} diff --git a/src/main/java/com/example/demo/DataRepository.java b/src/main/java/com/example/demo/DataRepository.java new file mode 100644 index 0000000..0486b19 --- /dev/null +++ b/src/main/java/com/example/demo/DataRepository.java @@ -0,0 +1,7 @@ +package com.example.demo; + +import org.springframework.data.repository.CrudRepository; + +public interface DataRepository extends CrudRepository{ + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b13789..a692ce4 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,6 @@ - +spring.datasource.url=jdbc:mysql://localhost/playground?serverTimezone=UTC +spring.datasource.username=sigonasr2 +spring.datasource.password= +spring.datasource.driver-class-name=com.mysql.jdbc.Driver +spring.jpa.hibernate.ddl-auto=update +spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect \ No newline at end of file