日期:2014-05-16 浏览次数:20409 次
1.环境
Jdk:1.6.0_10-rc2
Spring3.1.2 下载
依赖jar文件:
2.相关配置
①.Spring配置文件
<?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" /> <bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials"> <constructor-arg name="username" value="userName"/> <constructor-arg name="password" value="password"/> </bean> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg ref="mongo" /> <constructor-arg value="test-mongo" /> <constructor-arg ref="userCredentials" /> </bean> <bean id="userDao" class="com.x.mongodb.dao.impl.UserDao" /> </beans>
②.MongoTemplate Api
package com.x.mongodb.dao.support; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.data.mongodb.core.MongoTemplate; /** * @author <a href="http://blog.csdn.net/java2000_wl">java2000_wl</a> * @version <b>1.0</b> */ public abstract class AbstractBaseMongoTemplete implements ApplicationContextAware { protected MongoTemplate mongoTemplate; /** * 设置mongoTemplate * @param mongoTemplate the mongoTemplate to set */ public void setMongoTemplate(MongoTemplate mongoTemplate) { this.mongoTemplate = mongoTemplate; } public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { MongoTemplate mongoTemplate = applicationContext.getBean("mongoTemplate", MongoTemplate.class); setMongoTemplate(mongoTemplate); } }
package com.x.mongodb.dao.impl; import java.util.List; 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 com.x.mongodb.dao.IUserDao; import com.x.mongodb.dao.support.Abstract