程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> MongoDBÕûºÏSpringʵÀýÏêϸ½²½â(º¬´úÂë)

MongoDBÕûºÏSpringʵÀýÏêϸ½²½â(º¬´úÂë)

編輯:關於JAVA

MongoDBÕûºÏSpringʵÀýÏêϸ½²½â(º¬´úÂë)。本站提示廣大學習愛好者:(MongoDBÕûºÏSpringʵÀýÏêϸ½²½â(º¬´úÂë))文章只能為提供參考,不一定能成為您想要的結果。以下是MongoDBÕûºÏSpringʵÀýÏêϸ½²½â(º¬´úÂë)正文


дÕâƪÎÄÕÂÒ²×öÁËÏÂ˼¿¼£¬Ê×ÏÈÊDZ¾È˼¼ÊõÇ·¼Ñ¡£µ«¾ÍÊÇϲ»¶Ñо¿Ò»Ð©¶«Î÷¡£ÒòΪÔÚ´Ë֮ǰÓкܶàµÄÅóÓÑÒѾ­Ð´¹ýÀàËƵģ¬ºÜ¶àÎÒÒ²¿´¹ý£¬µ«Êǽ²½âµÄ²»¹»ÉîÈë¡£¶ÔÓÐЩÅóÓÑÌá³öµÄÎÊÌâ²»Äܸø³ö´ð°¸¡£ÔÚÕâÀÎÒ¸ù¾ÝÎÒÄ¿Ç°µÄÄÜÁ¦¶ÔÆä½øÐÐÕûÀí¡£²¢×îÖÕÔËÐгɹ¦¡£

ÔÚ²âÊÔ¹ý³ÌÖгöÏÖ¹ýÒ»ÏÂÎÊÌ⣺

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ÅäÖÃÎļþ

<?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µÄ½Ó¿Ú

/** 
 * AbstractRepository.java 
 */ 
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);   
 
   
} 

ÔÙд¶ÔÓ¦½Ó¿ÚµÄʵÏÖÀࣺ

/** 
 * PersonRepository.java 
 */ 
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¶ÔÏó´úÂë

/** 
 * Person.java 
 */ 
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+"]";   
    }   
 
 
} 

×îºóд³öÎÒÃǵIJâÊÔÀ࿪ʼ½øÐвâÊÔ

/** 
 * MongoTest.java 
 */ 
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(); 
  } 
 
} 

ÔËÐгöÏÖÒ»ÏÂÈÕÖ¾£¬¾ÍûʲôÎÊÌâ¡£

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Ô´Â룺demo

ÒÔÉϾÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝ£¬Ï£Íû¶Ô´ó¼ÒµÄѧϰÓÐËù°ïÖú£¬Ò²Ï£Íû´ó¼Ò¶à¶àÖ§³Ö½Å±¾Ö®¼Ò¡£

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved