建议大家看看第二个连接
首先引入jar文件
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=数据库ip地址
mongodb.port=27017
mongodb.connectionsPerHost=8
mongodb.threadsAllowedToBlockForConnectionMultiplier=4
mongodb.databasename=admin
mongo.user=用户名
mongo.pwd=密码
#
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
mongodb.port=27017
mongodb.connectionsPerHost=8
mongodb.threadsAllowedToBlockForConnectionMultiplier=4
mongodb.databasename=admin
mongo.user=用户名
mongo.pwd=密码
#
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
然后创建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连接属性 -->
<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返回值实体类路径 -->
<mongo:mapping-converter base-package="com.ssm.model" />
<!-- 具体操作mongodb数据库的方法文件路径 -->
<mongo:repositories base-package="com.ssm.service.Impl"/>
<context:annotation-config />
</beans>
<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连接属性 -->
<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返回值实体类路径 -->
<mongo:mapping-converter base-package="com.ssm.model" />
<!-- 具体操作mongodb数据库的方法文件路径 -->
<mongo:repositories base-package="com.ssm.service.Impl"/>
<context:annotation-config />
</beans>
接着,要在spring.xml文件中引入配置
<import resource="classpath*:config/spring-mongo.xml"/>
然后创建实体类
package com.ssm.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "test") //这里test指表名
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;
}
}
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "test") //这里test指表名
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;
}
}
具体实现类
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;
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;
/**查询所有日志*/
public List<MongoTest> findAll(){
return mongoTemplate.findAll(MongoTest.class);
}
/**保存日志**/
public void save(MongoTest mongoTest){
mongoTemplate.save(mongoTest);
}
public void save(MongoTest mongoTest){
mongoTemplate.save(mongoTest);
}
/**根据标题查询日志*/
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;
}
}
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;
}
}
实现类接口
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);
}
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);
}
这里使用junit测试
package com.test;
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;
//查询所有日志
@Test
public void getALl(){
List list=mongoService.findAll();
logger.info(JSONObject.toJSONString(list));
}
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;
//查询所有日志
@Test
public void getALl(){
List list=mongoService.findAll();
logger.info(JSONObject.toJSONString(list));
}
//添加新日志
@Test
public void save(){
MongoTest mongoTest=new MongoTest();
mongoTest.setTitle("日志标题33");
mongoTest.setDescription("描述33");
mongoTest.setBy("来源33");
mongoTest.setUrl("www.fdfd.com33");
mongoTest.setLikes(400);
mongoService.save(mongoTest);
}
@Test
public void save(){
MongoTest mongoTest=new MongoTest();
mongoTest.setTitle("日志标题33");
mongoTest.setDescription("描述33");
mongoTest.setBy("来源33");
mongoTest.setUrl("www.fdfd.com33");
mongoTest.setLikes(400);
mongoService.save(mongoTest);
}
//根据标题查询
@Test
public void getBy(){
String title="日志标题3";
List<MongoTest> list=mongoService.selectTitle(title);
logger.info(JSONObject.toJSONString(list));
}
}
@Test
public void getBy(){
String title="日志标题3";
List<MongoTest> list=mongoService.selectTitle(title);
logger.info(JSONObject.toJSONString(list));
}
}
下面先测试保存
貌似没啥反应
看看数据库
已经插入数据了
下面执行getALl(),查询所有
接着测试条件查询
@Test
public void getBy()
public void getBy()
大功告成