最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MongoDB整合Spring實例詳細(xì)講解(含代碼)

 更新時間:2017年01月09日 14:34:24   作者:cuiran  
這篇文章主要介紹了MongoDB整合Spring實例詳細(xì)講解(含代碼),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

寫這篇文章也做了下思考,首先是本人技術(shù)欠佳。但就是喜歡研究一些東西。因為在此之前有很多的朋友已經(jīng)寫過類似的,很多我也看過,但是講解的不夠深入。對有些朋友提出的問題不能給出答案。在這里,我根據(jù)我目前的能力對其進(jìn)行整理。并最終運行成功。

在測試過程中出現(xiàn)過一下問題:

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的包替換了個版本就不出現(xiàn)了。

我先說下我的開發(fā)環(huán)境:

myeclipse 6.5

MongoDB 2.0.8

spring 3.0.4

最后就是下面2個(這兩個版本不對就容易出現(xiàn)各種各樣的,雜七雜八的問題) 這里我就給出我所采用的版本

spring-data-document

spring-data-commons

有所改變所有版本必須要對應(yīng)好下面是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

下面給出我工程的圖片

 

然后就開始我們開發(fā)之旅吧!

首先新建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>根據(jù)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);   
 
   
} 

再寫對應(yīng)接口的實現(xiàn)類:

/** 
 * 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; 
  } 
 
}  

這里也給出對應(yīng)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+"]";   
    }   
 
 
} 

最后寫出我們的測試類開始進(jìn)行測試

/** 
 * 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>根據(jù)輸入的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("查詢結(jié)果如下:"); 
    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函數(shù) 
   * @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(); 
  } 
 
} 

運行出現(xiàn)一下日志,就沒什么問題。

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java技術(shù)匯總

    Java技術(shù)匯總

    本篇文章主要對Java基本知識點和技術(shù)點的一些看法和介紹,具有很好的參考價值。下面跟著小編一起來看下吧
    2017-03-03
  • springboot項目中添加自定義日志及配置過程

    springboot項目中添加自定義日志及配置過程

    這篇文章主要介紹了springboot項目中添加自定義日志,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-07-07
  • 詳解Java單例模式中的餓漢和懶漢模式

    詳解Java單例模式中的餓漢和懶漢模式

    這篇文章主要介紹了詳解Java單例模式中的餓漢和懶漢模式,單例模式中有兩種模式一種是餓漢模式,一種是懶漢模式,那么他們有什么區(qū)別呢,需要的朋友可以參考下本文
    2023-04-04
  • IDEA配置MAVEN本地倉庫的實現(xiàn)步驟

    IDEA配置MAVEN本地倉庫的實現(xiàn)步驟

    本文主要介紹了IDEA配置MAVEN本地倉庫的實現(xiàn)步驟,將詳細(xì)介紹如何配置Maven環(huán)境變量,Maven配置文件,可以輕松地設(shè)置和配置MAVEN本地倉庫,以便在IDEA中享受更高效的開發(fā)體驗
    2023-08-08
  • Java微信跳一跳操作指南

    Java微信跳一跳操作指南

    這篇文章主要為大家詳細(xì)介紹了Java微信跳一跳操作指南,通過adb來控制手機進(jìn)行操作,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • 解析java中的condition

    解析java中的condition

    Condition 將 Object 監(jiān)視器方法(wait()、notify()和notifyAll())分解成截然不同的對象,以便通過將這些對象與任意Lock實現(xiàn)組合使用,為每個對象提供多個等待 set(wait-set)
    2021-06-06
  • java高并發(fā)的線程中斷的幾種方式詳解

    java高并發(fā)的線程中斷的幾種方式詳解

    這篇文章主要介紹了Java線程中斷機制幾種方法及示例,向大家分享了這幾種方法的介紹幾代碼示例,具有一定參考價值,需要的朋友可以了解下。
    2021-10-10
  • Spring?Security?OAuth?Client配置加載源碼解析

    Spring?Security?OAuth?Client配置加載源碼解析

    這篇文章主要為大家介紹了Spring?Security?OAuth?Client配置加載源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • JDK?8和JDK?17的區(qū)別和新特性大全

    JDK?8和JDK?17的區(qū)別和新特性大全

    這篇文章主要給大家介紹了關(guān)于JDK?8和JDK?17的區(qū)別和新特性的相關(guān)資料,文中總結(jié)一些Jdk8到Jdk17的一些新特性,給大家選擇jdk版本的時候有些參考性,需要的朋友可以參考下
    2023-06-06
  • spring boot自定義log4j2日志文件的實例講解

    spring boot自定義log4j2日志文件的實例講解

    下面小編就為大家分享一篇spring boot自定義log4j2日志文件的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-11-11

最新評論

承德县| 韶山市| 海原县| 伊宁市| 双鸭山市| 苏州市| 平阳县| 青龙| 大港区| 延长县| 泰宁县| 莒南县| 盘锦市| 措美县| 新兴县| 山东| 安龙县| 东安县| 延吉市| 南投县| 南宁市| 富川| 武冈市| 互助| 信阳市| 社会| 临澧县| 连山| 云和县| 益阳市| 华亭县| 凤阳县| 白玉县| 东城区| 鄂伦春自治旗| 永康市| 平远县| 墨玉县| 古交市| 岳西县| 唐河县|