Implement database connection
This commit is contained in:
parent
5a3d6ef7ea
commit
ab71d13d2b
@ -17,6 +17,8 @@ dependencies {
|
|||||||
testImplementation('org.springframework.boot:spring-boot-starter-test') {
|
testImplementation('org.springframework.boot:spring-boot-starter-test') {
|
||||||
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
|
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 {
|
test {
|
||||||
|
@ -3,6 +3,7 @@ package com.example.demo;
|
|||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
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.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@ -24,6 +25,21 @@ import javax.websocket.server.PathParam;
|
|||||||
@RestController
|
@RestController
|
||||||
public class Controller {
|
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{
|
public static void downloadFileFromUrl(String url, String file) throws IOException{
|
||||||
File filer = new File(file);
|
File filer = new File(file);
|
||||||
filer.createNewFile();
|
filer.createNewFile();
|
||||||
|
43
src/main/java/com/example/demo/Data.java
Normal file
43
src/main/java/com/example/demo/Data.java
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
7
src/main/java/com/example/demo/DataRepository.java
Normal file
7
src/main/java/com/example/demo/DataRepository.java
Normal file
@ -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…
x
Reference in New Issue
Block a user