日期:2014-05-16  浏览次数:20515 次

Mongo的ORM框架的学习Morphia(十四)Mongo+spring-data-mongodb整合

????????? 本文通过Spring官方提供的Spring-data-mongodb实现和Mongo的整合应用实例。代码实现如下:

package com.easyway.spring.mongodb.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.config.AbstractMongoConfiguration;

import com.mongodb.Mongo;
/**
 * 
 * 使用 AbstractMongoConfiguration类,通过注解方法实现相关的配置
 * 
 * 
 * @Title: 以注解的方式使用AbstractMongoConfiguration
 * @Description: 实现TODO
 * @Copyright:Copyright (c) 2011
 * @Company:易程科技股份有限公司
 * @Date:2012-3-3
 * @author 
 * @version 1.0
 */
@Configuration
public class SpringMongoConfig extends AbstractMongoConfiguration {
 
	@Override
	public @Bean Mongo mongo() throws Exception {
		return new Mongo("localhost");
	}
	@Override
	public @Bean MongoTemplate mongoTemplate() throws Exception {
 
		return new MongoTemplate(mongo(),"test","mongo-user");
	}
 
}

?

?

model层:

package com.easyway.spring.mongodb.domain;
import java.io.Serializable;
import java.util.Date;
/**
 * 
 * @Title: 存储实体类公共的属性便于子类继承
 * @Description: 实现TODO
 * @Copyright:Copyright (c) 2011
 * @Company:易程科技股份有限公司
 * @Date:2012-3-3
 * @author 
 * @version 1.0
 */
public class Entity  implements Serializable{
	 /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 数据生成时间
	 */
	private Date createTime;

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	 
	

}

?

?

package com.easyway.spring.mongodb.domain;
/**
 * 实体类
 * @author longgangbai
 *
 */
public class UserInfo  extends Entity{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String userName;
	private Integer age;
	private String companyName;
	private String position;

	public UserInfo(){
		
	}
	/**
	 * 
	 * @param userName
	 * @param age
	 * @param companyName
	 * @param position
	 */
	public UserInfo(String userName,String companyName,String position,Integer age){
		this.userName=userName;
		this.age=age;
		this.companyName=companyName;
		this.position=position;
	}
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public String getCompanyName() {
		return companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	public String getPosition() {
		return position;
	}

	public void setPosition(String position) {
		this.position = position;
	}

}

?

?

DAO层:

package com.easyway.spring.mongodb.dao;

import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.document.mongodb.MongoOperations;
import org.springframework.data.document.mongodb.query.Criteria;
import org.springframework.data.document.mongodb.query.Query;

import com.easyway.spring.mongodb.domain.Entity;
/**
 * 实现Spring的ApplicationContextAware的接口目的
 *   ApplicationContext自动注入到此类中
 *   AbstractBasicDAO<T extends Entity,PK > 
 *     这里强制子类必须继承Entity采用公共的方法,用于检查子类是否继承,PK仅代表主键
 *   
 *   
 *  备注:此类中所有的方法基本是针对MongoOperations中的
 *      方法的简单调用,便于子类的直接实现
 *      
 * @author longgangbai
 */
public class AbstractBasicDAO<T extends Entity,PK >  implements ApplicationContextAware, DAO<T, PK> {
	
	private  ApplicationContext ctx =null;
	
	private MongoOperations mongoOperation;
	/**
	 * 
	 */
	public AbstractBasicDAO(){
	     //
		initBasicDao();
	}
	/**
	 * 启动此类时候,调用此方法初始化Spring 上下文
	 */
	p