spring 接入mongodb
程序员文章站
2022-04-02 23:18:51
...
引入jar
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.10.1.RELEASE</version>
</dependency>
</dependencies>
配置文件
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
<?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: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">
<!--credentials的配置形式是:用户名:密码@默认数据库 credentials="${mongo.username}:${mongo.password}@${mongo.dbname}"-->
<mongo:mongo-client id="mongoClient" host="${mongo.host}" port="${mongo.port}" >
<mongo:client-options write-concern="SAFE"
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>
<mongo:db-factory id="mongoDbFactory" dbname="${mongo.dbname}" mongo-ref="mongoClient" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>
</beans>
Dao定义
MongoBase
package com.suning.lcdcs.service.mongo;
import java.util.List;
import java.util.Map;
public interface MongoBase<T> {
//添加
public void insert(T object,String collectionName);
//根据条件查找
public T findOne(Map<String,Object> params,String collectionName);
//查找所有
public List<T> findAll(Map<String,Object> params,String collectionName);
//修改
public void update(Map<String,Object> params,String collectionName);
//创建集合
public void createCollection(String collectionName);
//根据条件删除
public void remove(Map<String,Object> params,String collectionName);
}
OrderInfoMongoDao.java
package com.suning.lcdcs.service.mongo;
import com.suning.fsp.lcdcs.order.OrderInfo;
public interface OrderInfoMongoDao extends MongoBase<OrderInfo>{
}
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<String, Object> params, String collectionName) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<OrderInfo> findAll(Map<String, Object> params, String collectionName) {
// TODO Auto-generated method stub
return null;
}
@Override
public void update(Map<String, Object> params, String collectionName) {
// TODO Auto-generated method stub
}
@Override
public void createCollection(String name) {
mongoTemplate.createCollection(name);
}
@Override
public void remove(Map<String, Object> 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");
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.10.1.RELEASE</version>
</dependency>
</dependencies>
配置文件
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
<?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: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">
<!--credentials的配置形式是:用户名:密码@默认数据库 credentials="${mongo.username}:${mongo.password}@${mongo.dbname}"-->
<mongo:mongo-client id="mongoClient" host="${mongo.host}" port="${mongo.port}" >
<mongo:client-options write-concern="SAFE"
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>
<mongo:db-factory id="mongoDbFactory" dbname="${mongo.dbname}" mongo-ref="mongoClient" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>
</beans>
Dao定义
MongoBase
package com.suning.lcdcs.service.mongo;
import java.util.List;
import java.util.Map;
public interface MongoBase<T> {
//添加
public void insert(T object,String collectionName);
//根据条件查找
public T findOne(Map<String,Object> params,String collectionName);
//查找所有
public List<T> findAll(Map<String,Object> params,String collectionName);
//修改
public void update(Map<String,Object> params,String collectionName);
//创建集合
public void createCollection(String collectionName);
//根据条件删除
public void remove(Map<String,Object> params,String collectionName);
}
OrderInfoMongoDao.java
package com.suning.lcdcs.service.mongo;
import com.suning.fsp.lcdcs.order.OrderInfo;
public interface OrderInfoMongoDao extends MongoBase<OrderInfo>{
}
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<String, Object> params, String collectionName) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<OrderInfo> findAll(Map<String, Object> params, String collectionName) {
// TODO Auto-generated method stub
return null;
}
@Override
public void update(Map<String, Object> params, String collectionName) {
// TODO Auto-generated method stub
}
@Override
public void createCollection(String name) {
mongoTemplate.createCollection(name);
}
@Override
public void remove(Map<String, Object> 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");