Added the example with MongoRepository
This commit is contained in:
parent
973227f23a
commit
2e54ddc841
|
@ -2,11 +2,13 @@ package org.baeldung.config;
|
|||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
|
||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||
|
||||
import com.mongodb.Mongo;
|
||||
import com.mongodb.MongoClient;
|
||||
|
||||
@Configuration
|
||||
@EnableMongoRepositories(basePackages = "org.baeldung.repository")
|
||||
public class MongoConfig extends AbstractMongoConfiguration {
|
||||
|
||||
@Override
|
||||
|
@ -20,7 +22,7 @@ public class MongoConfig extends AbstractMongoConfiguration {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected String getMappingBasePackage() {
|
||||
public String getMappingBasePackage() {
|
||||
return "org.baeldung";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,68 +1,11 @@
|
|||
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.data.mongodb.repository.MongoRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.baeldung.model.User;
|
||||
|
||||
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);
|
||||
}
|
||||
public interface UserRepository extends MongoRepository<User, String> {
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -1,15 +1,16 @@
|
|||
<?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"
|
||||
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans
|
||||
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/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>
|
||||
|
@ -20,5 +21,7 @@
|
|||
</bean>
|
||||
|
||||
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
|
||||
|
||||
<mongo:repositories base-package="org.baeldung.repository" mongo-template-ref="mongoTemplate" />
|
||||
|
||||
</beans>
|
|
@ -3,14 +3,19 @@ package org.baeldung.repository;
|
|||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.config.MongoConfig;
|
||||
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.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.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
|
@ -18,8 +23,9 @@ import org.springframework.test.context.ContextConfiguration;
|
|||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes={MongoConfig.class, UserRepository.class})
|
||||
@ContextConfiguration(classes = MongoConfig.class)
|
||||
public class UserRepositoryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
|
@ -40,7 +46,7 @@ public class UserRepositoryIntegrationTest {
|
|||
public void testInsert() {
|
||||
User user = new User();
|
||||
user.setName("Jon");
|
||||
userRepository.insertUser(user);
|
||||
userRepository.insert(user);
|
||||
|
||||
assertThat(mongoOps.findOne(Query.query(Criteria.where("name").is("Jon")), User.class).getName(), is("Jon"));
|
||||
}
|
||||
|
@ -49,84 +55,84 @@ public class UserRepositoryIntegrationTest {
|
|||
public void testSave() {
|
||||
User user = new User();
|
||||
user.setName("Jack");
|
||||
userRepository.insertUser(user);
|
||||
mongoOps.insert(user);
|
||||
|
||||
user = mongoOps.findOne(Query.query(Criteria.where("name").is("Jack")), User.class);
|
||||
String id = user.getId();
|
||||
|
||||
user.setName("Jim");
|
||||
userRepository.saveUser(user);
|
||||
userRepository.save(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() {
|
||||
public void testDelete() {
|
||||
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);
|
||||
userRepository.delete(user);
|
||||
|
||||
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"));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue