Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
617 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<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/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.boaglio.casadocodigo.mongodb</groupId> | ||
<artifactId>seriados-java-springdata</artifactId> | ||
|
||
<packaging>war</packaging> | ||
<version>1.0-SNAPSHOT</version> | ||
<name>Seriados com Java + Spring Data</name> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<spring.version>3.2.8.RELEASE</spring.version> | ||
</properties> | ||
|
||
<dependencies> | ||
|
||
<!-- dependencias do Spring 3 --> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-core</artifactId> | ||
<version>${spring.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-web</artifactId> | ||
<version>${spring.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-webmvc</artifactId> | ||
<version>${spring.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context</artifactId> | ||
<version>${spring.version}</version> | ||
</dependency> | ||
|
||
<!-- mongodb java driver --> | ||
<dependency> | ||
<groupId>org.mongodb</groupId> | ||
<artifactId>mongo-java-driver</artifactId> | ||
<version>2.12.3</version> | ||
</dependency> | ||
|
||
<!-- Spring data mongodb --> | ||
<dependency> | ||
<groupId>org.springframework.data</groupId> | ||
<artifactId>spring-data-mongodb</artifactId> | ||
<version>1.5.4.RELEASE</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>cglib</groupId> | ||
<artifactId>cglib</artifactId> | ||
<version>2.2.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>jstl</artifactId> | ||
<version>1.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>servletapi</groupId> | ||
<artifactId>servlet-api</artifactId> | ||
<version>2.4-20040521</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<finalName>seriados</finalName> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.1</version> | ||
<configuration> | ||
<source>1.7</source> | ||
<target>1.7</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.tomcat.maven</groupId> | ||
<artifactId>tomcat7-maven-plugin</artifactId> | ||
<version>2.2</version> | ||
<configuration> | ||
<httpPort>8080</httpPort> | ||
<path>/</path> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mvn clean install tomcat7:run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mvn clean install tomcat7:run |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/boaglio/casadocodigo/mongodb/config/SpringMongoConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.boaglio.casadocodigo.mongodb.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.mongodb.config.AbstractMongoConfiguration; | ||
|
||
import com.mongodb.Mongo; | ||
import com.mongodb.MongoClient; | ||
|
||
@Configuration | ||
public class SpringMongoConfig extends AbstractMongoConfiguration { | ||
|
||
private static final String SERVIDOR_MONGODB = "127.0.0.1"; | ||
private static final String BANCO_DE_DADOS = "test"; | ||
|
||
@Override | ||
public String getDatabaseName() { | ||
return BANCO_DE_DADOS; | ||
} | ||
|
||
@Override | ||
@Bean | ||
public Mongo mongo() throws Exception { | ||
return new MongoClient(SERVIDOR_MONGODB); | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
src/main/java/com/boaglio/casadocodigo/mongodb/controller/SeriadosController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.boaglio.casadocodigo.mongodb.controller; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.ModelMap; | ||
import org.springframework.web.bind.annotation.ModelAttribute; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
import com.boaglio.casadocodigo.mongodb.model.Seriado; | ||
import com.boaglio.casadocodigo.mongodb.repository.SeriadosRepository; | ||
|
||
@Controller | ||
public class SeriadosController { | ||
|
||
@Autowired | ||
private SeriadosRepository repository; | ||
|
||
@RequestMapping(value = "/home",method = RequestMethod.GET) | ||
public String index(ModelMap model) { | ||
|
||
List<Seriado> seriados = repository.findAll(); | ||
model.addAttribute("seriados",seriados); | ||
return "home"; | ||
|
||
} | ||
|
||
@RequestMapping(value = "/detalhe",method = RequestMethod.GET) | ||
public String detalhe(@RequestParam("id") String id,ModelMap model) { | ||
|
||
Seriado seriado = repository.findById(id); | ||
model.addAttribute("seriado",seriado); | ||
int totalDePersonagens = 0; | ||
if (seriado.getPersonagens() != null) { | ||
totalDePersonagens = seriado.getPersonagens().size(); | ||
} | ||
model.addAttribute("totalDePersonagens",totalDePersonagens); | ||
return "detalhe"; | ||
|
||
} | ||
|
||
@RequestMapping(value = "/alterar",method = RequestMethod.GET) | ||
public String alterar(@ModelAttribute Seriado seriado,ModelMap model,HttpServletRequest request) { | ||
|
||
List<String> personagens = new ArrayList<String>(); | ||
personagens.add(request.getParameter("personagem1")); | ||
personagens.add(request.getParameter("personagem2")); | ||
personagens.add(request.getParameter("personagem3")); | ||
personagens.add(request.getParameter("personagem4")); | ||
personagens.add(request.getParameter("personagem5")); | ||
personagens.add(request.getParameter("personagem6")); | ||
seriado.setPersonagens(personagens); | ||
|
||
repository.update(seriado); | ||
model.addAttribute("msg","update"); | ||
|
||
return "detalhe"; | ||
|
||
} | ||
|
||
@RequestMapping(value = "/remover",method = RequestMethod.GET) | ||
public String remover(@RequestParam("id") String id,ModelMap model) { | ||
|
||
repository.remove(id); | ||
model.addAttribute("msg","delete"); | ||
|
||
return "detalhe"; | ||
|
||
} | ||
|
||
@RequestMapping(value = "/novo",method = RequestMethod.GET) | ||
public String novo() { | ||
return "novo"; | ||
} | ||
|
||
@RequestMapping(value = "/adicionar",method = RequestMethod.POST) | ||
public String adicionar(@ModelAttribute Seriado seriado,ModelMap model,HttpServletRequest request) { | ||
|
||
List<String> personagens = new ArrayList<String>(); | ||
personagens.add(request.getParameter("personagem1")); | ||
personagens.add(request.getParameter("personagem2")); | ||
personagens.add(request.getParameter("personagem3")); | ||
personagens.add(request.getParameter("personagem4")); | ||
personagens.add(request.getParameter("personagem5")); | ||
personagens.add(request.getParameter("personagem6")); | ||
seriado.setPersonagens(personagens); | ||
|
||
repository.insert(seriado); | ||
model.addAttribute("msg","add"); | ||
|
||
return "novo"; | ||
|
||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
src/main/java/com/boaglio/casadocodigo/mongodb/model/Seriado.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.boaglio.casadocodigo.mongodb.model; | ||
|
||
import java.io.Serializable; | ||
import java.util.List; | ||
|
||
import org.bson.types.ObjectId; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
|
||
@Document(collection = "seriados") | ||
public class Seriado implements Serializable { | ||
|
||
private static final long serialVersionUID = -2944144005656142035L; | ||
|
||
@Id | ||
private ObjectId id; | ||
|
||
private String nome; | ||
private List<String> personagens; | ||
|
||
public ObjectId getId() { | ||
return id; | ||
} | ||
|
||
public void setId(ObjectId id) { | ||
this.id = id; | ||
} | ||
|
||
public String getNome() { | ||
return nome; | ||
} | ||
|
||
public void setNome(String nome) { | ||
this.nome = nome; | ||
} | ||
|
||
public List<String> getPersonagens() { | ||
return personagens; | ||
} | ||
|
||
public void setPersonagens(List<String> personagens) { | ||
this.personagens = personagens; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + (id == null ? 0 : id.hashCode()); | ||
result = prime * result + (nome == null ? 0 : nome.hashCode()); | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { return true; } | ||
if (obj == null) { return false; } | ||
if (getClass() != obj.getClass()) { return false; } | ||
Seriado other = (Seriado) obj; | ||
if (id == null) { | ||
if (other.id != null) { return false; } | ||
} else if (!id.equals(other.id)) { return false; } | ||
if (nome == null) { | ||
if (other.nome != null) { return false; } | ||
} else if (!nome.equals(other.nome)) { return false; } | ||
return true; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Seriado [id=" + id + ", nome=" + nome + ", personagens=" + personagens + "]"; | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/com/boaglio/casadocodigo/mongodb/repository/SeriadosRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.boaglio.casadocodigo.mongodb.repository; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.bson.types.ObjectId; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
import org.springframework.data.mongodb.core.query.Criteria; | ||
import org.springframework.data.mongodb.core.query.Query; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import com.boaglio.casadocodigo.mongodb.model.Seriado; | ||
|
||
@Repository | ||
public class SeriadosRepository { | ||
|
||
@Autowired | ||
private MongoTemplate mongoTemplate; | ||
|
||
public List<Seriado> findAll() { | ||
|
||
List<Seriado> seriados = new ArrayList<Seriado>(); | ||
seriados = mongoTemplate.findAll(Seriado.class); | ||
System.out.println("total de seriados = " + seriados.size()); | ||
return seriados; | ||
} | ||
|
||
public Seriado findById(String id) { | ||
|
||
Seriado seriado = new Seriado(); | ||
System.out.println("busca por id = " + id); | ||
|
||
Query queryDeBuscaPorID = new Query(Criteria.where("id").is(id)); | ||
|
||
seriado = mongoTemplate.findOne(queryDeBuscaPorID,Seriado.class); | ||
|
||
return seriado; | ||
} | ||
|
||
public void insert(Seriado seriado) { | ||
|
||
mongoTemplate.insert(seriado); | ||
|
||
System.out.println("novo seriado = " + seriado); | ||
|
||
} | ||
|
||
public void update(Seriado seriado) { | ||
|
||
mongoTemplate.save(seriado); | ||
|
||
System.out.println("seriado alterado = " + seriado); | ||
|
||
} | ||
|
||
public void remove(String id) { | ||
|
||
Seriado seriadoParaRemover = new Seriado(); | ||
seriadoParaRemover.setId(new ObjectId(id)); | ||
mongoTemplate.remove(seriadoParaRemover); | ||
|
||
System.out.println("seriado removido = " + id); | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.