小强哥博客

小强哥,小强哥博客,技术大咖

quezon项目总结,mongotemplate使用

Spring Data for MongoDB 是Spring Data项目的一部分,旨在为新的数据存储提供一种熟悉且一致的基于Spring的编程模型,同时保留特定于存储的功能和功能。Spring Data提供了诸多数据库操作模板,如:reidstemplate、jdbctemplate等。

文章简单记录使用mongotemplate做crud功能。阅读文章你需要对maven、mongodb、springboot有一定的了解。

以下代码在springboot1.5.4测试通过。

首先在pom.xml中添加mongo依赖,如下代码,

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

接着在application.propertis中,添加mongo配置,如下

spring.data.mongodb.uri=mongodb://10.99.70.53:32774
spring.data.mongodb.database=quezon

更多关于spring boot mongodb配置如下,可以参考 http://docs.spring.io/spring-boot/docs/1.5.4.RELEASE/reference/htmlsingle/ 获得更多内容

# MONGODB (MongoProperties)
spring.data.mongodb.authentication-database= # Authentication database name.
spring.data.mongodb.database=test # Database name.
spring.data.mongodb.field-naming-strategy= # Fully qualified name of the FieldNamingStrategy to use.
spring.data.mongodb.grid-fs-database= # GridFS database name.
spring.data.mongodb.host=localhost # Mongo server host. Cannot be set with uri.
spring.data.mongodb.password= # Login password of the mongo server. Cannot be set with uri.
spring.data.mongodb.port=27017 # Mongo server port. Cannot be set with uri.
spring.data.mongodb.repositories.enabled=true # Enable Mongo repositories.
spring.data.mongodb.uri=mongodb://localhost/test # Mongo database URI. Cannot be set with host, port and credentials.
spring.data.mongodb.username= # Login user of the mongo server. Cannot be set with uri.

接着编写curd代码,如下,

StudentDO.java

package com.eju.ess.bean;

import java.io.Serializable;

import org.springframework.data.mongodb.core.mapping.Document;

import lombok.Data;

@Data
@Document(collection="student")
public class StudentDO implements Serializable {
	private String name;
	private String sex;
	private Integer age;
	@Override
	public String toString() {
		return "StudentDO [name=" + name + ", sex=" + sex + ", age=" + age + "]";
	}
	public StudentDO(String name, String sex, Integer age) {
		super();
		this.name = name;
		this.sex = sex;
		this.age = age;
	}
}

MongoTest.java

package com.eju.ess;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
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.junit4.SpringRunner;

import com.eju.ess.bean.StudentDO;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RunWith(SpringRunner.class)
/*
 * WebApp测试,需要将下面注释去掉
 * 
 * //@SpringBootTest(classes=Startup.class,webEnvironment =
 * WebEnvironment.DEFINED_PORT) /* 非WebApp测试,需要将下面注释去掉
 */
@SpringBootTest(classes = Startup.class)
public class MongoTest {
	@Autowired
	private MongoTemplate mongoTemplate;

	@Test
	public void test1() {
		mongoTemplate.save(new StudentDO("张三", "男", 23));
	}

	@Test
	public void test2() {
		StudentDO studentDO = mongoTemplate.findOne(Query.query(new Criteria().where("age").is(23)), StudentDO.class);
		log.info(">> {}",studentDO.toString());
	}
	
	@Test
	public void test3() {
		mongoTemplate.updateFirst(
				Query.query(new Criteria().where("age").is(23)), 
				new Update().set("name", "李四"), 
				StudentDO.class);
	}
	
	@Test
	public void test4() {
		mongoTemplate.remove(Query.query(new Criteria().where("age").is(23)),StudentDO.class );
	}
}