Spring Data集成MongoDB訪問


Spring Source吭哧哼哧,從2011年2月開始到2011年2月終於把spring-data-mongo-1.0.1給Release出來了。從1.0.0.M1到1.0.0.M3的版本叫做Spring Data Document。1.0.0.M4開始更名為Spring Data MongoDB 1.0.0 M4,不過官網並沒有特別說明,乍一看有點莫名其妙,尤其是MongoTemplate從org.springframework.data.document.mongodb移動到org.springframework.data.mongodb.core,官網的HelloWorldExample卻還是用org.springframework.data.document.mongodb做例子,實在造成不少誤導。

 

 -----垃圾的cnblogs,編輯了半天的東西,一下子亂了格式,要重來!-----

 

  Spring Data Mongo需要依賴Spring Framework,因此,首先需要下載Spring Framework的jar包,新建一個Web工程,將Spring Framework的jar包引入。本文引用了如下組件:

   

其中,除了Spring的幾個組件包以外,還引用了MongoDB的Driver:mongo-2.7.3.jar;DWR3.0的組件dwr.jar;spring-data-mongdb-1.0.1.RELEASE.jar以及Spring Data的公共組件spring-data-commons-core-1.2.1.RELEASE.jar。需要說明的是,本文所使用的Spring Data Mongo的組件版本,需要采用Spring Framework 3.0.7及以上版本,否則程序運行會報錯。

 

引入上述組件后,需要修改Spring的配置文件ApplicationContext.xml文件,引入Spring Data Mongo的命名空間,並定義MongoDB的服務器地址和端口號,初始化MongoTemplate類,代碼如下

View Code
 1  <? xml version="1.0" encoding="UTF-8" ?>
 2 
 3  < beans  xmlns ="http://www.springframework.org/schema/beans"
 4          xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
 5          xmlns:context ="http://www.springframework.org/schema/context"
 6          xmlns:mongo ="http://www.springframework.org/schema/data/mongo"
 7          xmlns:dwr ="http://www.directwebremoting.org/schema/spring-dwr"
 8          xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 9              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
10              http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
11              http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd" >
12             
13     
14 
15      < context:annotation-config  />
16 
17      <!--  Properties files  -->
18      < bean  id ="propertyConfigurer"  class ="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
19          < property  name ="locations" >
20              < list >
21                  < value >classpath:server.properties </ value >
22              </ list >
23          </ property >
24      </ bean >
25     
26      < mongo:mongo  host ="localhost"  port ="27017"   />
27      < bean  id ="mongoTemplate"  class ="org.springframework.data.mongodb.core.MongoTemplate" >
28          < constructor-arg  ref ="mongo"   />
29          < constructor-arg  name ="databaseName"  value ="testDb"   />
30      </ bean >
31     
32     
33      < context:component-scan  base-package ="com.ics"   use-default-filters ="false" >
34         < context:include-filter  type ="regex"  expression ="com.ics.bean.*" />
35         < context:include-filter  type ="regex"  expression ="com.ics.dao.*" />
36         < context:include-filter  type ="regex"  expression ="com.ics.service.*" />
37      </ context:component-scan >
38     
39      < dwr:configuration >
40          < dwr:convert  type ="bean"  class ="com.ics.bean.HelloKitty"   />
41      </ dwr:configuration >
42      < dwr:annotation-scan  base-package ="com.ics.bean"  scanDataTransferObject ="true"  scanRemoteProxy ="true" />
43      < dwr:annotation-scan  base-package ="com.ics.web.dwr"  scanDataTransferObject ="false"  scanRemoteProxy ="true" />
44  </ beans >

 下面開始一個簡單的樣例。首先,定義一個HelloKitty bean

View Code
 1  /**
 2   * 
 3    */
 4  package com.ics.bean;
 5 
 6  import org.directwebremoting.annotations.DataTransferObject;
 7 
 8 @DataTransferObject
 9  public  class HelloKitty
10 {
11      private String id;
12     
13      private String name;
14     
15     @Override
16      public String toString()
17     {
18          return "HelloKitty[" + "id=" + id + ", name=" + name + "]";
19     }
20 
21      public String getId()
22     {
23          return id;
24     }
25 
26      public  void setId(String id)
27     {
28          this.id = id;
29     }
30 
31      public String getName()
32     {
33          return name;
34     }
35 
36      public  void setName(String name)
37     {
38          this.name = name;
39     }
40 }

定義數據訪問類,定義兩個方法,一個用於在集合中插入一條記錄,另一個根據name屬性查詢一條記錄

View Code
 1  /**
 2   * 
 3    */
 4  package com.ics.dao;
 5 
 6  import org.springframework.beans.factory.annotation.Autowired;
 7  import org.springframework.data.mongodb.core.MongoTemplate;
 8  import org.springframework.data.mongodb.core.query.Criteria;
 9  import org.springframework.data.mongodb.core.query.Query;
10 
11  import com.ics.bean.HelloKitty;
12 
13 
14  public  class HelloKittyDAO
15 {
16      /**
17       * 定義集合名稱
18        */
19      private  static String HELLOKITTY = "HelloKitty";
20     
21      /**
22       * 操作MongoDB的對象
23        */
24     @Autowired
25      private MongoTemplate mongoTemplate;
26     
27     
28      public  void createHelloKitty(HelloKitty hello)
29     {
30         mongoTemplate.insert(hello, HELLOKITTY);
31     }
32     
33      public HelloKitty getHelloKittyByName(String name)
34     {
35          return mongoTemplate.findOne( new Query(Criteria.where("name").is(name)), HelloKitty. class, HELLOKITTY);
36     }
37 }

簡單的Service方法

 1  /**
 2   * 
 3    */
 4  package com.ics.service;
 5 
 6  import org.springframework.beans.factory.annotation.Autowired;
 7 
 8  import com.ics.bean.HelloKitty;
 9  import com.ics.dao.HelloKittyDAO;
10 
11  public  class HelloKittyService
12 {
13     @Autowired
14      private HelloKittyDAO helloKittyDAO;
15     
16      public String createHelloKitty(HelloKitty hello)
17     {
18         helloKittyDAO.createHelloKitty(hello);
19         
20         HelloKitty ret = helloKittyDAO.getHelloKittyByName(hello.getName());
21         
22          return ret ==  null ? "" : ret.getId();
23     }
24 }

通過DWR發布出去

 1  /**
 2   * 
 3    */
 4  package com.ics.web.dwr;
 5 
 6  import org.directwebremoting.annotations.RemoteMethod;
 7  import org.directwebremoting.annotations.RemoteProxy;
 8  import org.springframework.beans.factory.annotation.Autowired;
 9 
10  import com.ics.bean.HelloKitty;
11  import com.ics.service.HelloKittyService;
12 
13 @RemoteProxy(name = "HelloKittyManage")
14  public  class HelloKittyManage
15 {
16  @Autowired
17   private HelloKittyService helloKittyService;
18  
19  @RemoteMethod
20   public String sayHello(HelloKitty hello)
21  {
22    return "hello " + helloKittyService.createHelloKitty(hello);
23  }
24 }

 最后,在index.html訪問這個DWR方法

 1  < html >
 2      < head >
 3          < title >hello DWR </ title >
 4          < script  type ="text/javascript"  src ="/Mars/dwr/engine.js" ></ script >
 5          < script  type ="text/javascript"  src ="/Mars/dwr/util.js" ></ script >
 6          < script  type ="text/javascript"  src ="/Mars/dwr/interface/HelloKittyManage.js" ></ script >
 7          < script  type ="text/javascript" >
 8               function  sayHello() 
 9              {
10                   var  helloworld  =  { " name " : " xyzz " };
11                  HelloKittyManage.sayHello(helloworld,  function (data){alert(data);});
12              }
13           </ script >
14      </ head >
15      < body >
16          < h3 > Spring 3.X with DWR </ h3 >
17          < href ="javascript:void(0);"  onclick ="sayHello(); return false;" >Retrieve test data </ a >< br />
18      </ body >
19  </ html >

啟動MongoDB服務器,運行樣例程序。

在頁面點擊“Retrieve test data”,將會彈出“Hello 4f9fe5112d0182c5bc0a6c39”字樣,其中“4f9fe5112d0182c5bc0a6c39”就是剛剛插入MongoDB中自動生成的ID。So easy,:)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM