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);
}

插入成功

猜你喜欢

转载自www.cnblogs.com/cw828/p/11757616.html
今日推荐