`
dyllove98
  • 浏览: 1376048 次
  • 性别: Icon_minigender_1
  • 来自: 济南
博客专栏
73a48ce3-d397-3b94-9f5d-49eb2ab017ab
Eclipse Rcp/R...
浏览量:38147
4322ac12-0ba9-3ac3-a3cf-b2f587fdfd3f
项目管理checkList...
浏览量:78287
4fb6ad91-52a6-307a-9e4f-816b4a7ce416
哲理故事与管理之道
浏览量:131410
社区版块
存档分类
最新评论

MongoDB整合Spring 详细讲解(含代码) .

 
阅读更多

写这篇文章也做了下思考,首先是本人技术欠佳。但就是喜欢研究一些东西。因为在此之前有很多的朋友已经写过类似的,很多我也看过,但是讲解的不够深入。对有些朋友提出的问题不能给出答案。在这里,我根据我目前的能力对其进行整理。并最终运行成功。

在测试过程中出现过一下问题:

1、org/springframework/data/mapping/context/MappingContextAware

2、src-resolve: Cannot resolve the name 'repository:repository' to a(n) 'type definition'

以上都是版本不匹配引起的。特别是第二个错误我看有些解决时候提到了jpa,但是我这里没有使用jpa后来我是把spring-data-commons的包替换了个版本就不出现了。

我先说下我的开发环境:

myeclipse 6.5

mongodb 2.0.8

spring 3.0.4 

最后就是下面2个(这两个版本不对就容易出现各种各样的,杂七杂八的问题) 这里我就给出我所采用的版本

spring-data-document

spring-data-commons

有所改变所有版本必须要对应好下面是jar下载地址
http://www.springsource.org/spring-data/mongodb
http://www.springsource.org/spring-data/commons

下载版本分别为:

spring-data-commons-dist-1.4.0.M1

spring-data-document-1.0.0.M2.zip
下面给出我工程的图片

 

 

然后就开始我们开发之旅吧!

首先新建application.xml配置文件

  1. <SPAN style="COLOR: #3366ff; FONT-SIZE: 18px"><?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"    
  3.           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  4.           xmlns:context="http://www.springframework.org/schema/context"    
  5.           xmlns:mongo="http://www.springframework.org/schema/data/mongo"    
  6.           xsi:schemaLocation="http://www.springframework.org/schema/context     
  7.           http://www.springframework.org/schema/context/spring-context-3.0.xsd     
  8.           http://www.springframework.org/schema/data/mongo     
  9.           http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd     
  10.           http://www.springframework.org/schema/beans     
  11.           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">     
  12.       
  13.         <mongo:mongo host="192.168.0.138" port="27017"/>  
  14.           
  15.           
  16.       
  17.        <bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">     
  18.         <constructor-arg ref="mongo"/>     
  19.         <constructor-arg name="databaseName" value="db"/>     
  20.         <constructor-arg name="defaultCollectionName" value="person" />     
  21.       </bean>     
  22.       
  23.      <bean id="personRepository" class="com.mongo.dao.impl.PersonRepository">     
  24.         <property name="mongoTemplate" ref="mongoTemplate"></property>     
  25.     </bean>     
  26.       
  27.      <context:annotation-config />  
  28.           
  29. </beans>   
  30.     </SPAN>  
<?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">   
    
		<mongo:mongo host="192.168.0.138" port="27017"/>
		
		
	
	   <bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">   
	    <constructor-arg ref="mongo"/>   
	    <constructor-arg name="databaseName" value="db"/>   
	    <constructor-arg name="defaultCollectionName" value="person" />   
	  </bean>   
   	
   	 <bean id="personRepository" class="com.mongo.dao.impl.PersonRepository">   
        <property name="mongoTemplate" ref="mongoTemplate"></property>   
    </bean>   
   	
  	 <context:annotation-config />
 		
</beans> 
	


然后编写操作mongodb的接口

  1. <SPAN style="COLOR: #3366ff; FONT-SIZE: 18px">/** 
  2.  * AbstractRepository.java 
  3.  * 版权所有(C) 2012  
  4.  * 创建:cuiran 2012-12-12 11:40:40 
  5.  */  
  6. package com.mongo.dao;  
  7.   
  8. import java.util.List;  
  9.   
  10. import com.mongo.bean.Person;  
  11.   
  12. /** 
  13.  * TODO 
  14.  * @author cuiran 
  15.  * @version TODO 
  16.  */  
  17. public interface AbstractRepository {  
  18.       
  19.     /** 
  20.      *  
  21.      *<b>function:</b>添加对象 
  22.      * @author cuiran 
  23.      * @createDate 2012-12-12 11:41:30 
  24.      */  
  25.     public void insert(Person person);   
  26.       
  27.     /** 
  28.      *  
  29.      *<b>function:</b>根据ID查找对象 
  30.      * @author cuiran 
  31.      * @createDate 2012-12-12 11:41:41 
  32.      */  
  33.     public Person findOne(String id);     
  34.     /** 
  35.      *  
  36.      *<b>function:</b>查询所有 
  37.      * @author cuiran 
  38.      * @createDate 2012-12-12 16:26:06 
  39.      */  
  40.     public List<Person> findAll();     
  41.       
  42.     public List<Person> findByRegex(String regex);  
  43.     /** 
  44.      *  
  45.      *<b>function:</b>删除指定的ID对象 
  46.      * @author cuiran 
  47.      * @createDate 2012-12-12 16:26:16 
  48.      */  
  49.     public void removeOne(String id);     
  50.     /** 
  51.      *  
  52.      *<b>function:</b>删除所有 
  53.      * @author cuiran 
  54.      * @createDate 2012-12-12 16:25:40 
  55.      */  
  56.     public void removeAll();     
  57.     /** 
  58.      * 通过ID找到并修改 
  59.      *<b>function:</b> 
  60.      * @author cuiran 
  61.      * @createDate 2012-12-12 16:25:51 
  62.      */  
  63.     public void findAndModify(String id);     
  64.   
  65.       
  66. }  
  67. </SPAN>  
/**
 * AbstractRepository.java
 * 版权所有(C) 2012 
 * 创建:cuiran 2012-12-12 11:40:40
 */
package com.mongo.dao;

import java.util.List;

import com.mongo.bean.Person;

/**
 * TODO
 * @author cuiran
 * @version TODO
 */
public interface AbstractRepository {
	
	/**
	 * 
	 *<b>function:</b>添加对象
	 * @author cuiran
	 * @createDate 2012-12-12 11:41:30
	 */
	public void insert(Person person); 
	
	/**
	 * 
	 *<b>function:</b>根据ID查找对象
	 * @author cuiran
	 * @createDate 2012-12-12 11:41:41
	 */
    public Person findOne(String id);   
    /**
     * 
     *<b>function:</b>查询所有
     * @author cuiran
     * @createDate 2012-12-12 16:26:06
     */
    public List<Person> findAll();   
    
    public List<Person> findByRegex(String regex);
    /**
     * 
     *<b>function:</b>删除指定的ID对象
     * @author cuiran
     * @createDate 2012-12-12 16:26:16
     */
    public void removeOne(String id);   
    /**
     * 
     *<b>function:</b>删除所有
     * @author cuiran
     * @createDate 2012-12-12 16:25:40
     */
    public void removeAll();   
    /**
     * 通过ID找到并修改
     *<b>function:</b>
     * @author cuiran
     * @createDate 2012-12-12 16:25:51
     */
    public void findAndModify(String id);   

	
}


再写对应接口的实现类:

  1. <SPAN style="COLOR: #3366ff; FONT-SIZE: 18px">/** 
  2.  * PersonRepository.java 
  3.  * 版权所有(C) 2012  
  4.  * 创建:cuiran 2012-12-12 11:42:51 
  5.  */  
  6. package com.mongo.dao.impl;  
  7.   
  8. import java.util.List;  
  9. import java.util.regex.Pattern;  
  10.   
  11. import org.springframework.data.document.mongodb.MongoTemplate;  
  12. import org.springframework.data.document.mongodb.query.Criteria;  
  13. import org.springframework.data.document.mongodb.query.Query;  
  14. import org.springframework.data.document.mongodb.query.Update;  
  15. import com.mongo.bean.Person;  
  16. import com.mongo.dao.AbstractRepository;  
  17.   
  18. /** 
  19.  * TODO 
  20.  * @author cuiran 
  21.  * @version TODO 
  22.  */  
  23. public class PersonRepository implements AbstractRepository {  
  24.   
  25.       private MongoTemplate mongoTemplate;     
  26.   
  27.     /* (non-Javadoc) 
  28.      * @see com.mongo.dao.AbstractRepository#findAll() 
  29.      */  
  30.     @Override  
  31.     public List<Person> findAll() {  
  32.         // TODO Auto-generated method stub   
  33.         return getMongoTemplate().find(new Query(), Person.class);     
  34.   
  35.     }  
  36.   
  37.     /* (non-Javadoc) 
  38.      * @see com.mongo.dao.AbstractRepository#findAndModify(java.lang.String) 
  39.      */  
  40.     @Override  
  41.     public void findAndModify(String id) {  
  42.         // TODO Auto-generated method stub   
  43.         //new Query(Criteria.where("id").is(id)), new Update().inc("age", 3)   
  44.           
  45.         getMongoTemplate().updateFirst(new Query(Criteria.where("id").is(id)), new Update().inc("age"3));  
  46.   
  47.     }  
  48.   
  49.     /* (non-Javadoc) 
  50.      * @see com.mongo.dao.AbstractRepository#findByRegex(java.lang.String) 
  51.      */  
  52.     @Override  
  53.     public List<Person> findByRegex(String regex) {  
  54.         // TODO Auto-generated method stub   
  55.          Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);     
  56.           Criteria criteria = new Criteria("name").regex(pattern.toString());     
  57.             return getMongoTemplate().find(new Query(criteria), Person.class);     
  58.   
  59.     }  
  60.   
  61.     /* (non-Javadoc) 
  62.      * @see com.mongo.dao.AbstractRepository#findOne(java.lang.String) 
  63.      */  
  64.     @Override  
  65.     public Person findOne(String id) {  
  66.         // TODO Auto-generated method stub   
  67.          return getMongoTemplate().findOne(new Query(Criteria.where("id").is(id)), Person.class);     
  68.   
  69.     }  
  70.   
  71.     /* (non-Javadoc) 
  72.      * @see com.mongo.dao.AbstractRepository#insert(com.mongo.bean.Person) 
  73.      */  
  74.     @Override  
  75.     public void insert(Person person) {  
  76.         // TODO Auto-generated method stub   
  77.         getMongoTemplate().insert(person);     
  78.     }  
  79.   
  80.     /* (non-Javadoc) 
  81.      * @see com.mongo.dao.AbstractRepository#removeAll() 
  82.      */  
  83.     @Override  
  84.     public void removeAll() {  
  85.         // TODO Auto-generated method stub   
  86.         List<Person> list = this.findAll();     
  87.         if(list != null){     
  88.             for(Person person : list){     
  89.                 getMongoTemplate().remove(person);     
  90.             }     
  91.         }     
  92.   
  93.     }  
  94.   
  95.     /* (non-Javadoc) 
  96.      * @see com.mongo.dao.AbstractRepository#removeOne(java.lang.String) 
  97.      */  
  98.     @Override  
  99.     public void removeOne(String id) {  
  100.         // TODO Auto-generated method stub   
  101.         Criteria criteria = Criteria.where("id").in(id);     
  102.         if(criteria == null){     
  103.              Query query = new Query(criteria);     
  104.              if(query != null && getMongoTemplate().findOne(query, Person.class) != null)     
  105.                  getMongoTemplate().remove(getMongoTemplate().findOne(query, Person.class));     
  106.         }     
  107.   
  108.     }  
  109.   
  110.     /** 
  111.      * @return the mongoTemplate 
  112.      */  
  113.     public MongoTemplate getMongoTemplate() {  
  114.         return mongoTemplate;  
  115.     }  
  116.   
  117.     /** 
  118.      * @param mongoTemplate the mongoTemplate to set 
  119.      */  
  120.     public void setMongoTemplate(MongoTemplate mongoTemplate) {  
  121.         this.mongoTemplate = mongoTemplate;  
  122.     }  
  123.   
  124. }  
  125. </SPAN>  
/**
 * PersonRepository.java
 * 版权所有(C) 2012 
 * 创建:cuiran 2012-12-12 11:42:51
 */
package com.mongo.dao.impl;

import java.util.List;
import java.util.regex.Pattern;

import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.query.Criteria;
import org.springframework.data.document.mongodb.query.Query;
import org.springframework.data.document.mongodb.query.Update;
import com.mongo.bean.Person;
import com.mongo.dao.AbstractRepository;

/**
 * TODO
 * @author cuiran
 * @version TODO
 */
public class PersonRepository implements AbstractRepository {

	  private MongoTemplate mongoTemplate;   

	/* (non-Javadoc)
	 * @see com.mongo.dao.AbstractRepository#findAll()
	 */
	@Override
	public List<Person> findAll() {
		// TODO Auto-generated method stub
		return getMongoTemplate().find(new Query(), Person.class);   

	}

	/* (non-Javadoc)
	 * @see com.mongo.dao.AbstractRepository#findAndModify(java.lang.String)
	 */
	@Override
	public void findAndModify(String id) {
		// TODO Auto-generated method stub
		//new Query(Criteria.where("id").is(id)), new Update().inc("age", 3)
		
		getMongoTemplate().updateFirst(new Query(Criteria.where("id").is(id)), new Update().inc("age", 3));

	}

	/* (non-Javadoc)
	 * @see com.mongo.dao.AbstractRepository#findByRegex(java.lang.String)
	 */
	@Override
	public List<Person> findByRegex(String regex) {
		// TODO Auto-generated method stub
		 Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);   
	      Criteria criteria = new Criteria("name").regex(pattern.toString());   
	        return getMongoTemplate().find(new Query(criteria), Person.class);   

	}

	/* (non-Javadoc)
	 * @see com.mongo.dao.AbstractRepository#findOne(java.lang.String)
	 */
	@Override
	public Person findOne(String id) {
		// TODO Auto-generated method stub
		 return getMongoTemplate().findOne(new Query(Criteria.where("id").is(id)), Person.class);   

	}

	/* (non-Javadoc)
	 * @see com.mongo.dao.AbstractRepository#insert(com.mongo.bean.Person)
	 */
	@Override
	public void insert(Person person) {
		// TODO Auto-generated method stub
		getMongoTemplate().insert(person);   
	}

	/* (non-Javadoc)
	 * @see com.mongo.dao.AbstractRepository#removeAll()
	 */
	@Override
	public void removeAll() {
		// TODO Auto-generated method stub
		List<Person> list = this.findAll();   
        if(list != null){   
            for(Person person : list){   
                getMongoTemplate().remove(person);   
            }   
        }   

	}

	/* (non-Javadoc)
	 * @see com.mongo.dao.AbstractRepository#removeOne(java.lang.String)
	 */
	@Override
	public void removeOne(String id) {
		// TODO Auto-generated method stub
		Criteria criteria = Criteria.where("id").in(id);   
        if(criteria == null){   
             Query query = new Query(criteria);   
             if(query != null && getMongoTemplate().findOne(query, Person.class) != null)   
                 getMongoTemplate().remove(getMongoTemplate().findOne(query, Person.class));   
        }   

	}

	/**
	 * @return the mongoTemplate
	 */
	public MongoTemplate getMongoTemplate() {
		return mongoTemplate;
	}

	/**
	 * @param mongoTemplate the mongoTemplate to set
	 */
	public void setMongoTemplate(MongoTemplate mongoTemplate) {
		this.mongoTemplate = mongoTemplate;
	}

}

这里也给出对应Person对象代码

  1. <SPAN style="COLOR: #3366ff; FONT-SIZE: 18px">/** 
  2.  * Person.java 
  3.  * 版权所有(C) 2012  
  4.  * 创建:cuiran 2012-12-12 11:37:16 
  5.  */  
  6. package com.mongo.bean;  
  7.   
  8. import java.io.Serializable;  
  9.   
  10. /** 
  11.  * TODO 
  12.  * @author cuiran 
  13.  * @version TODO 
  14.  */  
  15. public class Person implements Serializable {  
  16.   
  17.     /** 
  18.      *  
  19.      */  
  20.     private static final long serialVersionUID = 3617931430808763429L;  
  21.       
  22.     private String id;     
  23.     private String name;     
  24.     private int age;  
  25.     public Person() {  
  26.         super();  
  27.     }  
  28.     public Person(String id, String name, int age) {  
  29.         super();  
  30.         this.id = id;  
  31.         this.name = name;  
  32.         this.age = age;  
  33.     }  
  34.     /** 
  35.      * @return the id 
  36.      */  
  37.     public String getId() {  
  38.         return id;  
  39.     }  
  40.     /** 
  41.      * @param id the id to set 
  42.      */  
  43.     public void setId(String id) {  
  44.         this.id = id;  
  45.     }  
  46.     /** 
  47.      * @return the name 
  48.      */  
  49.     public String getName() {  
  50.         return name;  
  51.     }  
  52.     /** 
  53.      * @param name the name to set 
  54.      */  
  55.     public void setName(String name) {  
  56.         this.name = name;  
  57.     }  
  58.     /** 
  59.      * @return the age 
  60.      */  
  61.     public int getAge() {  
  62.         return age;  
  63.     }  
  64.     /** 
  65.      * @param age the age to set 
  66.      */  
  67.     public void setAge(int age) {  
  68.         this.age = age;  
  69.     }  
  70.     /** 
  71.      *  
  72.      * @param name 
  73.      * @param age 
  74.      */  
  75.     public Person(String name, int age) {  
  76.         super();  
  77.         this.name = name;  
  78.         this.age = age;  
  79.     }     
  80.   
  81.      public String toString() {     
  82.             return "Person[id="+id+",name="+name+",age="+age+"]";     
  83.         }     
  84.   
  85.   
  86. }  
  87. </SPAN>  
/**
 * Person.java
 * 版权所有(C) 2012 
 * 创建:cuiran 2012-12-12 11:37:16
 */
package com.mongo.bean;

import java.io.Serializable;

/**
 * TODO
 * @author cuiran
 * @version TODO
 */
public class Person implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3617931430808763429L;
	
	private String id;   
    private String name;   
    private int age;
	public Person() {
		super();
	}
	public Person(String id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}
	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the age
	 */
	public int getAge() {
		return age;
	}
	/**
	 * @param age the age to set
	 */
	public void setAge(int age) {
		this.age = age;
	}
	/**
	 * 
	 * @param name
	 * @param age
	 */
	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}   

	 public String toString() {   
	        return "Person[id="+id+",name="+name+",age="+age+"]";   
	    }   


}

最后写出我们的测试类开始进行测试

  1. <SPAN style="COLOR: #3366ff; FONT-SIZE: 18px">/** 
  2.  * MongoTest.java 
  3.  * 版权所有(C) 2012  
  4.  * 创建:cuiran 2012-12-12 11:54:30 
  5.  */  
  6. package com.mongo.test;  
  7.   
  8. import java.util.List;  
  9.   
  10. import org.apache.commons.logging.Log;  
  11. import org.apache.commons.logging.LogFactory;  
  12. import org.springframework.context.ApplicationContext;  
  13. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  14.   
  15. import com.mongo.bean.Person;  
  16. import com.mongo.dao.AbstractRepository;  
  17. import com.mongo.dao.impl.PersonRepository;  
  18.   
  19.   
  20.   
  21. /** 
  22.  * TODO 
  23.  * @author cuiran 
  24.  * @version TODO 
  25.  */  
  26. public class MongoTest {  
  27.   
  28.     private static Log log = LogFactory.getLog(MongoTest.class.getName());  
  29.       
  30.     private  AbstractRepository pr=null;  
  31.       
  32.     /** 
  33.      *  
  34.      *<b>function:</b> 
  35.      * @author cuiran 
  36.      * @createDate 2012-12-12 16:08:02 
  37.      */  
  38.     public void init(){  
  39.          log.debug("开始启动");  
  40.          ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");  
  41.           pr= (PersonRepository)ctx.getBean("personRepository");  
  42.            
  43.           
  44.           
  45.     }  
  46.     /** 
  47.      *  
  48.      *<b>function:</b>添加 
  49.      * @author cuiran 
  50.      * @createDate 2012-12-12 16:11:01 
  51.      */  
  52.     public void insert(){  
  53.           
  54.         Person p=new Person("cuiran",27);  
  55.          pr.insert(p);  
  56.          log.debug("添加成功");  
  57.     }  
  58.     /** 
  59.      *  
  60.      *<b>function:</b>根据输入的ID查找对象 
  61.      * @author cuiran 
  62.      * @createDate 2012-12-12 16:24:10 
  63.      */  
  64.     public void findOne(){  
  65.         String id="50c83cb552c2ceb0463177d6";  
  66.         Person p= pr.findOne(id);  
  67.         log.debug(p);  
  68.     }  
  69.       
  70.       
  71.     /** 
  72.      *  
  73.      *<b>function:</b>查询所有 
  74.      * @author cuiran 
  75.      * @createDate 2012-12-12 16:08:54 
  76.      */  
  77.     public void listAll(){  
  78.           
  79.         List<Person> list=pr.findAll();  
  80.         log.debug("查询结果如下:");  
  81.         for (Person p:list){  
  82.             log.debug(p.toString());  
  83.         }  
  84.           
  85.           
  86.     }  
  87.       
  88.     /** 
  89.      *  
  90.      *<b>function:</b>测试方法 
  91.      * @author cuiran 
  92.      * @createDate 2012-12-12 16:11:37 
  93.      */  
  94.     public void start(){  
  95.         init();  
  96.           
  97.         //insert();   
  98.         //listAll();   
  99.           
  100.         findOne();  
  101.     }  
  102.       
  103.     /** 
  104.      *<b>function:</b>main函数 
  105.      * @author cuiran 
  106.      * @createDate 2012-12-12 11:54:30 
  107.      */  
  108.     public static void main(String[] args) {  
  109.         // TODO Auto-generated method stub   
  110.         MongoTest t=new MongoTest();  
  111.         t.start();  
  112.     }  
  113.   
  114. }  
  115. </SPAN>  
/**
 * MongoTest.java
 * 版权所有(C) 2012 
 * 创建:cuiran 2012-12-12 11:54:30
 */
package com.mongo.test;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mongo.bean.Person;
import com.mongo.dao.AbstractRepository;
import com.mongo.dao.impl.PersonRepository;



/**
 * TODO
 * @author cuiran
 * @version TODO
 */
public class MongoTest {

	private static Log log = LogFactory.getLog(MongoTest.class.getName());
	
	private  AbstractRepository pr=null;
	
	/**
	 * 
	 *<b>function:</b>
	 * @author cuiran
	 * @createDate 2012-12-12 16:08:02
	 */
	public void init(){
		 log.debug("开始启动");
		 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		  pr= (PersonRepository)ctx.getBean("personRepository");
		 
		
		
	}
	/**
	 * 
	 *<b>function:</b>添加
	 * @author cuiran
	 * @createDate 2012-12-12 16:11:01
	 */
	public void insert(){
		
		Person p=new Person("cuiran",27);
		 pr.insert(p);
		 log.debug("添加成功");
	}
	/**
	 * 
	 *<b>function:</b>根据输入的ID查找对象
	 * @author cuiran
	 * @createDate 2012-12-12 16:24:10
	 */
	public void findOne(){
		String id="50c83cb552c2ceb0463177d6";
		Person p= pr.findOne(id);
		log.debug(p);
	}
	
	
	/**
	 * 
	 *<b>function:</b>查询所有
	 * @author cuiran
	 * @createDate 2012-12-12 16:08:54
	 */
	public void listAll(){
		
		List<Person> list=pr.findAll();
		log.debug("查询结果如下:");
		for (Person p:list){
			log.debug(p.toString());
		}
		
		
	}
	
	/**
	 * 
	 *<b>function:</b>测试方法
	 * @author cuiran
	 * @createDate 2012-12-12 16:11:37
	 */
	public void start(){
		init();
		
		//insert();
		//listAll();
		
		findOne();
	}
	
	/**
	 *<b>function:</b>main函数
	 * @author cuiran
	 * @createDate 2012-12-12 11:54:30
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MongoTest t=new MongoTest();
		t.start();
	}

}


运行出现一下日志,就没什么问题。

  1. <SPAN style="COLOR: #3366ff; FONT-SIZE: 18px">2012-12-12 16:23:59:DEBUG com.mongo.test.MongoTest - 开始启动  
  2. 2012-12-12 16:23:59:INFO org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@253498: startup date [Wed Dec 12 16:23:59 CST 2012]; root of context hierarchy  
  3. 2012-12-12 16:23:59:INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [applicationContext.xml]  
  4. 2012-12-12 16:24:00:INFO org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@12a0f6c: defining beans [mongo,mongoTemplate,personRepository,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor]; root of factory hierarchy  
  5. 2012-12-12 16:24:00:DEBUG com.mongo.test.MongoTest - Person[id=50c83cb552c2ceb0463177d6,name=cuiran,age=27]  
  6. </SPAN>  
2012-12-12 16:23:59:DEBUG com.mongo.test.MongoTest - 开始启动
2012-12-12 16:23:59:INFO org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@253498: startup date [Wed Dec 12 16:23:59 CST 2012]; root of context hierarchy
2012-12-12 16:23:59:INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [applicationContext.xml]
2012-12-12 16:24:00:INFO org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@12a0f6c: defining beans [mongo,mongoTemplate,personRepository,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor]; root of factory hierarchy
2012-12-12 16:24:00:DEBUG com.mongo.test.MongoTest - Person[id=50c83cb552c2ceb0463177d6,name=cuiran,age=27]


 

由于这些程序只是作为测试使用,对出现的问题, 欢迎留言咨询。谢谢大家,点击查看更多关于

 在此附上demo源码欢迎朋友下载学习  http://vdisk.weibo.com/s/krDlf 虾米站长

分享到:
评论

相关推荐

    MongoDB整合Spring实例详细讲解(含代码)

    主要介绍了MongoDB整合Spring实例详细讲解(含代码),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    Spring4 整合MongoDB详细讲解项目源码

    spring4 整合MongoDB详细讲解项目源码 Mongo DB 是目前在IT行业非常流行的一种非关系型数据库 NoSql 其灵活的数据存储方式备受当前IT从业人员的青睐 Mongo DB很好的实现了面向对象的思想 OO思想 在Mongo DB中 每一条...

    Spring高级之注解驱动开发视频教程

    视频详细讲解,需要的小伙伴自行百度网盘下载,链接见附件,永久有效。 1、课程简介 Spring框架是一系列应用框架的核心,也可以说是整合其他应用框架的基座。同时还是SpringBoot的基础。在当下的市场开发环境中,...

    JAVA面试题[秒杀/Redis/MongoDB/SpringBoot/SpringCloud]

    本套面试题将Java中的各个知识点模块混合详讲,讲解的非常详细,是一套极好的面试题宝典哦。本套课程涵盖的知识点有:SpringBoot、SpringCloud、SpringMVC、Spring、Mybatis、MySQL、Redis、Oracle、秒杀等 我所有...

    PassJava-Learning:整合了Spring Cloud 实战教程+公众号内容都会同步到这个仓库。文档地址:http

    PassJava 项目全套学习教程...采用流行的技术,如 SpringBoot、MyBatis、Redis、 MySql、 MongoDB、 RabbitMQ、Elasticsearch,采用Docker容器化部署。 更好的阅读体验 文档地址: 备用地址1: 备用地址2: PassJava

    SpringMVC+Spring+Mybatis三大框架综合练习[MySQL/AJAX/IDEA]

    项目讲解---&gt;Mybatis框架----&gt;spring框架----&gt;springmvc框架---&gt;ssm三大框架整合---&gt;maven---&gt;SVN/GIT---&gt;hibernate框架---&gt;struts2框架---&gt;linux---&gt;SSM项目综合小练习---&gt;...

    SpringBoot-Hello:SpringBoot学习入门案例,持续更新中..

    希望做到每个子项目都配有一篇博客文章的详细讲解 :backhand_index_pointing_right: 项目名称 详情 springboot-aop-log 使用AOP记录日志 springboot-easyexcel 使用更好用的阿里巴巴表格工具EasyExcel springboot-...

    正则表达式详讲

    项目讲解---&gt;Mybatis框架----&gt;spring框架----&gt;springmvc框架---&gt;ssm三大框架整合---&gt;maven---&gt;SVN/GIT---&gt;hibernate框架---&gt;struts2框架---&gt;linux---&gt;SSM项目综合小练习---&gt;...

    SpringBoot详讲

    项目讲解---&gt;Mybatis框架----&gt;spring框架----&gt;springmvc框架---&gt;ssm三大框架整合---&gt;maven---&gt;SVN/GIT---&gt;hibernate框架---&gt;struts2框架---&gt;linux---&gt;SSM项目综合小练习---&gt;...

    EasyUI框架详讲[jQuery]

    项目讲解---&gt;Mybatis框架----&gt;spring框架----&gt;springmvc框架---&gt;ssm三大框架整合---&gt;maven---&gt;SVN/GIT---&gt;hibernate框架---&gt;struts2框架---&gt;linux---&gt;SSM项目综合小练习---&gt;...

    Java项目电商秒杀[Redis+RabbitMQ+JMeter]

    项目讲解---&gt;Mybatis框架----&gt;spring框架----&gt;springmvc框架---&gt;ssm三大框架整合---&gt;maven---&gt;SVN/GIT---&gt;hibernate框架---&gt;struts2框架---&gt;linux---&gt;SSM项目综合小练习---&gt;...

    SVN和GIT详讲视频

    项目讲解---&gt;Mybatis框架----&gt;spring框架----&gt;springmvc框架---&gt;ssm三大框架整合---&gt;maven---&gt;SVN/GIT---&gt;hibernate框架---&gt;struts2框架---&gt;linux---&gt;SpringBoot---&gt;SpringCloud--...

    jQuery+Ajax+三层模型详讲[Java]

    项目讲解---&gt;Mybatis框架----&gt;spring框架----&gt;springmvc框架---&gt;ssm三大框架整合---&gt;maven---&gt;SVN/GIT---&gt;hibernate框架---&gt;struts2框架---&gt;linux---&gt;SSM项目综合小练习---&gt;...

Global site tag (gtag.js) - Google Analytics