minor cleanup

This commit is contained in:
eugenp 2015-07-31 16:20:14 +03:00
parent 90937c825d
commit 73a63d007f
4 changed files with 44 additions and 43 deletions

View File

@ -7,21 +7,8 @@
<name>spring-data-mongodb</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.springframework.data.version>1.7.1.RELEASE</org.springframework.data.version>
<org.springframework.version>4.1.7.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>
<org.slf4j.version>1.7.12</org.slf4j.version>
<logback.version>1.1.3</logback.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
@ -60,19 +47,19 @@
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
@ -80,6 +67,7 @@
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
@ -92,4 +80,18 @@
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.springframework.data.version>1.7.1.RELEASE</org.springframework.data.version>
<org.springframework.version>4.1.7.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>
<org.slf4j.version>1.7.12</org.slf4j.version>
<logback.version>1.1.3</logback.version>
</properties>
</project>

View File

@ -1,8 +1,8 @@
package org.baeldung.repository;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.baeldung.model.User;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface UserRepository extends MongoRepository<User, String> {
//
}

View File

@ -11,13 +11,12 @@ import org.springframework.data.mongodb.core.query.Update;
import com.mongodb.WriteResult;
public class UserService {
@Autowired
private MongoTemplate mongoTemplate;
public void insertUser(User user) {
public void insertUser(final User user) {
mongoTemplate.insert(user, "user");
}
@ -25,42 +24,42 @@ public class UserService {
return mongoTemplate.findAll(User.class, "user");
}
public void removeUser(User user) {
public void removeUser(final User user) {
mongoTemplate.remove(user, "user");
}
public void saveUser(User user) {
public void saveUser(final User user) {
mongoTemplate.save(user, "user");
}
public User findAndModifyUser(String name, String newName) {
Query query = new Query();
public User findAndModifyUser(final String name, final String newName) {
final Query query = new Query();
query.addCriteria(Criteria.where("name").is(name));
Update update = new Update();
final 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();
public void updateFirstUser(final String name, final String newName) {
final Query query = new Query();
query.addCriteria(Criteria.where("name").is(name));
Update update = new Update();
final 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();
public WriteResult upsertUser(final String name, final String newName) {
final Query query = new Query();
query.addCriteria(Criteria.where("name").is(name));
Update update = new Update();
final 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();
public void updateMultiUser(final String name, final String newName) {
final Query query = new Query();
query.addCriteria(Criteria.where("name").is(name));
Update update = new Update();
final Update update = new Update();
update.set("name", newName);
mongoTemplate.updateMulti(query, update, User.class);
}

View File

@ -44,7 +44,7 @@ public class UserRepositoryIntegrationTest {
@Test
public void testInsert() {
User user = new User();
final User user = new User();
user.setName("Jon");
userRepository.insert(user);
@ -58,7 +58,7 @@ public class UserRepositoryIntegrationTest {
mongoOps.insert(user);
user = mongoOps.findOne(Query.query(Criteria.where("name").is("Jack")), User.class);
String id = user.getId();
final String id = user.getId();
user.setName("Jim");
userRepository.save(user);
@ -68,7 +68,7 @@ public class UserRepositoryIntegrationTest {
@Test
public void testDelete() {
User user = new User();
final User user = new User();
user.setName("Benn");
mongoOps.insert(user);
@ -84,7 +84,7 @@ public class UserRepositoryIntegrationTest {
mongoOps.insert(user);
user = mongoOps.findOne(Query.query(Criteria.where("name").is("Chris")), User.class);
User foundUser = userRepository.findOne(user.getId());
final User foundUser = userRepository.findOne(user.getId());
assertThat(user.getName(), is(foundUser.getName()));
}
@ -96,7 +96,7 @@ public class UserRepositoryIntegrationTest {
mongoOps.insert(user);
user = mongoOps.findOne(Query.query(Criteria.where("name").is("Harris")), User.class);
boolean isExists = userRepository.exists(user.getId());
final boolean isExists = userRepository.exists(user.getId());
assertThat(isExists, is(true));
}
@ -111,7 +111,7 @@ public class UserRepositoryIntegrationTest {
user.setName("Adam");
mongoOps.insert(user);
List<User> users = userRepository.findAll(new Sort(Sort.Direction.ASC, "name"));
final List<User> users = userRepository.findAll(new Sort(Sort.Direction.ASC, "name"));
assertThat(users.size(), is(2));
assertThat(users.get(0).getName(), is("Adam"));
@ -128,9 +128,9 @@ public class UserRepositoryIntegrationTest {
user.setName("Adam");
mongoOps.insert(user);
Pageable pageableRequest = new PageRequest(0, 2);
final Pageable pageableRequest = new PageRequest(0, 2);
Page<User> users = userRepository.findAll(pageableRequest);
final Page<User> users = userRepository.findAll(pageableRequest);
assertThat(users.getTotalPages(), is(1));
assertThat(users.iterator().next().getName(), is("Brendan"));