Spring-data-MongoDB操作MongoDB

2018/06/09 11:50
阅读数 490

参考资料

参考资料

参考资料

参考资料

为MongoDB设置用户名密码

整合MongoDB 整合的时候注意spring和spring-data-mongodb的版本

两种配置方式,一种是MongoDB没有设置密码,另一种是MongoDB设置密码的

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:p="http://www.springframework.org/schema/p" 				xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context" 	xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
	xmlns:tool="http://www.springframework.org/schema/tool" 		xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	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.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
    http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.10.xsd">

	<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
		<constructor-arg name="mongo" ref="mongo" />
		<constructor-arg name="databaseName" value="${mongo.dbname}" />
	</bean>

	<bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
		<property name="host" value="${mongo.host}" />
		<property name="port" value="${mongo.port}" />
	</bean>

	<!--credentials的配置形式是:用户名:密码@默认数据库 -->
<!-- 	<mongo:mongo-client id="mongoClient" host="${mongo.host}"
		port="${mongo.port}" credentials="${mongo.username}:${mongo.password}@${mongo.dbname}">
		<mongo:client-options write-concern="SAFE"
			connections-per-host="${mongo.connectionsPerHost}"
			threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
			connect-timeout="${mongo.connectTimeout}" max-wait-time="${mongo.maxWaitTime}"
			socket-timeout="${mongo.socketTimeout}" />
	</mongo:mongo-client>
	
	<mongo:db-factory id="mongoDbFactory" dbname="${mongo.dbname}" mongo-ref="mongoClient" />
	
	<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
		<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
	</bean> -->
</beans> 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:application-context.xml"})
public class MongoDBTest {
	

	@Autowired
	private MongoTemplate mongoTemplate;
	
	@Test
	public void createCollectionTest(){
		mongoTemplate.createCollection("user");
	}
	
	@Test
	public void insertTest(){
		User user1 = new User.BuilderUser().setName("kevin").setAge(25).setSex("man").build();
		User user2 = new User.BuilderUser().setName("shen").setAge(26).setSex("man").build();
		
		List<User> batchToSave = new ArrayList<User>();
		batchToSave.add(user1);
		batchToSave.add(user2);
		mongoTemplate.insert(batchToSave, User.class);
	}
	@Test
	public void updateTest() {
		Query query = new Query();
		query.addCriteria(Criteria.where("sex").is("1"));
		Update update = Update.update("name", "Mr.wang");
		mongoTemplate.upsert(query, update, "user");

	}
	
	@Test
	public void findTest(){
		List<User> userList = mongoTemplate.findAll(User.class);
		System.out.println(userList);
	}
	
	@Test
	public void findByQueryTest(){
		Query query = new Query(Criteria.where("sex").is("man")).with(new Sort(Sort.Direction.ASC,"age"));
		query.fields().include("name");
		List<User> userList = mongoTemplate.find(query, User.class);
		System.out.println(userList);
	}
	
	
	@Test
	public void deleteSocumentTest(){
		Query query = new Query(Criteria.where("name").is("kevin"));
		mongoTemplate.remove(query, User.class);
	}
	
	@Test
	public void deleteTest(){
		mongoTemplate.dropCollection("user");
	}
	
}
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部