zoukankan      html  css  js  c++  java
  • ssm集成mongodb

    摘自https://blog.csdn.net/weixin_43923112/article/details/86676643

    mongodb.properties

    #DB name
    mongo.dbname=mycol
    #username
    mongo.username=
    #password
    mongo.password=
    #host
    mongo.host=***.***.*.**
    #port
    mongo.port=****
    #xiancheng zui da zu se shu
    mongo.connectionsPerHost=8
    #xiancheng dui lie shu
    mongo.threadsAllowedToBlockForConnectionMultiplier=4
    #connectTimeout ms
    mongo.connectTimeout=1500
    #maxWaitTime
    mongo.maxWaitTime=1500
    #autoConnect
    mongo.autoConnectRetry=true
    #socketKeepAlive
    mongo.socketKeepAlive=true
    #socketTimeout
    mongo.socketTimeout=1500
    #du xie fen li
    mongo.slaveOk=true

    applicationContext-mongodb.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:mongo="http://www.springframework.org/schema/data/mongo" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/data/mongo
    http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <!-- 读取jdbc.properties -->
    <context:property-placeholder location="classpath:mongodb.properties" ignore-unresolvable="true"/>

    <!-- 我们使用的mongodb的版本是3.X,所以在构造这段话的时候要根据 Mongo 类的构造函数来构造,不同版本可能会造成构造函数的不同 -->
    <mongo:mongo-client id="mongo" 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-keep-alive="${mongo.socketKeepAlive}" socket-timeout="${mongo.socketTimeout}" />
    </mongo:mongo-client>
    <!-- 用户验证 -->
    <bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials">
    <constructor-arg name="username" value="${mongo.username}" />
    <constructor-arg name="password" value="${mongo.password}" />
    </bean>
    <!-- mongo的工厂,通过它来取得mongo实例,dbname为mongodb的数据库名,没有的话会自动创建 -->
    <bean id="mongoDbFactory"
    class="org.springframework.data.mongodb.core.SimpleMongoDbFactory">
    <constructor-arg ref="mongo" />
    <constructor-arg value="${mongo.dbname}" />
    </bean>

    <bean id="mappingContext"
    class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />

    <bean id="defaultMongoTypeMapper"
    class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
    <constructor-arg name="typeKey">
    <null />
    </constructor-arg>
    </bean>
    <!-- collection的映射 -->
    <bean id="mappingMongoConverter"
    class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    <constructor-arg name="mappingContext" ref="mappingContext" />
    <property name="typeMapper" ref="defaultMongoTypeMapper" />
    </bean>
    <!-- mongodb的主要操作对象,所有对mongodb的增删改查的操作都是通过它完成 -->
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    <constructor-arg name="mongoConverter" ref="mappingMongoConverter" />
    </bean>

    </beans>

    pom整合

    <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-mongodb</artifactId>
    <version>1.9.8.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>2.14.2</version>
    </dependency>

    简单使用一下

    @Service
    public class TestMongodbServerImpl implements TestMongodbServer {
    @Autowired
    private MongoOperations mongoOperations;

    @Override
    public void test() {
    List<WxRes> list = new ArrayList<WxRes>();
    WxRes wx = new WxRes();
    WxRes wx2 = new WxRes();
    wx.setId("111111");
    wx.setOrderId("22222");
    list.add(wx);
    wx2.setId("33333");
    wx2.setOrderId("44444");
    list.add(wx2);
    mongoOperations.insert(list, WxRes.class);
    }

    插入成功

    增删查改:

    //save存在则更新,不存在则插入
    @Override
    public void save(WxRes wxRes) {
    WxRes wx = new WxRes();
    wx.setId("111111");
    wx.setOrderId("2222233");
    mongoOperations.save(wx);
    }

    //insert存在则不做处理,不存在则插入
    @Override
    public void insert(WxRes wxRes) {
    WxRes wx = new WxRes();
    wx.setId("111111");
    wx.setOrderId("2222233");
    mongoOperations.insert(wx);
    }

    //根据userid删除文档
    @Override
    public void remove(String uid) {
    mongoOperations.remove("111111");
    }

    //根据id更新文档
    @Override
    public int update(WxRes wxRes) {

    Query qu = new Query();
    qu.addCriteria(Criteria.where("id").is("111111"));
    Update up = new Update();
    up.set("id", "8888888");
    // int n = mongoOperations.updateFirst(qu, up, WxRes.class).getN();//只修改第一个
    int n2 = mongoOperations.updateMulti(qu, up, WxRes.class).getN();//修改所有
    return n2;

    }

    //单条件查询,find查询所有
    @Override
    public List<WxRes> findByUid(String uid) {
    Query qu = new Query();
    qu.addCriteria(Criteria.where("id").is("33333"));
    return mongoOperations.find(qu,WxRes.class);
    }

    //多条件查询,find查询所有
    @Override
    public List<WxRes> findByUidAndTimestamp(String uid, String name) {
    Query qu = new Query();
    qu.addCriteria(Criteria.where("id").is("33333").and("orderId").is("44444"));
    return mongoOperations.find(qu, WxRes.class);
    }

    //根据uid分页查询
    @Override
    public List<WxRes> page(int page, String uid) {
    Query qu = new Query();
    qu.addCriteria(Criteria.where("id").is("33333"));
    qu.with(new Sort(new Sort.Order(Sort.Direction.DESC,"id")));
    qu.limit(5);
    qu.skip((page-1)*5);
    return mongoOperations.find(qu, WxRes.class);
    }

  • 相关阅读:
    给博客园编辑器完善个插件及简单产品化工作
    在Visual Studio中新增生成项目
    用了三星Dex,我已经快一个月回家没开过电脑了
    BizTalk证书相关操作
    定长文本格式编辑神器
    B2B相关编码说明
    OFTP简介
    Apigee 简介与简单试用
    重置BizTalk RosettaNet
    BizTalk Map 累积连接字符串
  • 原文地址:https://www.cnblogs.com/cw828/p/11757616.html
Copyright © 2011-2022 走看看