<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zjf</groupId>
<artifactId>springmvc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<spring.webmvc.version>4.3.0.RELEASE</spring.webmvc.version>
<junit.version>4.8.2</junit.version>
<jedis.version>2.8.1</jedis.version>
<log4j.version>1.2.16</log4j.version>
<slf4j.version>1.7.2</slf4j.version>
<spring.data.redis.version>1.7.2.RELEASE</spring.data.redis.version>
<spring.data.mongodb.version>1.8.0.RELEASE</spring.data.mongodb.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.webmvc.version}</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>${spring.data.redis.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>${spring.data.mongodb.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>
</project>
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.2.xsd ">
<context:component-scan base-package="com.zjf.*" />
<!-- 属性文件位置 -->
<bean id="annotationPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config/properties/mongodb.properties</value>
<value>classpath:config/properties/redis.properties</value>
</list>
</property>
</bean>
</beans>
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<!-- 定义mongo对象,对应的是mongodb官方jar包中的Mongo,replica-set设置集群副本的ip地址和端口 -->
<mongo:mongo id="mongo" replica-set="${mongo.hostport}">
<!-- 一些连接属性的设置 -->
<mongo:options connections-per-host="${mongo.connectionsPerHost}"
threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
connect-timeout="${mongo.connectTimeout}" max-wait-time="${mongo.maxWaitTime}"
auto-connect-retry="${mongo.autoConnectRetry}" socket-keep-alive="${mongo.socketKeepAlive}"
socket-timeout="${mongo.socketTimeout}" slave-ok="${mongo.slaveOk}"
write-number="1" write-timeout="0" write-fsync="true" />
</mongo:mongo>
<!-- dbname是要操作的数据库 -->
<mongo:db-factory id="myMongoDbFactory" dbname="mydb" mongo-ref="mongo" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="myMongoDbFactory" />
</bean>
</beans>
package com.zjf.spring.mongodb.dao.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;
import com.mongodb.WriteResult;
import com.zjf.spring.mongodb.dao.StudentDao;
import com.zjf.spring.mongodb.model.Student;
//持久层注解
@Repository
public class StudentDaoImpl implements StudentDao {
//引入mongo.xml中配置的mongoTemplate
@Autowired
private MongoOperations mongoTemplate;
public void add(Student student)
{
this.mongoTemplate.insert(student);
}
public Student getById(String id)
{
return this.mongoTemplate.findById(id, Student.class);
}
public void modify(Student student)
{
this.mongoTemplate.save(student);
}
public void delete(Student student)
{
this.mongoTemplate.remove(student);
}
public void deleteById(String id)
{
this.mongoTemplate.remove(new Query(Criteria.where("_id").is(id)), Student.class);
}
//一个根据名字查询结果的方法
@Override
public List<Student> getByName(String name) {
List<Student> students = new ArrayList<Student>();
Query query = new Query();
query.addCriteria(new Criteria("name").is(name));
students = this.mongoTemplate.find(query, Student.class);
return students;
}
////一个根据名字更新数据方法
@Override
public int updateByName(String name) {
int n = 0;
List<Student> students = new ArrayList<Student>();
Query query = new Query();
query.addCriteria(new Criteria("name").is(name));
Update update = new Update();
update.set("name", "xhj");
WriteResult result = this.mongoTemplate.updateMulti(query, update, Student.class);
n = result.getN();
return n;
}
}