Initial commit
This commit is contained in:
parent
b3d9ada21e
commit
fbc3539b6e
|
@ -0,0 +1,26 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="output" path="target/classes"/>
|
||||||
|
</classpath>
|
|
@ -0,0 +1,23 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>spring-data-mongodb</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
|
@ -0,0 +1,102 @@
|
||||||
|
<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>org.baeldung</groupId>
|
||||||
|
<artifactId>spring-data-mongodb</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>spring-data-mongodb</name>
|
||||||
|
<url>http://maven.apache.org</url>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
|
||||||
|
<org.springframework.version>4.1.6.RELEASE</org.springframework.version>
|
||||||
|
<org.hamcrest.version>1.3</org.hamcrest.version>
|
||||||
|
<junit.version>4.11</junit.version>
|
||||||
|
<rest-assured.version>2.4.1</rest-assured.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-orm</artifactId>
|
||||||
|
<version>${org.springframework.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-webmvc</artifactId>
|
||||||
|
<version>${org.springframework.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.data</groupId>
|
||||||
|
<artifactId>spring-data-mongodb</artifactId>
|
||||||
|
<version>1.7.1.RELEASE</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>jstl</groupId>
|
||||||
|
<artifactId>jstl</artifactId>
|
||||||
|
<version>1.2</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>3.8.1</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit-dep</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hamcrest</groupId>
|
||||||
|
<artifactId>hamcrest-core</artifactId>
|
||||||
|
<version>${org.hamcrest.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hamcrest</groupId>
|
||||||
|
<artifactId>hamcrest-library</artifactId>
|
||||||
|
<version>${org.hamcrest.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-test</artifactId>
|
||||||
|
<version>3.2.0.RELEASE</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.servlet</groupId>
|
||||||
|
<artifactId>javax.servlet-api</artifactId>
|
||||||
|
<version>3.0.1</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>2.3.2</version>
|
||||||
|
<configuration>
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
|
@ -0,0 +1,37 @@
|
||||||
|
package org.baeldung.model;
|
||||||
|
|
||||||
|
import org.springframework.data.annotation.Id;
|
||||||
|
import org.springframework.data.mongodb.core.mapping.Document;
|
||||||
|
|
||||||
|
@Document
|
||||||
|
public class User {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
private String id;
|
||||||
|
private String name;
|
||||||
|
private Integer age;
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(Integer age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,68 @@
|
||||||
|
package org.baeldung.repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.baeldung.model.User;
|
||||||
|
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.data.mongodb.core.query.Update;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import com.mongodb.WriteResult;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class UserRepository {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MongoTemplate mongoTemplate;
|
||||||
|
|
||||||
|
public void insertUser(User user) {
|
||||||
|
mongoTemplate.insert(user, "user");
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<User> listUser() {
|
||||||
|
return mongoTemplate.findAll(User.class, "user");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeUser(User user) {
|
||||||
|
mongoTemplate.remove(user, "user");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void saveUser(User user) {
|
||||||
|
mongoTemplate.save(user, "user");
|
||||||
|
}
|
||||||
|
|
||||||
|
public User findAndModifyUser(String name, String newName) {
|
||||||
|
Query query = new Query();
|
||||||
|
query.addCriteria(Criteria.where("name").is(name));
|
||||||
|
Update update = new Update();
|
||||||
|
update.set("name", newName);
|
||||||
|
return mongoTemplate.findAndModify(query, update, User.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateFirstUser(String name, String newName) {
|
||||||
|
Query query = new Query();
|
||||||
|
query.addCriteria(Criteria.where("name").is(name));
|
||||||
|
Update update = new Update();
|
||||||
|
update.set("name", newName);
|
||||||
|
mongoTemplate.updateFirst(query, update, User.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public WriteResult upsertUser(String name, String newName) {
|
||||||
|
Query query = new Query();
|
||||||
|
query.addCriteria(Criteria.where("name").is(name));
|
||||||
|
Update update = new Update();
|
||||||
|
update.set("name", newName);
|
||||||
|
return mongoTemplate.upsert(query, update, User.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateMultiUser(String name, String newName) {
|
||||||
|
Query query = new Query();
|
||||||
|
query.addCriteria(Criteria.where("name").is(name));
|
||||||
|
Update update = new Update();
|
||||||
|
update.set("name", newName);
|
||||||
|
mongoTemplate.updateMulti(query, update, User.class);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
|
||||||
|
xmlns:p="http://www.springframework.org/schema/p"
|
||||||
|
xsi:schemaLocation="
|
||||||
|
http://www.springframework.org/schema/beans
|
||||||
|
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
|
||||||
|
http://www.springframework.org/schema/context
|
||||||
|
http://www.springframework.org/schema/context/spring-context-3.2.xsd"
|
||||||
|
>
|
||||||
|
|
||||||
|
<context:component-scan base-package="org.baeldung"/>
|
||||||
|
|
||||||
|
<bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
|
||||||
|
<property name="host" value="localhost"/>
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
|
||||||
|
<constructor-arg name="mongo" ref="mongo"/>
|
||||||
|
<constructor-arg name="databaseName" value="test"/>
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
|
||||||
|
|
||||||
|
</beans>
|
|
@ -0,0 +1,26 @@
|
||||||
|
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
|
||||||
|
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
|
||||||
|
version="2.5"
|
||||||
|
>
|
||||||
|
|
||||||
|
<servlet>
|
||||||
|
<servlet-name>dispatcher</servlet-name>
|
||||||
|
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|
||||||
|
<load-on-startup>1</load-on-startup>
|
||||||
|
</servlet>
|
||||||
|
|
||||||
|
<servlet-mapping>
|
||||||
|
<servlet-name>dispatcher</servlet-name>
|
||||||
|
<url-pattern>/</url-pattern>
|
||||||
|
</servlet-mapping>
|
||||||
|
|
||||||
|
<context-param>
|
||||||
|
<param-name>contextConfigLocation</param-name>
|
||||||
|
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
|
||||||
|
</context-param>
|
||||||
|
|
||||||
|
<listener>
|
||||||
|
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
|
||||||
|
</listener>
|
||||||
|
|
||||||
|
</web-app>
|
|
@ -0,0 +1,131 @@
|
||||||
|
package org.baeldung.repository;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
import org.baeldung.model.User;
|
||||||
|
import org.baeldung.repository.UserRepository;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.mongodb.core.MongoOperations;
|
||||||
|
import org.springframework.data.mongodb.core.query.Criteria;
|
||||||
|
import org.springframework.data.mongodb.core.query.Query;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration("file:src/main/webapp/WEB-INF/dispatcher-servlet.xml")
|
||||||
|
public class UserRepositoryTest {
|
||||||
|
@Autowired
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MongoOperations mongoOps;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void testSetup() {
|
||||||
|
mongoOps.createCollection(User.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
mongoOps.dropCollection(User.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInsert() {
|
||||||
|
User user = new User();
|
||||||
|
user.setName("Jon");
|
||||||
|
userRepository.insertUser(user);
|
||||||
|
|
||||||
|
assertThat(mongoOps.findOne(Query.query(Criteria.where("name").is("Jon")), User.class).getName(), is("Jon"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSave() {
|
||||||
|
User user = new User();
|
||||||
|
user.setName("Jack");
|
||||||
|
userRepository.insertUser(user);
|
||||||
|
|
||||||
|
user = mongoOps.findOne(Query.query(Criteria.where("name").is("Jack")), User.class);
|
||||||
|
String id = user.getId();
|
||||||
|
|
||||||
|
user.setName("Jim");
|
||||||
|
userRepository.saveUser(user);
|
||||||
|
|
||||||
|
assertThat(mongoOps.findOne(Query.query(Criteria.where("id").is(id)), User.class).getName(), is("Jim"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateFirst() {
|
||||||
|
User user = new User();
|
||||||
|
user.setName("Alex");
|
||||||
|
mongoOps.insert(user);
|
||||||
|
|
||||||
|
user = new User();
|
||||||
|
user.setName("Alex");
|
||||||
|
mongoOps.insert(user);
|
||||||
|
|
||||||
|
userRepository.updateFirstUser("Alex", "James");
|
||||||
|
|
||||||
|
assertThat(mongoOps.find(Query.query(Criteria.where("name").is("James")), User.class).size(), is(1));
|
||||||
|
assertThat(mongoOps.find(Query.query(Criteria.where("name").is("Alex")), User.class).size(), is(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateMulti() {
|
||||||
|
User user = new User();
|
||||||
|
user.setName("Eugen");
|
||||||
|
mongoOps.insert(user);
|
||||||
|
|
||||||
|
user = new User();
|
||||||
|
user.setName("Eugen");
|
||||||
|
mongoOps.insert(user);
|
||||||
|
|
||||||
|
userRepository.updateMultiUser("Eugen", "Victor");
|
||||||
|
|
||||||
|
assertThat(mongoOps.find(Query.query(Criteria.where("name").is("Victor")), User.class).size(), is(2));
|
||||||
|
assertThat(mongoOps.find(Query.query(Criteria.where("name").is("Eugen")), User.class).size(), is(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFindAndModify() {
|
||||||
|
User user = new User();
|
||||||
|
user.setName("Markus");
|
||||||
|
mongoOps.insert(user);
|
||||||
|
|
||||||
|
user = userRepository.findAndModifyUser("Markus", "Nick");
|
||||||
|
|
||||||
|
assertThat(user.getName(), is("Markus"));
|
||||||
|
assertThat(mongoOps.find(Query.query(Criteria.where("name").is("Nick")), User.class).size(), is(1));
|
||||||
|
assertThat(mongoOps.find(Query.query(Criteria.where("name").is("Markus")), User.class).size(), is(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpsert() {
|
||||||
|
User user = new User();
|
||||||
|
user.setName("Markus");
|
||||||
|
mongoOps.insert(user);
|
||||||
|
|
||||||
|
userRepository.upsertUser("Markus", "Nick");
|
||||||
|
|
||||||
|
assertThat(mongoOps.find(Query.query(Criteria.where("name").is("Nick")), User.class).size(), is(1));
|
||||||
|
assertThat(mongoOps.find(Query.query(Criteria.where("name").is("Markus")), User.class).size(), is(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRemove() {
|
||||||
|
User user = new User();
|
||||||
|
user.setName("Benn");
|
||||||
|
mongoOps.insert(user);
|
||||||
|
|
||||||
|
assertThat(mongoOps.find(Query.query(Criteria.where("name").is("Benn")), User.class).size(), is(1));
|
||||||
|
|
||||||
|
userRepository.removeUser(user);
|
||||||
|
|
||||||
|
assertThat(mongoOps.find(Query.query(Criteria.where("name").is("Benn")), User.class).size(), is(0));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue