最近才接触Mongodb,在刚开始完全懵逼到现在熟练使用这个过程中走了很多弯路。
这个演示项目基于maven构建,使用Spring4.3.3、Mongodb版本是3.2、mongo-java-driver 3.3.0、spring-data-mongodb 1.9.3
首先创建一个web项目并添加Maven支持
然后pom.xml中添加如下坐标
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.3.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>spring-core</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>spring-beans</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-mongodb -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.9.3.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.3.0</version>
</dependency>
接着创建Spring的配置文件 applicationContext.xml,引入命名空间和如下配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/aop
http://www.springframework.org/schema/aop/spring-aop-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/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.8.xsd
">
<!-- 开启注解 -->
<context:annotation-config />
<!-- 这里要注意引入spring-mongo的命名空间 ,这样才能使用Spring为我们提供的API来方便的操作Mongo -->
<!-- 我这里只贴出基本的配置,保证程序能够基本的运行起来。更多的配置请大家自行折腾 -->
<mongo:mongo-client host="localhost" port="27017"
credentials="username:password@dbname" />
<!-- 注意:上面credentials这个属性是用来认证的,我们都知道,Mongodb刚安装后是没有开启认证的,所以千万不要加上这个属性 不然认证失败,无法操作。
也就是说,当添加用户后并开启认证后的mongodb 才能用这个属性来进行认证,注意写法 @符号必须有 -->
<!-- 这里就是配置MongoTemplate 这个东西是Spring为我们提供的,极大的简化了对Mongodb的操作 -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongo" />
<constructor-arg name="databaseName" value="dbname" />
</bean>
</beans>
创建一个实体类,并插入Mongodb
@Document(collection="person")
public class Person {
@Id
private String id;
private String name;
private int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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;
}
}
这个注解@Document(collection="person"),就是说明这个类映射mongodb中的哪一个集合
spring-data-mongodb中的实体映射是通过MongoMappingConverter这个类实现的。它可以通过注释把java类转换为mongodb的文档。
它有以下几种注释:
- @Id - 文档的唯一标识,在mongodb中为ObjectId,它是唯一的,通过时间戳+机器标识+进程ID+自增计数器(确保同一秒内产生的Id不会冲突)构成。
- @Document - 把一个java类声明为mongodb的文档,可以通过collection参数指定这个类对应的文档。
- @DBRef - 声明类似于关系数据库的关联关系。ps:暂不支持级联的保存功能,当你在本实例中修改了DERef对象里面的值时,单独保存本实例并不能保存DERef引用的对象,它要另外保存,如下面例子的Person和Account。
- @Indexed - 声明该字段需要索引,建索引可以大大的提高查询效率。
- @CompoundIndex - 复合索引的声明,建复合索引可以有效地提高多字段的查询效率。
- @GeoSpatialIndexed - 声明该字段为地理信息的索引。
- @Transient - 映射忽略的字段,该字段不会保存到mongodb。
- @PersistenceConstructor - 声明构造函数,作用是把从数据库取出的数据实例化为对象。该构造函数传入的值为从DBObject中取出的数据。
随便写个service和dao和controller来模拟测试,并在mongodb中创建一个数据库springtest
Dao:
/**
* dao,注入MongoTemplate,插入数据
* @author Sweiit
*
*/
@Repository
public class MongoDao {
@Resource
private MongoTemplate mongoTemplate;
public void savePerson(Person person){
this.mongoTemplate.save(person);
}
}
Service:
@Service
public class MongoService {
@Resource
private MongoDao mongoDao;
@Test
public void savePerson(){
Person person = new Person();
person.setName("test");
person.setAge(18);
this.mongoDao.savePerson(person);
}
}
@Controller
@RequestMapping("/mongo")
public class PersonController {
@Resource
private MongoService mongoService;
@RequestMapping("/test.action")
public void mongotest(){
this.mongoService.savePerson();
}
}
最后启动tomcat,打开浏览器,执行 http://localhost:8080/springmongo/mongo/test.action
再来看数据库: