Merge pull request #230 from alex-semenyuk/master

Examples with MongoRepositories.
This commit is contained in:
Eugen 2015-07-31 06:17:42 -07:00
commit 90937c825d
5 changed files with 150 additions and 135 deletions

View File

@ -2,11 +2,13 @@ package org.baeldung.config;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration; import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import com.mongodb.Mongo; import com.mongodb.Mongo;
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
@Configuration @Configuration
@EnableMongoRepositories(basePackages = "org.baeldung.repository")
public class MongoConfig extends AbstractMongoConfiguration { public class MongoConfig extends AbstractMongoConfiguration {
@Override @Override
@ -20,7 +22,7 @@ public class MongoConfig extends AbstractMongoConfiguration {
} }
@Override @Override
protected String getMappingBasePackage() { public String getMappingBasePackage() {
return "org.baeldung"; return "org.baeldung";
} }
} }

View File

@ -1,68 +1,8 @@
package org.baeldung.repository; package org.baeldung.repository;
import java.util.List; import org.springframework.data.mongodb.repository.MongoRepository;
import org.baeldung.model.User; 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; public interface UserRepository extends MongoRepository<User, String> {
@Repository
public class UserRepository {
@Autowired
private MongoTemplate mongoTemplate;
public void insertUser(final User user) {
mongoTemplate.insert(user, "user");
}
public List<User> listUser() {
return mongoTemplate.findAll(User.class, "user");
}
public void removeUser(final User user) {
mongoTemplate.remove(user, "user");
}
public void saveUser(final User user) {
mongoTemplate.save(user, "user");
}
public User findAndModifyUser(final String name, final String newName) {
final Query query = new Query();
query.addCriteria(Criteria.where("name").is(name));
final Update update = new Update();
update.set("name", newName);
return mongoTemplate.findAndModify(query, update, User.class);
}
public void updateFirstUser(final String name, final String newName) {
final Query query = new Query();
query.addCriteria(Criteria.where("name").is(name));
final Update update = new Update();
update.set("name", newName);
mongoTemplate.updateFirst(query, update, User.class);
}
public WriteResult upsertUser(final String name, final String newName) {
final Query query = new Query();
query.addCriteria(Criteria.where("name").is(name));
final Update update = new Update();
update.set("name", newName);
return mongoTemplate.upsert(query, update, User.class);
}
public void updateMultiUser(final String name, final String newName) {
final Query query = new Query();
query.addCriteria(Criteria.where("name").is(name));
final Update update = new Update();
update.set("name", newName);
mongoTemplate.updateMulti(query, update, User.class);
}
} }

View File

@ -0,0 +1,67 @@
package org.baeldung.service;
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 com.mongodb.WriteResult;
public class UserService {
@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);
}
}

View File

@ -1,15 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?> <?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" <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" xmlns:p="http://www.springframework.org/schema/p" xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation=" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd" 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"> <bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
<property name="host" value="localhost"/> <property name="host" value="localhost"/>
</bean> </bean>
@ -21,4 +21,6 @@
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/> <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<mongo:repositories base-package="org.baeldung.repository" mongo-template-ref="mongoTemplate"/>
</beans> </beans>

View File

@ -3,6 +3,8 @@ package org.baeldung.repository;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import java.util.List;
import org.baeldung.config.MongoConfig; import org.baeldung.config.MongoConfig;
import org.baeldung.model.User; import org.baeldung.model.User;
import org.junit.After; import org.junit.After;
@ -10,6 +12,10 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Query;
@ -17,7 +23,7 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { MongoConfig.class, UserRepository.class }) @ContextConfiguration(classes = MongoConfig.class)
public class UserRepositoryIntegrationTest { public class UserRepositoryIntegrationTest {
@Autowired @Autowired
@ -36,13 +42,11 @@ public class UserRepositoryIntegrationTest {
mongoOps.dropCollection(User.class); mongoOps.dropCollection(User.class);
} }
// tests
@Test @Test
public void testInsert() { public void testInsert() {
final User user = new User(); User user = new User();
user.setName("Jon"); user.setName("Jon");
userRepository.insertUser(user); userRepository.insert(user);
assertThat(mongoOps.findOne(Query.query(Criteria.where("name").is("Jon")), User.class).getName(), is("Jon")); assertThat(mongoOps.findOne(Query.query(Criteria.where("name").is("Jon")), User.class).getName(), is("Jon"));
} }
@ -51,84 +55,84 @@ public class UserRepositoryIntegrationTest {
public void testSave() { public void testSave() {
User user = new User(); User user = new User();
user.setName("Jack"); user.setName("Jack");
userRepository.insertUser(user); mongoOps.insert(user);
user = mongoOps.findOne(Query.query(Criteria.where("name").is("Jack")), User.class); user = mongoOps.findOne(Query.query(Criteria.where("name").is("Jack")), User.class);
final String id = user.getId(); String id = user.getId();
user.setName("Jim"); user.setName("Jim");
userRepository.saveUser(user); userRepository.save(user);
assertThat(mongoOps.findOne(Query.query(Criteria.where("id").is(id)), User.class).getName(), is("Jim")); assertThat(mongoOps.findOne(Query.query(Criteria.where("id").is(id)), User.class).getName(), is("Jim"));
} }
@Test @Test
public void testUpdateFirst() { public void testDelete() {
User user = new User(); 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() {
final 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() {
final User user = new User();
user.setName("Benn"); user.setName("Benn");
mongoOps.insert(user); mongoOps.insert(user);
assertThat(mongoOps.find(Query.query(Criteria.where("name").is("Benn")), User.class).size(), is(1)); userRepository.delete(user);
userRepository.removeUser(user);
assertThat(mongoOps.find(Query.query(Criteria.where("name").is("Benn")), User.class).size(), is(0)); assertThat(mongoOps.find(Query.query(Criteria.where("name").is("Benn")), User.class).size(), is(0));
} }
@Test
public void testFindOne() {
User user = new User();
user.setName("Chris");
mongoOps.insert(user);
user = mongoOps.findOne(Query.query(Criteria.where("name").is("Chris")), User.class);
User foundUser = userRepository.findOne(user.getId());
assertThat(user.getName(), is(foundUser.getName()));
}
@Test
public void testExists() {
User user = new User();
user.setName("Harris");
mongoOps.insert(user);
user = mongoOps.findOne(Query.query(Criteria.where("name").is("Harris")), User.class);
boolean isExists = userRepository.exists(user.getId());
assertThat(isExists, is(true));
}
@Test
public void testFindAllWithSort() {
User user = new User();
user.setName("Brendan");
mongoOps.insert(user);
user = new User();
user.setName("Adam");
mongoOps.insert(user);
List<User> users = userRepository.findAll(new Sort(Sort.Direction.ASC, "name"));
assertThat(users.size(), is(2));
assertThat(users.get(0).getName(), is("Adam"));
assertThat(users.get(1).getName(), is("Brendan"));
}
@Test
public void testFindAllWithPageable() {
User user = new User();
user.setName("Brendan");
mongoOps.insert(user);
user = new User();
user.setName("Adam");
mongoOps.insert(user);
Pageable pageableRequest = new PageRequest(0, 2);
Page<User> users = userRepository.findAll(pageableRequest);
assertThat(users.getTotalPages(), is(1));
assertThat(users.iterator().next().getName(), is("Brendan"));
}
} }