Here is a reference to the article: https://www.cnblogs.com/dennisit/p/3372568.html https://www.cnblogs.com/TinyBobo/p/7867208.html/http limingnihao.iteye.com/blog/1940446
We recommend that you look at the second connection
First introduce the JAR file
Mongo-java-driver-3.5.0.jar
Spring-core-4.3.5.release.jar
Spring-data-commons-1.13.9.release.jar
Spring-data-commons-core-1.4.1.release.jar
Spring-data-mongodb-1.8.4.release.jar
Mongodb.properties
mongodb.host= Database IP Address
mongodb.port=27017
Mongodb.connectionsperhost=8
Mongodb.threadsallowedtoblockforconnectionmultiplier=4
Mongodb.databasename=admin
mongo.user= User Name
mongo.pwd= Password
#
mongodb.connecttimeout=10000
mongodb.maxwaittime=120000
Mongodb.autoconnectretry=true
Mongodb.socketkeepalive=true
Mongodb.sockettimeout=0
Mongodb.slaveok=true
Mongodb.writenumber=1
Mongodb.writetimeout=0
Mongodb.writefsync=true
Then create 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"
Xsi:schemalocation= "Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context.xsd
Http://www.springframework.org/schema/data/mongo
Http://www.springframework.org/schema/data/mongo/spring-mongo-2.0.xsd
Http://www.springframework.org/schema/data/repository
Http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd
Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans.xsd ">
<!--<context:property-placeholder location= "Classpath:mongodb.properties"/>--
<mongo:mongo-client id= "mongoclient" host= "${mongodb.host}" port= "${mongodb.port}"
credentials= "${mongo.user}:${mongo.pwd}@${mongodb.databasename}" >
<!--MONGO Connection properties--
<mongo:client-options
Connections-per-host= "${mongodb.connectionsperhost}"
Threads-allowed-to-block-for-connection-multiplier= "${mongodb.threadsallowedtoblockforconnectionmultiplier}"
connect-timeout= "${mongodb.connecttimeout}"
Max-wait-time= "${mongodb.maxwaittime}"
Socket-keep-alive= "${mongodb.socketkeepalive}"
socket-timeout= "${mongodb.sockettimeout}"
/>
</mongo:mongo-client>
<mongo:db-factory id= "Mongodbfactory" Dbname= "${mongodb.databasename}" mongo-ref= "MongoClient"/>
<bean id= "Mongotemplate" class= "Org.springframework.data.mongodb.core.MongoTemplate" >
<constructor-arg ref= "Mongodbfactory"/>
</bean>
<!--MONGO return value entity class path--
<mongo:mapping-converter base-package= "Com.ssm.model"/>
<!--How to manipulate the MongoDB database method file path--
<mongo:repositories base-package= "Com.ssm.service.Impl"/>
<context:annotation-config/>
</beans>
Next, to introduce the configuration <import resource= "Classpath*:config/spring-mongo.xml" in the Spring.xml file/>
Then create the entity class package Com.ssm.model;
Import Org.springframework.data.annotation.Id;
Import org.springframework.data.mongodb.core.mapping.Document;
@Document (collection = "Test")//Here test refers to the name of the table
public class Mongotest {
@Id
Private String ID;
Private String title;
Private String description;
Private String by;
Private String URL;
Private double likes;
Public String getId () {
return ID;
}
public void SetId (String id) {
This.id = ID;
}
Public String GetTitle () {
return title;
}
public void Settitle (String title) {
This.title = title;
}
Public String getdescription () {
return description;
}
public void SetDescription (String description) {
this.description = description;
}
Public String Getby () {
return by;
}
public void Setby (String by) {
This.by = by;
}
Public double Getlikes () {
return likes;
}
public void Setlikes (double likes) {
This.likes = likes;
}
Public String GetUrl () {
return URL;
}
public void SetUrl (String URL) {
This.url = URL;
}
}
Concrete implementation of the class package Com.ssm.service.Impl;
Import java.util.List;
Import org.springframework.beans.factory.annotation.Autowired;
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.ssm.model.MongoTest;
Import Com.ssm.service.MongoService;
@Repository
public class Mongoserviceimpl implements Mongoservice {
@Autowired (required = false)
Private Mongotemplate mongotemplate;
/** Query All Logs */
Public list<mongotest> FindAll () {
Return Mongotemplate.findall (Mongotest.class);
}
/** Save Log **/
public void Save (Mongotest mongotest) {
Mongotemplate.save (mongotest);
}
/** query log by Title */
Public list<mongotest> Selecttitle (String title) {
Query query=new query (criteria.where ("title"). Is (title));
List<mongotest> list=mongotemplate.find (query, Mongotest.class);
return list;
}
}
Implement class interface package com.ssm.service;
Import java.util.List;
Import Com.ssm.model.MongoTest;
Public interface Mongoservice {
List<mongotest> FindAll ();
public void Save (Mongotest t);
Public list<mongotest> Selecttitle (String title);
}
The JUnit test package com.test is used here;
Import java.util.List;
Import Org.apache.log4j.Logger;
Import Org.junit.Test;
Import Org.junit.runner.RunWith;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.test.context.ContextConfiguration;
Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
Import Com.alibaba.fastjson.JSONObject;
Import Com.ssm.model.MongoTest;
Import Com.ssm.service.MongoService;
@RunWith (Springjunit4classrunner.class)
@ContextConfiguration (locations={"Classpath*:spring.xml", "Classpath*:spring-mybatis.xml", "Classpath: Spring-mongo.xml "})
public class Test1 {
Logger logger= (Logger) Logger.getlogger (This.getclass ());
@Autowired
Mongoservice Mongoservice;
Querying all logs
@Test
public void GetALl () {
List List=mongoservice.findall ();
Logger.info (jsonobject.tojsonstring (list));
}
Add New log
@Test
public void Save () {
Mongotest mongotest=new mongotest ();
Mongotest.settitle ("log title 33");
Mongotest.setdescription ("description 33");
Mongotest.setby ("source 33");
Mongotest.seturl ("Www.fdfd.com33");
Mongotest.setlikes (400);
Mongoservice.save (mongotest);
}
Query by Title
@Test
public void Getby () {
String title= "Journal header 3";
List<mongotest> List=mongoservice.selecttitle (title);
Logger.info (jsonobject.tojsonstring (list));
}
}
The following test saves first
Looks like there's no reaction. Look at the database
Data has been inserted below execute getall (), query all
Then test the condition query @Test
public void Getby ()
Done