0
点赞
收藏
分享

微信扫一扫

【MongoDB】spring 集成 MongoDB

天使魔鬼 2022-11-11 阅读 143


1,所需要的jar包:

   spring本身的jar;

   mongodb驱动包:mongo-java-driver.jar

   spring对mongo的支持:spring-data-commons-core.jar和spring-data-mongodb.jar

   sfl4j相关的:因为mongo要用到sfl4j日志,所以要加入。slf4j-api-1.5.8.jar,slf4j-log4j12-1.5.8.jar,log4j-1.2.17.jar还有commom的一堆jar。


2,mongodb的属性文件:

mongo.host=localhost
mongo.port=27017
mongo.dbname=tmrbigtable


3,spring配置连接池:

<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">


<!-- 获取配置文件属性 -->
<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:res/db.properties</value>
</property>
</bean>

<!-- Default bean name is 'mongo' -->
<!-- 连接池配置详细查看http://api.mongodb.org/java/2.7.2/com/mongodb/MongoOptions.html#connectionsPerHost

-->
<mongo:mongo host="${mongo.host}" port="${mongo.port}">
<mongo:options connections-per-host="10"
threads-allowed-to-block-for-connection-multiplier="10"
auto-connect-retry="true" />
</mongo:mongo>


<!-- Spring提供的mongodb操作模板-->
<bean id="mongoTemplate"
class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongo" />
<constructor-arg name="databaseName" value="${mongo.dbname}" />
</bean>
</beans>

使用placeholderConfig配置。这个配置文件可能会报错,如果报错,就把错误相应的skema的版本号去掉。


4,使用template:

package com.controller;

import javax.annotation.Resource;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bean.Person;

@Controller
public class MainController {

@Resource
private MongoTemplate mongoTemplate;

@RequestMapping("index.htm")
public String index(){

Person p1 = new Person();
p1.setName("ly");
Person p2 = new Person();
p2.setId(1);
p2.setName("lzy");

mongoTemplate.insert(p1);
mongoTemplate.insert(p2);

int size = mongoTemplate.find(new Query(), Person.class).size();
System.out.println(size);
return "index";
}
}


要先启动mongodb才可以(cmd命令行),然后可以用mongo的一款可视化工具来操作,mongoVUE。

5,实体类:

package com.bean;

import org.springframework.data.annotation.Id;

public class Person {

@Id
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}

mongodb里面的每一条文档,都会有属性_id,对应实体类中名字为id或者使用@Id标记的属性,如果没有指定,那么会用java对象的objectId。如果要插入数据,那么要指定id,比如id是int类型,如果不指定的话,id默认为0,这样会被当作插入主键相同的数据,那么只会插入一条数据。insert函数如果不指定集合名,那么会使用默认的集合,也就是类名的全小写。


项目源码:

​​http://pan.baidu.com/s/1slvb5Sh​​


举报

相关推荐

0 条评论