diff --git a/pom.xml b/pom.xml
index c36a4aa..893bd49 100644
--- a/pom.xml
+++ b/pom.xml
@@ -48,6 +48,10 @@
spring-boot-starter-test
test
+
+ org.springframework.boot
+ spring-boot-starter-data-mongodb
+
diff --git a/src/main/java/fr/gouv/interieur/dso/AppJavaForgeDemoApplication.java b/src/main/java/fr/gouv/interieur/dso/AppJavaForgeDemoApplication.java
index e0dd02a..4c42040 100644
--- a/src/main/java/fr/gouv/interieur/dso/AppJavaForgeDemoApplication.java
+++ b/src/main/java/fr/gouv/interieur/dso/AppJavaForgeDemoApplication.java
@@ -1,13 +1,35 @@
package fr.gouv.interieur.dso;
+import fr.gouv.interieur.dso.models.Customer;
+import fr.gouv.interieur.dso.models.Demo;
+import fr.gouv.interieur.dso.repository.CustomerRepository;
+import fr.gouv.interieur.dso.repository.DemoRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
-public class AppJavaForgeDemoApplication {
+public class AppJavaForgeDemoApplication implements CommandLineRunner {
+
+ @Autowired
+ private CustomerRepository customerRepository;
+
+ @Autowired
+ private DemoRepository demoRepository;
public static void main(String[] args) {
SpringApplication.run(AppJavaForgeDemoApplication.class, args);
}
+ @Override
+ public void run(String... args) throws Exception {
+ customerRepository.deleteAll();
+ customerRepository.save(new Customer("Zakari", "Karahacane"));
+ Demo demo = new Demo();
+ demo.setName("Zakari ;)");
+ demoRepository.equals(demo);
+ System.out.println("created $$$$");
+
+ }
}
diff --git a/src/main/java/fr/gouv/interieur/dso/controller/DemoController.java b/src/main/java/fr/gouv/interieur/dso/controller/DemoController.java
index a95ce26..2d7cd6c 100644
--- a/src/main/java/fr/gouv/interieur/dso/controller/DemoController.java
+++ b/src/main/java/fr/gouv/interieur/dso/controller/DemoController.java
@@ -1,6 +1,8 @@
package fr.gouv.interieur.dso.controller;
+import fr.gouv.interieur.dso.models.Customer;
import fr.gouv.interieur.dso.models.Demo;
+import fr.gouv.interieur.dso.repository.CustomerRepository;
import fr.gouv.interieur.dso.service.jdbc.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
@@ -20,6 +22,9 @@ public class DemoController {
@Autowired
private DemoService demoService;
+ @Autowired
+ private CustomerRepository customerRepository;
+
@GetMapping("/")
public ResponseDto helloWorld() {
ResponseDto responseDto = new ResponseDto();
@@ -28,8 +33,13 @@ public ResponseDto helloWorld() {
return responseDto;
}
- @GetMapping("/demo")
+ @GetMapping("/postgres")
public ResponseEntity> getDemosList(){
return new ResponseEntity<>(demoService.getListDemo(), HttpStatus.OK);
}
+
+ @GetMapping("/mongo")
+ public ResponseEntity> getCustomers() {
+ return new ResponseEntity<>(customerRepository.findAll(), HttpStatus.OK);
+ }
}
diff --git a/src/main/java/fr/gouv/interieur/dso/models/Customer.java b/src/main/java/fr/gouv/interieur/dso/models/Customer.java
new file mode 100644
index 0000000..1707c4c
--- /dev/null
+++ b/src/main/java/fr/gouv/interieur/dso/models/Customer.java
@@ -0,0 +1,27 @@
+package fr.gouv.interieur.dso.models;
+
+import org.springframework.data.annotation.Id;
+
+public class Customer {
+
+ @Id
+ public String id;
+
+ public String firstName;
+ public String lastName;
+
+ public Customer() {}
+
+ public Customer(String firstName, String lastName) {
+ this.firstName = firstName;
+ this.lastName = lastName;
+ }
+
+ @Override
+ public String toString() {
+ return String.format(
+ "Customer[id=%s, firstName='%s', lastName='%s']",
+ id, firstName, lastName);
+ }
+
+}
\ No newline at end of file
diff --git a/src/main/java/fr/gouv/interieur/dso/repository/CustomerRepository.java b/src/main/java/fr/gouv/interieur/dso/repository/CustomerRepository.java
new file mode 100644
index 0000000..5059d66
--- /dev/null
+++ b/src/main/java/fr/gouv/interieur/dso/repository/CustomerRepository.java
@@ -0,0 +1,11 @@
+package fr.gouv.interieur.dso.repository;
+
+import fr.gouv.interieur.dso.models.Customer;
+import org.springframework.data.mongodb.repository.MongoRepository;
+
+import java.util.List;
+
+public interface CustomerRepository extends MongoRepository {
+ public List findAll();
+
+}
diff --git a/src/main/java/fr/gouv/interieur/dso/repository/DemoRepository.java b/src/main/java/fr/gouv/interieur/dso/repository/DemoRepository.java
index fc87146..7da8131 100644
--- a/src/main/java/fr/gouv/interieur/dso/repository/DemoRepository.java
+++ b/src/main/java/fr/gouv/interieur/dso/repository/DemoRepository.java
@@ -7,4 +7,6 @@
@Repository
public interface DemoRepository {
public List getAll();
+
+ public Integer insert(Demo demo);
}
diff --git a/src/main/java/fr/gouv/interieur/dso/repository/impl/DemoRepositoryImpl.java b/src/main/java/fr/gouv/interieur/dso/repository/impl/DemoRepositoryImpl.java
index 2d1693d..8cec384 100644
--- a/src/main/java/fr/gouv/interieur/dso/repository/impl/DemoRepositoryImpl.java
+++ b/src/main/java/fr/gouv/interieur/dso/repository/impl/DemoRepositoryImpl.java
@@ -4,11 +4,13 @@
import fr.gouv.interieur.dso.repository.DemoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Repository;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
+import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -35,4 +37,18 @@ public List getAll() {
});
return demoList;
}
+
+ @Override
+ public Integer insert(Demo demo) {
+ GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
+ String sql = "INSERT INTO `demo` (`name`) VALUES (?);";
+ int rowsAffected = jdbcTemplate.update(conn -> {
+ PreparedStatement preparedStatement = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
+ preparedStatement.setString(1, demo.getName());
+ return preparedStatement;
+ }, generatedKeyHolder);
+ // Get auto-incremented ID
+ Integer id = generatedKeyHolder.getKey().intValue();
+ return id;
+ }
}
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index 511fb04..a34d2cf 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -3,5 +3,12 @@ spring:
url: jdbc:postgresql://localhost:5432/demo_dso
username: demo_dso
password: demo_dso
+ data:
+ mongodb:
+ uri: mongodb://root:root@localhost:27018/demo?authSource=admin
+ database: demo
+ username: root
+ password: root
+ authentication-database: admin
liquibase:
- change-log: classpath:db/master.xml
\ No newline at end of file
+ change-log: classpath:db/master.xml