1、首先还是在maven的pom.xml配置好所需的jar包:
<properties>
<spring.version>3.1.2.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.11.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.11.3</version>
</dependency>
</dependencies>
本来最开始用的3.0.2.RELEASE,后来貌似会出现一些冲突,然后就换用了3.1.2.RELEASE
2、配置spring的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/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
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">
<mongo:mongo id="mongo" host="127.0.0.1" port="27017" />
<!-- mongo的工厂,通过它来取得mongo实例,dbname为mongodb的数据库名,没有的话会自动创建 -->
<mongo:db-factory dbname="TiedooV2" mongo-ref="mongo"/>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
</bean>
<bean id="studentDao" class="com.zcx.dao.StudentDaoImpl" >
<property name="mongoTemplate" ref="mongoTemplate"/>
</bean>
<bean id="student" class="com.zcx.entiey.Student">
<constructor-arg name="name" value="will"/>
<constructor-arg name="sex" value="male"/>
<constructor-arg name="age" value="29"/>
</bean>
</beans>
注:最开始的mongoTemplate的构造方法不是用mongoDbFactory去实现,而是用的3参数的构造放方法,但始终报错,于是换成这种
3、Student,dao,daoImpl的写法。需要注意的是,daoImpl内的mongoTemplate需要加上setter方法,不然无法注入。
Student:
@Document(collection = "Student")
public class Student {
private String name;
private String sex;
private Integer age;
public Student() {}
public Student(String name, String sex, Integer age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "name:"+name+",sex:"+sex+",age:"+age;
}
}
IStudentDao:
public interface IStudentDao {
public void insert(Student s);
public Student findOne(String id);
public List<Student> findAll();
public List<Student> findByRegex(String regex);
public void removeOne(String id);
public void removeAll();
public void findAndModify(String id);
}
StudentDaoImpl:
public class StudentDaoImpl implements IStudentDao{
@Autowired
private MongoTemplate mongoTemplate;
@Override
public void insert(Student s) {
mongoTemplate.insert(s);
}
@Override
public Student findOne(String id) {
return mongoTemplate.findOne(new Query(Criteria.where("id").is(id)), Student.class);
}
@Override
public List<Student> findAll() {
return mongoTemplate.find(new Query(), Student.class);
}
@Override
public List<Student> findByRegex(String regex) {
Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
Criteria c = new Criteria("name").regex(pattern.toString());
return mongoTemplate.find(new Query(c), Student.class);
}
@Override
public void removeOne(String id) {
Criteria c = Criteria.where("id").is(id);
if(c == null) {
Query query = new Query(c);
Student s = mongoTemplate.findOne(query, Student.class);
if(query != null && s != null) {
mongoTemplate.remove(s);
}
}
}
@Override
public void removeAll() {
List<Student> list = mongoTemplate.find(new Query(), Student.class);
if(list.size() > 0) {
for(Student s : list) {
mongoTemplate.remove(s);
}
}
}
@Override
public void findAndModify(String id) {
// TODO Auto-generated method stub
}
public void setMongoTemplate(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}
}
4、编写测试类,此处用junit-4.7
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="/beans.xml")
public class SpringTest {// extends AbstractTransactionalJUnit4SpringContextTests{
@Autowired
public IStudentDao studentDao;
@Autowired
public Student student;
@Test
public void addTest() {
try {
studentDao.insert(student);
System.out.println("insert success");
} catch(Exception ex) {
System.out.println("insert fail");
ex.printStackTrace();
}
}
@Test
public void findAllTest() {
try {
List<Student> list = studentDao.findAll();
if(list.size() > 0 ) {
for(Student s : list) {
System.out.println(s.toString());
}
}
} catch(Exception ex ) {
ex.printStackTrace();
System.out.println("findAll fail");
}
}
@SuppressWarnings("resource")
public void init() {
try {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/beans.xml");
studentDao = applicationContext.getBean("studentDao",IStudentDao.class);
student = applicationContext.getBean("student",Student.class);
System.out.println(student.toString());
} catch(Throwable ex) {
ex.printStackTrace();
}
}
}
注:init()方法是自己手动实例化applicationContext之后再手动获取dao,student对象。如果需要尝试手动,需在该方法前加上@Before注解
需要加上 @RunWith(SpringJUnit4ClassRunner.class) ,但不明白啥意思。
5、运行测试,成功。