第一步:src/main/resources下新建配置文件application.properties
#spring.data.mongodb是前缀,任意写
spring.data.mongodb.uri = mongodb://account:password@192.168.0.0:27017/test?maxPoolSize=1000
第二步:pom文件添加依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.5.RELEASE</version>
<relativePath />
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
第三步:看如下代码
package com.baidu.config;
import java.io.Serializable;
public class MongoConfig implements Serializable {
private static final long serialVersionUID = -5941423929526795088L;
private String uri;
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
}
package com.baidu.config;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.core.MongoTemplate;
public class MongoTemplateConfig {
@Bean(name = "mongoConfig")
@ConfigurationProperties(prefix = "spring.data.mongodb")
public MongoConfig phcloudConfig() {
return new MongoConfig();
}
@Primary
@Bean(name = "mongoTemplate")
public MongoTemplate phcloudMongoTemplate(@Qualifier("mongoConfig") MongoConfig mongoConfig) {
return MongoTemplateFactory.createMongoTemplate(mongoConfig);
}
}
package com.baidu.config;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
public class MongoTemplateFactory {
public static MongoTemplate createMongoTemplate(MongoConfig mongoConfig) {
MongoClientURI uri = new MongoClientURI(mongoConfig.getUri());
MongoClient client = new MongoClient(uri);
MongoDbFactory factory = new SimpleMongoDbFactory(client, uri.getDatabase());
// converter Don't save _class to mongo
MappingMongoConverter converter = new MappingMongoConverter(new DefaultDbRefResolver(factory), new MongoMappingContext());
converter.setTypeMapper(new DefaultMongoTypeMapper(null));
// template
return new MongoTemplate(factory, converter);
}
}
第四步:dao层 实体类
package com.baidu.entity;
import java.io.Serializable;
public class UserEntity implements Serializable {
/**
*
*/
private static final long serialVersionUID = 4477284653938772276L;
private String name;
private int age;
private String gender;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "UserEntity [name=" + name + ", age=" + age + ", gender=" + gender + "]";
}
}
package com.baidu.dao;
import com.baidu.entity.UserEntity;
public interface MongoDao {
void saveUser(UserEntity user);
UserEntity findUserByName(String name);
}
package com.baidu.dao.impl;
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.stereotype.Component;
import com.baidu.entity.UserEntity;
@Component
public class MongoDaoImpl implements MongoDao {
@Autowired
private MongoTemplate mongoTemplate;
public void saveUser(UserEntity user) {
mongoTemplate.save(user);
}
public UserEntity findUserByName(String name) {
Query query = new Query(Criteria.where("name").is(name));
UserEntity user = mongoTemplate.findOne(query, UserEntity.class);
return user;
}
}
第五步:测试
package com.baidu.test;
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.test.context.junit4.SpringRunner;
import com.baidu.entity.UserEntity;
import com.baidu.dao.MongoDao;
@RunWith(SpringRunner.class)
@SpringBootTest
public class MongoTest {
@Autowired
private MongoDao userDao;
@Test
public void saveUser() {
UserEntity user = new UserEntity();
user.setAge(44);
user.setGender("女");
user.setName("菜虚鲲");
userDao.saveUser(user);
}
@Test
public void findUserByName() {
UserEntity user = userDao.findUserByName("菜虚鲲");
System.out.println(user);
}
}
大功告成!