1.导入相关的jar包
- aopalliance-1.0.jar
- commons-logging-1.1.1.jar
- junit-4.7.jar
- log4j-1.2.15.jar
- mongo-java-driver-2.12.1.jar
- slf4j-api-1.6.1.jar
- slf4j-nop-1.7.7.jar
- spring-aop-3.2.8.RELEASE.jar
- spring-beans-3.2.8.RELEASE.jar
- spring-context-3.2.8.RELEASE.jar
- spring-core-3.2.8.RELEASE.jar
- spring-data-commons-1.8.0.RELEASE.jar
- spring-data-mongodb-1.5.0.RELEASE.jar
- spring-expression-3.2.8.RELEASE.jar
- spring-test-3.2.8.RELEASE.jar
- spring-tx-3.2.8.RELEASE.jar
2.创建mongodb配置文件mongodb.properties
mongo.dbname = cdb
mongo.password =
mongo.username =
mongo.host = localhost
mongo.port = 27017
mongo.connectionsPerHost=8
mongo.threadsAllowedToBlockForConnectionMultiplier=4
mongo.connectTimeout=1500
mongo.maxWaitTime=1500
mongo.autoConnectRetry=true
mongo.socketKeepAlive=true
mongo.socketTimeout=1500
mongo.slaveOk=true
3.创建spring的配置文件applicationContext.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-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 加载 mongodb配置文件-->
<context:property-placeholder location="classpath:mongodb.properties" />
<!--自动扫描-->
<context:component-scan base-package="com.ms"/>
<!-- repository -->
<mongo:repositories base-package="com.ms.repository" />
<!-- mongo -->
<mongo:mongo id="mongo" host="${mongo.host}" port="${mongo.port}" />
<!-- mongoDbFactory -->
<mongo:db-factory id="mongoDbFactory" mongo-ref="mongo" dbname="${mongo.dbname}" />
<!-- mongoTemplate -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>
<!-- mongoRepositoryFactory -->
<bean id="mongoRepositoryFactory"
class="org.springframework.data.mongodb.repository.support.MongoRepositoryFactory">
<constructor-arg name="mongoOperations" ref="mongoTemplate" />
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<param name="Threshold" value="ALL" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss:SSS} %p][%l] %m%n"/>
</layout>
</appender>
<root>
<priority value="debug" />
<appender-ref ref="CONSOLE" />
</root>
</log4j:configuration>
5.创建包和类
com.ms.domain 实体类存放的包
com.ms.repository repository存放的包,相当于dao层
在domain包下创建User.java
package com.ms.domain;在repository下创建接口UserRepository.java ,需要继承MongoRepository:
public class User {
private String id;
private String name;
private String email;
public User(String name, String email) {
super();
this.name = name;
this.email = email;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", email=" + email + "]";
}
}
package com.ms.repository;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.query.Param;
import com.ms.domain.User;
public interface UserRepository extends MongoRepository<User, String> {
User findById(@Param("id") final String id);
}
6.新建测试类
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Assert;
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.ms.domain.User;
import com.ms.repository.UserRepository;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class UserTest {
private static final Log log = LogFactory.getLog(UserTest.class);
@Autowired
private UserRepository repository;
@Test
public void test(){
Assert.assertNotNull(repository);
User user = new User("zhangsan","zhangsan@126.com");
//save
repository.save(user);
log.info("save : " + user);
//findOne
user = repository.findById(user.getId());
log.info("findOne : " + user);
//update
user.setEmail("chain@hotmail.com");
user = repository.save(user);
user = repository.findById(user.getId());
log.info("update : " + user);
//delete
repository.delete(user);
log.info("delete : " + repository.count());
}
}
最后Run As ---->junit test,如果配置没问题将看到如下输出:
[UserTest.test(UserTest.java:28)] save : User [id=54432fe67c1f864795c9f72a, name=zhangsan, email=zhangsan@126.com]
[UserTest.test(UserTest.java:32)] findOne : User [id=54432fe67c1f864795c9f72a, name=zhangsan, email=zhangsan@126.com]
[UserTest.test(UserTest.java:38)] update : User [id=54432fe67c1f864795c9f72a, name=zhangsan, email=chain@hotmail.com]
[UserTest.test(UserTest.java:42)] delete : 0
(运行环境是在jdk7下面,jdk8下面需要更换spring4)