MongoDB-Spring集成

1:spring xml配置:

<?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: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.1.xsd
		http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.3.xsd">

	<!-- Default bean name is 'mongo' -->
    <mongo:mongo host="192.168.6.196" port="27017">
        <!-- OPTIONAL: configure <mongo:options /> -->
    </mongo:mongo>
  
	<mongo:db-factory id="mongoDbFactory" mongo-ref="mongo" dbname="SHAM" />

	<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
		<constructor-arg ref="mongoDbFactory" />
		<property name="writeConcern" value="SAFE" />
	</bean>

</beans>

 

2:测试用例:

import java.util.Date;
import java.util.List;

import org.junit.Test;
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.test.context.ContextConfiguration;

import com.xx.entity.Role;
import com.xx.test.core.SpringTransactionalTestCase;
 
@ContextConfiguration(locations = { "/applicationContext*.xml" })
public class MongoDBTest extends SpringTransactionalTestCase  {
	@Autowired MongoTemplate mongoTemplate;
	
	@Test public void testCrud() {
		Role role = new Role();
		role.setCode("role");
		role.setCreateDate(new Date(System.currentTimeMillis()));
		role.setId(4l);
		role.setName("张三");
		
		mongoTemplate.save(role,"role");
	}
	
	@Test
	public void findAll() {
		List<Role> roles = mongoTemplate.find(new Query(), Role.class);
		for(Role r : roles) {
			System.out.println(r.getName());
		}
	}

	@Test
	public void findAndModify() { 
		mongoTemplate.updateFirst(new Query(Criteria.where("id").is(4l)), new Update().set("code", "编码"),Role.class);
	}

	/*@Test
	public List<Role> findByRegex(String regex) {
		Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
		Criteria criteria = new Criteria("name").regex(pattern.toString());
		return mongoTemplate.find(new Query(criteria), Role.class);
	}*/

	@Test
	public void findOne() {
		Role role = mongoTemplate.findOne(new Query(Criteria.where("id").is(4l)), Role.class);
		System.out.println(role.getName() + " " + role.getCode());
	}

	/*@Test
	public void insert(Role role) {
		mongoTemplate.insert(role);
	}*/

	@Test
	public void removeAll() {
		List<Role> list = mongoTemplate.find(new Query(), Role.class);
		if(list != null){
			for(Role role : list){
				mongoTemplate.remove(role);
				System.out.println("remove :" + role.getName());
			}
		}
	}

	@Test
	public void removeOne(){
		Criteria criteria = Criteria.where("id").in(4l);
		if(criteria == null){
			 Query query = new Query(criteria);
			 if(query != null && mongoTemplate.findOne(query, Role.class) != null)
				 mongoTemplate.remove(mongoTemplate.findOne(query, Role.class));
		}
	}

}

 

你可能感兴趣的:(mongodb)