您的位置:首页 > 数据库 > Mongodb

Spring+Mongodb开发记录

2016-05-04 16:11 169 查看
阅读本文的前提是对Spring框架有所了解。

本文着重记录Spring和Mongodb开发的实例,对Spring本身并未做过多的叙述。

mongo.xml

<?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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"
default-lazy-init="true">
<!-- mongodb配置 -->
<bean id="mongodb" class="com.shbc.common.mongo.MongoConfig">
<property name="host" value="${mongo.host}"/>
<property name="port" value="${mongo.port}"/>
<property name="username" value="${mongo.username}"/>
<property name="password" value="${mongo.password}"/>
<property name="connectionsPerHost" value="${mongo.connectionsPerHost}"/>
<property name="socketTimeOut" value="${mongo.socketTimeOut}"/>
<property name="threadsAllowedToBlockForConnectionMultiplier" value="${mongo.threadsAllowedToBlockForConnectionMultiplier}"/>
<property name="defaultDatebase" value="${mongo.defaultDatebase}"/>
</bean>
</beans>


mongo.properties

mongo.host=192.168.0.205
mongo.port=27017
mongo.username=test
mongo.password=test
mongo.connectionsPerHost=50
mongo.socketTimeOut=300000
mongo.threadsAllowedToBlockForConnectionMultiplier=50
mongo.defaultDatebase=WLCloud


package com.shbc.batch;

import java.net.UnknownHostException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.QueryOperators;
import com.mongodb.ServerAddress;

@Controller
public class QidongTest {

@Scheduled(cron = "00 10 18 * * ?")
public void hourgetCount() throws UnknownHostException, ParseException {

MongoCredential credential = MongoCredential.createCredential("test", "WLCloud", "test".toCharArray());
ServerAddress serverAddress = new ServerAddress("192.168.0.205", 27017);
MongoClient mg = new MongoClient(serverAddress, Arrays.asList(credential));
DB db = mg.getDB("WLCloud");
//查询所有的聚集集合
DBCollection users = db.getCollection("DeviceStatusLog");

DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date myDate2 = dateFormat2.parse("2016-05-03 00:00:00");

BasicDBObject queryObject = new BasicDBObject().append("DisplayTime",
new BasicDBObject()
.append(QueryOperators.LTE, new Date())
.append(QueryOperators.GTE, myDate2));

//查询所有的数据
DBCursor cur = users.find(queryObject);

List<DBObject> list = new LinkedList<DBObject>();
while (cur.hasNext()) {
list.add(cur.next());
}
System.out.println("11111111111111");
System.out.println(list.size());
System.out.println("===========");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: