当前位置: 移动技术网 > IT编程>开发语言>Java > MongoDB整合Spring实例详细讲解(含代码)

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

2019年07月22日  | 移动技术网IT编程  | 我要评论

范玮琪 婚礼,好听的dj网站,江西考证

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

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

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下载地址



下载版本分别为:

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+"]";   
    }   
 
 
} 

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

/** 
 * 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源码:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网