initial commit

This commit is contained in:
manalejandro 2018-11-18 21:10:51 +01:00
commit 75115585a6
15 changed files with 378 additions and 0 deletions

1
README.md Normal file
View File

@ -0,0 +1 @@
## MongoDB CRUD with SpringBoot

64
pom.xml Normal file
View File

@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.manalejandro</groupId>
<artifactId>mongodbcrud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>mongodbcrud</name>
<description>Demo project for Spring Boot MongoDB CRUD</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<!-- <scope>test</scope> -->
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,14 @@
package com.manalejandro.mongodbcrud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
@SpringBootApplication
@EnableMongoRepositories(basePackages = { "com.manalejandro.mongodbcrud.repositories" })
public class MongodbcrudApplication {
public static void main(String[] args) {
SpringApplication.run(MongodbcrudApplication.class, args);
}
}

View File

@ -0,0 +1,13 @@
package com.manalejandro.mongodbcrud;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MongodbcrudApplication.class);
}
}

View File

@ -0,0 +1,44 @@
package com.manalejandro.mongodbcrud.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.manalejandro.mongodbcrud.forms.IndexForm;
import com.manalejandro.mongodbcrud.model.Item;
import com.manalejandro.mongodbcrud.services.ItemService;
import com.manalejandro.mongodbcrud.vo.IndexVO;
@Controller
public class IndexController implements ErrorController {
private ItemService itemService;
@Autowired
public IndexController(ItemService itemService) {
this.itemService = itemService;
}
@RequestMapping("/")
public String index(final @ModelAttribute IndexForm indexForm, final Model model) {
IndexVO indexVO = new IndexVO();
if (indexForm.getId() == null) {
indexVO.getListItem().addAll(itemService.getAll());
} else {
Item item = itemService.getItem(indexForm.getId());
indexForm.setId(item.getId());
indexForm.setNombre(item.getNombre());
indexForm.setApellido1(item.getApellido1());
indexForm.setApellido2(item.getApellido2());
}
model.addAttribute("indexVO", indexVO);
return "index";
}
public String getErrorPath() {
return "error";
}
}

View File

@ -0,0 +1,41 @@
package com.manalejandro.mongodbcrud.forms;
public class IndexForm {
private String id;
private String nombre;
private String apellido1;
private String apellido2;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getApellido1() {
return apellido1;
}
public void setApellido1(String apellido1) {
this.apellido1 = apellido1;
}
public String getApellido2() {
return apellido2;
}
public void setApellido2(String apellido2) {
this.apellido2 = apellido2;
}
}

View File

@ -0,0 +1,56 @@
package com.manalejandro.mongodbcrud.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document
public class Item {
@Id
private String id;
private String nombre;
private String apellido1;
private String apellido2;
public Item() {
super();
};
public Item(String nombre, String apellido1, String apellido2) {
this.nombre = nombre;
this.apellido1 = apellido1;
this.apellido2 = apellido2;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getApellido1() {
return apellido1;
}
public void setApellido1(String apellido1) {
this.apellido1 = apellido1;
}
public String getApellido2() {
return apellido2;
}
public void setApellido2(String apellido2) {
this.apellido2 = apellido2;
}
}

View File

@ -0,0 +1,10 @@
package com.manalejandro.mongodbcrud.repositories;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import com.manalejandro.mongodbcrud.model.Item;
@Repository
public interface ItemRepository extends MongoRepository<Item, String> {
}

View File

@ -0,0 +1,13 @@
package com.manalejandro.mongodbcrud.services;
import java.util.List;
import com.manalejandro.mongodbcrud.model.Item;
public interface ItemService {
public Item getItem(String id);
public List<Item> getAll();
}

View File

@ -0,0 +1,28 @@
package com.manalejandro.mongodbcrud.services;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.manalejandro.mongodbcrud.model.Item;
import com.manalejandro.mongodbcrud.repositories.ItemRepository;
@Service
public class ItemServiceImpl implements ItemService {
private ItemRepository itemRepository;
@Autowired
public ItemServiceImpl(ItemRepository itemRepository) {
this.itemRepository = itemRepository;
}
public List<Item> getAll() {
return this.itemRepository.findAll();
}
public Item getItem(String id) {
return this.itemRepository.findById(id).isPresent() ? this.itemRepository.findById(id).get() : null;
}
}

View File

@ -0,0 +1,56 @@
package com.manalejandro.mongodbcrud.vo;
import java.util.ArrayList;
import java.util.List;
import com.manalejandro.mongodbcrud.model.Item;
public class IndexVO {
private List<Item> listItem = new ArrayList<Item>();
private String id;
private String nombre;
private String apellido1;
private String apellito2;
public List<Item> getListItem() {
return listItem;
}
public void setListItem(List<Item> listItem) {
this.listItem = listItem;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getApellido1() {
return apellido1;
}
public void setApellido1(String apellido1) {
this.apellido1 = apellido1;
}
public String getApellito2() {
return apellito2;
}
public void setApellito2(String apellito2) {
this.apellito2 = apellito2;
}
}

View File

@ -0,0 +1,2 @@
server.servlet.context-path=/mongodbcrud
server.port=3117

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Error</title>
</head>
<body>
Error
</body>
</html>

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>

View File

@ -0,0 +1,16 @@
package com.manalejandro.mongodbcrud;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class MongodbcrudApplicationTests {
@Test
public void contextLoads() {
}
}