ssm integrated mongodb

Taken 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- 4.0.xsd-MVC ">

! <- read jdbc.properties ->
<context: Property-placeholder LOCATION =" the CLASSPATH: mongodb.properties "the ignore-unresolvable =" to true "/>

<- we use! mongodb version is 3.X,Therefore, in the construction of this passage of time to be constructed according to the constructor Mongo class, different versions may result in different constructors ->
<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 plant through which to get mongo example, dbname is the database name mongodb of, if not automatically created ->
<bean the above mentioned id = "mongoDbFactory"
class = "org.springframework.data.mongodb.core. SimpleMongoDbFactory ">
<REF = Arg-constructor" Mongo "/>
<constructor-Arg value =" $ {mongo.dbname} "/>
</ the bean>

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

<the bean ID =" defaultMongoTypeMapper "
class =" org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper ">
<constructor Arg-name =" TypeKey ">
<null />
< / Arg-constructor>
</ the bean>
<!- collection of mappings ->
<the 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" />
</ the bean>
<-! mongodb main target of the operation, all of the deletions of mongodb change search operations are completed by it ->
<the 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 integration

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

Insert success

 

 

Guess you like

Origin www.cnblogs.com/cw828/p/11757616.html