spring 接入mongodb

引入jar


org.mongodb
mongo-java-driver
3.6.1


org.springframework.data
spring-data-mongodb
1.10.1.RELEASE



配置文件
mongo.properties
mongo.dbname=lcdcs
mongo.host=10.27.210.198
mongo.port=27017
mongo.username=robotassister
mongo.password=xxyyyxxxxx
#一个线程变为可用的最大阻塞数
mongo.connectionsPerHost=8
#线程队列数,它以上面connectionsPerHost值相乘的结果就是线程队列最大值
mongo.threadsAllowedToBlockForConnectionMultiplier=4
#连接超时时间(毫秒)
mongo.connectTimeout=1500
#最大等待时间
mongo.maxWaitTime=1500
#自动重连
mongo.autoConnectRetry=true
#scoket保持活动
mongo.socketKeepAlive= true
#scoket超时时间
mongo.socketTimeout=1500
#读写分离
mongo.slaveOk=true


XML文件
spring-mongo.xml


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.7.xsd
http://www.springframework.org/schema/data/repository
http://www.springframework.org/schema/data/repository/spring-repository-1.7.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.2.xsd"
default-lazy-init="default">



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}"/>










Dao定义
MongoBase
package com.suning.lcdcs.service.mongo;

import java.util.List;
import java.util.Map;

public interface MongoBase {
//添加
public void insert(T object,String collectionName);
//根据条件查找
public T findOne(Map params,String collectionName);
//查找所有
public List findAll(Map params,String collectionName);
//修改
public void update(Map params,String collectionName);
//创建集合
public void createCollection(String collectionName);
//根据条件删除
public void remove(Map params,String collectionName);

}


OrderInfoMongoDao.java
package com.suning.lcdcs.service.mongo;

import com.suning.fsp.lcdcs.order.OrderInfo;

public interface OrderInfoMongoDao extends MongoBase{

}


package com.suning.lcdcs.service.mongo;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

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.stereotype.Repository;

import com.suning.fsp.lcdcs.order.OrderInfo;
@Repository("orderInfoMongoDao")
public class OrderInfoMongoDaoImpl implements OrderInfoMongoDao {
@Resource
private MongoTemplate mongoTemplate;
@Override
public void insert(OrderInfo object, String collectionName) {
mongoTemplate.insert(object, collectionName);
}

@Override
public OrderInfo findOne(Map params, String collectionName) {
// TODO Auto-generated method stub
return null;
}

@Override
public List findAll(Map params, String collectionName) {
// TODO Auto-generated method stub
return null;
}

@Override
public void update(Map params, String collectionName) {
// TODO Auto-generated method stub

}

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

@Override
public void remove(Map params, String collectionName) {
mongoTemplate.remove(new Query(Criteria.where("id").is(params.get("id"))),OrderInfo.class,collectionName);
}

}


package com.suning.fsp.lcdcs.order;

import java.math.BigDecimal;

import org.springframework.data.mongodb.core.mapping.Document;
@Document
public class OrderInfo {


private java.lang.String id;//
private java.lang.String orderNo;// 订单号
private java.lang.String accountNo;// 户头号
private java.lang.Long channelId;// 渠道ID
private BigDecimal orderAmount;// 订单金额
private java.lang.String orderStatus;// 订单状态
private java.util.Date createTime;// 创建时间
private java.util.Date updateTime;// 更新时间@Id
private String source;//1:app 2:微信
private String productId;//产品id
private String productName;//产品名称
private String realName;//用户姓名
private String telPhone;//电话号码
private String idCard;//身份证
private String orderType;//订单类型



/**
* @return the id
*/
public java.lang.String getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(java.lang.String id) {
this.id = id;
}
public java.lang.String getOrderNo() {
return this.orderNo;
}
public void setOrderNo(java.lang.String orderNo) {
this.orderNo=orderNo;
}
public java.lang.String getAccountNo() {
return this.accountNo;
}
public void setAccountNo(java.lang.String accountNo) {
this.accountNo=accountNo;
}
public java.lang.Long getChannelId() {
return this.channelId;
}
public void setChannelId(java.lang.Long channelId) {
this.channelId=channelId;
}
public BigDecimal getOrderAmount() {
return this.orderAmount;
}
public void setOrderAmount(BigDecimal orderAmount) {
this.orderAmount=orderAmount;
}
public java.lang.String getOrderStatus() {
return this.orderStatus;
}
public void setOrderStatus(java.lang.String orderStatus) {
this.orderStatus=orderStatus;
}
public java.util.Date getCreateTime() {
return this.createTime;
}
public void setCreateTime(java.util.Date createTime) {
this.createTime=createTime;
}
public java.util.Date getUpdateTime() {
return this.updateTime;
}
public void setUpdateTime(java.util.Date updateTime) {
this.updateTime=updateTime;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public String getTelPhone() {
return telPhone;
}
public void setTelPhone(String telPhone) {
this.telPhone = telPhone;
}
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
public String getOrderType() {
return orderType;
}
public void setOrderType(String orderType) {
this.orderType = orderType;
}

}


service中引入
@Autowired
private OrderInfoMongoDao orderMongoDao;
插入mongodb数据库
orderMongoDao.insert(mongoOrder, "order_info");

你可能感兴趣的:(mongodb)