mongodb(3)Mongodb Configuration and Class
1. Spring XML:
Some concept in Spring data configuration:
<repositories base-package="com.acme.repositories">
<context:exclude-filter type="regex" expression=".*SomeRepository" />
</repositories>
<repositories base-package="com.acme.repository">
<repository id="userRepository" repository-impl-ref="customRepositoryImplementation" />
</repositories>
<bean id="customRepositoryImplementation" class="…">
<!-- further configuration -->
</bean>
interface UserRepositoryCustom {
public void someCustomMethod(User user);
}
class UserRepositoryImpl implements UserRepositoryCustom {
public void someCustomMethod(User user) {
// Your custom implementation
}
}
There are a lot of customized codes and configuration below. But from the blog, the simple example is as follow:
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.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/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd">
<!-- Activate Spring Data MongoDB repository support -->
<mongo:repositories base-package="com.sillycat.easynosql.dao.mongodb.repository" />
<!-- MongoDB host -->
<mongo:mongo host="${mongo.host.name}" port="${mongo.host.port}"/>
<!-- Template for performing MongoDB operations -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"
c:mongo-ref="mongo" c:databaseName="${mongo.db.name}"/>
<!-- Service for initializing MongoDB with sample data using MongoTemplate -->
<bean id="initMongoService" class="com.sillycat.easynosql.dao.mongodb.init.InitMongoService" init-method="init"/>
</beans>
2. Annotation in POJO
package com.sillycat.easynosql.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document
public class Role {
@Id
private String id;
private Integer role;
...snip getter and setter...
}
package com.sillycat.easynosql.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;
@Document
public class User {
@Id
private String id;
private String firstName;
private String lastName;
private String username;
private String password;
@DBRef
private Role role;
...snip... getter and setter...
}
3. Interface of Repository
Repsitory Lay is the DAO layer, With the help of Spring Data MongoDB, Spring will automatically provide the actual implementation.
Page<User> users = repository.findAll(new PageRequest(1, 20);
There are two main ways that the repository proxy is able to come up with the store specific query from the method name. The first option is to derive the query from the method name directly, the second is using some kind of additionally created query.
User findByUsername(String username);
We will strip the prefixes findBy, find, readBy, read, getBy as well as get from the method and start parsing the rest of it.At a very basic level you can define conditions on entity properties and concatenate them with AND and OR.
List<Person> findByEmailAddressAndLastname(EmailAddress emailAddress, String lastname);
Page<User> findByLastname(String lastname, Pageable pageable);
List<User> findByLastname(String lastname, Sort sort);
List<User> findByLastname(String lastname, Pageable pageable);
package com.sillycat.easynosql.dao.mongodb.repository;
import org.springframework.data.mongodb.repository.MongoRepository;
import com.sillycat.easynosql.model.User;
public interface UserRepository extends MongoRepository<User, String> {
User findByUsername(String username);
}
4. InitMongoService to build the init data
package com.sillycat.easynosql.dao.mongodb.init;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import com.sillycat.easynosql.model.Role;
import com.sillycat.easynosql.model.User;
public class InitMongoService {
@Autowired
private MongoTemplate mongoTemplate;
public void init() {
// Drop existing collections
mongoTemplate.dropCollection("role");
mongoTemplate.dropCollection("user");
// Create new records
Role adminRole = new Role();
adminRole.setId(UUID.randomUUID().toString());
adminRole.setRole(1);
Role userRole = new Role();
userRole.setId(UUID.randomUUID().toString());
userRole.setRole(2);
User john = new User();
john.setId(UUID.randomUUID().toString());
john.setFirstName("John");
john.setLastName("Smith");
john.setPassword("111111");
john.setRole(adminRole);
john.setUsername("john");
User jane = new User();
jane.setId(UUID.randomUUID().toString());
jane.setFirstName("Jane");
jane.setLastName("Adams");
jane.setPassword("111111");
jane.setRole(userRole);
jane.setUsername("jane");
// Insert to db
mongoTemplate.insert(john, "user");
mongoTemplate.insert(jane, "user");
mongoTemplate.insert(adminRole, "role");
mongoTemplate.insert(userRole, "role");
}
}
5. UserService to work with the repositories
package com.sillycat.easynosql.service;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sillycat.easynosql.dao.mongodb.repository.RoleRepository;
import com.sillycat.easynosql.dao.mongodb.repository.UserRepository;
import com.sillycat.easynosql.model.User;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
@Autowired
private RoleRepository roleRepository;
public User create(User user) {
user.setId(UUID.randomUUID().toString());
user.getRole().setId(UUID.randomUUID().toString());
roleRepository.save(user.getRole());
return userRepository.save(user);
}
public User read(User user) {
return user;
}
public List<User> readAll() {
return userRepository.findAll();
}
public User update(User user) {
User existingUser = userRepository.findByUsername(user.getUsername());
if (existingUser == null) {
return null;
}
existingUser.setFirstName(user.getFirstName());
existingUser.setLastName(user.getLastName());
existingUser.getRole().setRole(user.getRole().getRole());
roleRepository.save(existingUser.getRole());
return userRepository.save(existingUser);
}
public Boolean delete(User user) {
User existingUser = userRepository.findByUsername(user.getUsername());
if (existingUser == null) {
return false;
}
roleRepository.delete(existingUser.getRole());
userRepository.delete(existingUser);
return true;
}
}
references:
http://feiyan35488.iteye.com/blog/763165
http://qljcly.iteye.com/blog/703714
http://liureying.blog.163.com/blog/static/6151352011030433930/
http://gundumw100.iteye.com/blog/452221
http://static.springsource.org/spring-data/data-mongodb/docs/current/reference/html/
1. Spring XML:
Some concept in Spring data configuration:
<repositories base-package="com.acme.repositories">
<context:exclude-filter type="regex" expression=".*SomeRepository" />
</repositories>
<repositories base-package="com.acme.repository">
<repository id="userRepository" repository-impl-ref="customRepositoryImplementation" />
</repositories>
<bean id="customRepositoryImplementation" class="…">
<!-- further configuration -->
</bean>
interface UserRepositoryCustom {
public void someCustomMethod(User user);
}
class UserRepositoryImpl implements UserRepositoryCustom {
public void someCustomMethod(User user) {
// Your custom implementation
}
}
There are a lot of customized codes and configuration below. But from the blog, the simple example is as follow:
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.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/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd">
<!-- Activate Spring Data MongoDB repository support -->
<mongo:repositories base-package="com.sillycat.easynosql.dao.mongodb.repository" />
<!-- MongoDB host -->
<mongo:mongo host="${mongo.host.name}" port="${mongo.host.port}"/>
<!-- Template for performing MongoDB operations -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"
c:mongo-ref="mongo" c:databaseName="${mongo.db.name}"/>
<!-- Service for initializing MongoDB with sample data using MongoTemplate -->
<bean id="initMongoService" class="com.sillycat.easynosql.dao.mongodb.init.InitMongoService" init-method="init"/>
</beans>
2. Annotation in POJO
package com.sillycat.easynosql.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document
public class Role {
@Id
private String id;
private Integer role;
...snip getter and setter...
}
package com.sillycat.easynosql.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;
@Document
public class User {
@Id
private String id;
private String firstName;
private String lastName;
private String username;
private String password;
@DBRef
private Role role;
...snip... getter and setter...
}
3. Interface of Repository
Repsitory Lay is the DAO layer, With the help of Spring Data MongoDB, Spring will automatically provide the actual implementation.
Page<User> users = repository.findAll(new PageRequest(1, 20);
There are two main ways that the repository proxy is able to come up with the store specific query from the method name. The first option is to derive the query from the method name directly, the second is using some kind of additionally created query.
User findByUsername(String username);
We will strip the prefixes findBy, find, readBy, read, getBy as well as get from the method and start parsing the rest of it.At a very basic level you can define conditions on entity properties and concatenate them with AND and OR.
List<Person> findByEmailAddressAndLastname(EmailAddress emailAddress, String lastname);
Page<User> findByLastname(String lastname, Pageable pageable);
List<User> findByLastname(String lastname, Sort sort);
List<User> findByLastname(String lastname, Pageable pageable);
package com.sillycat.easynosql.dao.mongodb.repository;
import org.springframework.data.mongodb.repository.MongoRepository;
import com.sillycat.easynosql.model.User;
public interface UserRepository extends MongoRepository<User, String> {
User findByUsername(String username);
}
4. InitMongoService to build the init data
package com.sillycat.easynosql.dao.mongodb.init;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import com.sillycat.easynosql.model.Role;
import com.sillycat.easynosql.model.User;
public class InitMongoService {
@Autowired
private MongoTemplate mongoTemplate;
public void init() {
// Drop existing collections
mongoTemplate.dropCollection("role");
mongoTemplate.dropCollection("user");
// Create new records
Role adminRole = new Role();
adminRole.setId(UUID.randomUUID().toString());
adminRole.setRole(1);
Role userRole = new Role();
userRole.setId(UUID.randomUUID().toString());
userRole.setRole(2);
User john = new User();
john.setId(UUID.randomUUID().toString());
john.setFirstName("John");
john.setLastName("Smith");
john.setPassword("111111");
john.setRole(adminRole);
john.setUsername("john");
User jane = new User();
jane.setId(UUID.randomUUID().toString());
jane.setFirstName("Jane");
jane.setLastName("Adams");
jane.setPassword("111111");
jane.setRole(userRole);
jane.setUsername("jane");
// Insert to db
mongoTemplate.insert(john, "user");
mongoTemplate.insert(jane, "user");
mongoTemplate.insert(adminRole, "role");
mongoTemplate.insert(userRole, "role");
}
}
5. UserService to work with the repositories
package com.sillycat.easynosql.service;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sillycat.easynosql.dao.mongodb.repository.RoleRepository;
import com.sillycat.easynosql.dao.mongodb.repository.UserRepository;
import com.sillycat.easynosql.model.User;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
@Autowired
private RoleRepository roleRepository;
public User create(User user) {
user.setId(UUID.randomUUID().toString());
user.getRole().setId(UUID.randomUUID().toString());
roleRepository.save(user.getRole());
return userRepository.save(user);
}
public User read(User user) {
return user;
}
public List<User> readAll() {
return userRepository.findAll();
}
public User update(User user) {
User existingUser = userRepository.findByUsername(user.getUsername());
if (existingUser == null) {
return null;
}
existingUser.setFirstName(user.getFirstName());
existingUser.setLastName(user.getLastName());
existingUser.getRole().setRole(user.getRole().getRole());
roleRepository.save(existingUser.getRole());
return userRepository.save(existingUser);
}
public Boolean delete(User user) {
User existingUser = userRepository.findByUsername(user.getUsername());
if (existingUser == null) {
return false;
}
roleRepository.delete(existingUser.getRole());
userRepository.delete(existingUser);
return true;
}
}
references:
http://feiyan35488.iteye.com/blog/763165
http://qljcly.iteye.com/blog/703714
http://liureying.blog.163.com/blog/static/6151352011030433930/
http://gundumw100.iteye.com/blog/452221
http://static.springsource.org/spring-data/data-mongodb/docs/current/reference/html/