Implement database connection

master
Joshua Sigona 4 years ago
parent 5a3d6ef7ea
commit ab71d13d2b
  1. 2
      build.gradle
  2. 16
      src/main/java/com/example/demo/Controller.java
  3. 43
      src/main/java/com/example/demo/Data.java
  4. 7
      src/main/java/com/example/demo/DataRepository.java
  5. 7
      src/main/resources/application.properties

@ -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 {

@ -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<Data> _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();

@ -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;
}
}

@ -0,0 +1,7 @@
package com.example.demo;
import org.springframework.data.repository.CrudRepository;
public interface DataRepository extends CrudRepository<Data,Long>{
}

@ -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
Loading…
Cancel
Save