1. 程式人生 > >Mongodb ssm框架專案基本操作

Mongodb ssm框架專案基本操作

Mongdb連線到java專案中:

mongodb在普通ssm專案中配置檔案:

在resourse包中建立兩個檔案mongodb-context.xml檔案 : 1.

<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:cache="http://www.springframework.org/schema/cache" 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-4.3.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-4.3.xsd
          http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
          http://www.springframework.org/schema/cache
          http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
          http://www.springframework.org/schema/data/mongo
          http://www.springframework.org/schema/data/mongo/spring-mongo-1.10.xsd">

    
    <mongo:db-factory id="mongoDbFactory" dbname="${mongo.dbname}"
        mongo-ref="mongoClient" />

    <!--credentials的配置形式是:使用者名稱:密碼@預設資料庫 -->
    <!-- <mongo:mongo-client id="mongoClient" host="${mongo.host}" port="${mongo.port}" 
        credentials="${mongo.username}:${mongo.password}@${mongo.dbname}" > -->
    <mongo:mongo-client id="mongoClient" host="${mongo.host}"
        port="${mongo.port}">
        <mongo:client-options connections-per-host="${mongo.connectionsPerHost}"
            threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
            connect-timeout="${mongo.connectTimeout}" max-wait-time="${mongo.maxWaitTime}"
            socket-timeout="${mongo.socketTimeout}" />
    </mongo:mongo-client>
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    </bean>

</beans>

2.  mongodb.properties

#mongoDB連線配置  
mongo.dbname=sxxnh
mongo.host=localhost  
mongo.port=27017  
#mongo.username=root  
#mongo.password=root  
#一個執行緒變為可用的最大阻塞數  
  
#mongo.writeConcern=SAFE  
mongo.connectionsPerHost=8  
#執行緒佇列數,它以上面connectionsPerHost值相乘的結果就是執行緒佇列最大值  
mongo.threadsAllowedToBlockForConnectionMultiplier=4  
#連線超時時間(毫秒)  
mongo.connectTimeout=1500  
#最大等待時間  
mongo.maxWaitTime=1500  
#自動重連  
mongo.autoConnectRetry=true  
#scoket保持活動  
mongo.socketKeepAlive= true  
#scoket超時時間  
mongo.socketTimeout=1500  

maven  依賴

        <dependency>  
            <groupId>org.springframework.data</groupId>  
            <artifactId>spring-data-mongodb</artifactId>  
            <version>1.10.7.RELEASE</version>  
        </dependency> 

建立Mongodb連線工具類: 其中部分方法使用時需要修改

import org.springframework.beans.factory.annotation.Qualifier;
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.stereotype.Repository;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

import io.renren.modules.MongoDB.entity.MongoDBUser;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Repository(value="mongoDBUserDaoImpl")
public class MongoDBUserDaoImpl implements MongoDBUserDao {
    @Resource
    @Qualifier("mongoTemplate")
    private MongoTemplate mongoTemplate;

    @Override
    public void insert(Object object, String collectionName) {
        mongoTemplate.insert(object, collectionName);
    }

    @Override
    public MongoDBUser findOne(Map<String, Object> params, String collectionName) {
        return mongoTemplate.findOne(new Query(Criteria.where("id").is(params.get("id"))), MongoDBUser.class,
                collectionName);
    }

    public List<BasicDBObject> findAllList(String collectionName) {
        return mongoTemplate.findAll(BasicDBObject.class, collectionName);
    }
    @Override
    public List<MongoDBUser> findAll(String collectionName) {
        return mongoTemplate.findAll(MongoDBUser.class, collectionName);
    }

    @Override
    public void update(Map<String, Object> params, String collectionName) {
        Update update = new Update();
        if (null != params.get("username")) {
            update.set("username", params.get("username"));
        }
        if (null != params.get("password")) {
            update.set("password", params.get("password"));
        }
        mongoTemplate.upsert(new Query(Criteria.where("id").is(params.get("id"))), update, MongoDBUser.class,
                collectionName);
    }

    @Override
    public void createCollection(String collectionName) {
        mongoTemplate.createCollection(collectionName);
    }

    @Override
    public void remove(Map<String, Object> params, String collectionName) {
        mongoTemplate.remove(new Query(Criteria.where("id").is(params.get("id"))), MongoDBUser.class, collectionName);
    }
    
    @Override
    public DBCollection getCollection(String collectionName) {
       
        return  mongoTemplate.getCollection(collectionName);
    }
    @Override
    public void dropCollection(String collectionName) {
          mongoTemplate.dropCollection(collectionName);
    }

}