Hotle.java


package  test_yu.morphiaSpring;

import  com.google.code.morphia.annotations.Embedded;
import  com.google.code.morphia.annotations.Entity;
import  com.google.code.morphia.annotations.Id;
/**
 * 
 * 
@author  yu
 * 创建了两个领域Hotel,Address,前者为实体存在,有自己的生命周期,后者则为内嵌在实体之中,没有独立的生命周期
 
*/
@Entity
public   class  Hotle {
    @Id
    
private   int  id;

    
private  String name;
    
private   int  stars;
    @Embedded
    
private  Address address;
    
public   int  getId() {
        
return  id;
    }
    
public   void  setId( int  id) {
        
this .id  =  id;
    }
    
public  String getName() {
        
return  name;
    }
    
public   void  setName(String name) {
        
this .name  =  name;
    }
    
public   int  getStars() {
        
return  stars;
    }
    
public   void  setStars( int  stars) {
        
this .stars  =  stars;
    }
    
public  Address getAddress() {
        
return  address;
    }
    
public   void  setAddress(Address address) {
        
this .address  =  address;
    }
    
}

@Embedded
class  Address {
    
private  String street;
    
private  String city;
    
private  String postCode;
    
private  String country;
    
public  String getStreet() {
        
return  street;
    }
    
public   void  setStreet(String street) {
        
this .street  =  street;
    }
    
public  String getCity() {
        
return  city;
    }
    
public   void  setCity(String city) {
        
this .city  =  city;
    }
    
public  String getPostCode() {
        
return  postCode;
    }
    
public   void  setPostCode(String postCode) {
        
this .postCode  =  postCode;
    }
    
public  String getCountry() {
        
return  country;
    }
    
public   void  setCountry(String country) {
        
this .country  =  country;
    }

    
    
}

MorphiaBean.java

package  test_yu.morphiaSpring;

import  com.google.code.morphia.Datastore;
import  com.google.code.morphia.Morphia;
import  com.mongodb.Mongo;

public   class  MorphiaBean  extends  Morphia {
    
private  Mongo mongo;
    
private  String dbName;
    
public  Datastore getDatastore(){
        
if ( this .mongo == null || this .dbName == null ){
            
return   null ;
        }
      
return  createDatastore(mongo, dbName);
    }
    
public  Mongo getMongo() {
        
return  mongo;
    }
    
public   void  setMongo(Mongo mongo) {
        
this .mongo  =  mongo;
    }
    
public  String getDbName() {
        
return  dbName;
    }
    
public   void  setDbName(String dbName) {
        
this .dbName  =  dbName;
    }

}

MorphiaTest.java

package  test_yu.morphiaSpring;


import  java.net.UnknownHostException;

import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.ClassPathXmlApplicationContext;

import  com.google.code.morphia.Datastore;

public   class  MorphiaTest {
    
public   static   void  main(String[] args)  throws  UnknownHostException {
        ApplicationContext context 
= new  ClassPathXmlApplicationContext( " applicationContext.xml " );
        MorphiaBean morphiaBean
= (MorphiaBean) context.getBean( " morphia " );
        Datastore ds
= morphiaBean.getDatastore();
        
// 向数据库中添加多条记录
        addManyArticles(ds);
//         printLine();
//
//         Article single = ds.get(Article.class,9);
//         System.out.println("single = " + single);
//
//         printLine();
//
//          // 查询文章大于5,编辑id大于106的文章
//         List<Article> articles= ds.find(Article.class,"id >",5).filter("editor.id >",106).asList();
//         displayArticleList(articles);
//
//         printLine();
//
//          // 更新id=123的文章标题和编辑名称
//         ds.update(ds.createQuery(Article.class).where("id = 123"),ds.createUpdateOperations(Article.class).set("title","修改后的标题").set("editor.name","yumeng"));
//
//         printLine();
//
//          // 所有id号大于5的文章的编辑id号+1
//         ds.update(ds.createQuery(Article.class).where("id > 5"),ds.createUpdateOperations(Article.class).inc("editor.id"));
//
//         printLine();
//
//          // 向id为123的文章的关键词列表中新增一个关键词
//         ds.update(ds.createQuery(Article.class).filter("id",123),ds.createUpdateOperations(Article.class).add("keywords", "宏基"));
//          // 批量添加关键词
//         List<String > keys = new ArrayList<String>();
//         keys.add("苍天");
//         keys.add("大地");
//         ds.update(ds.createQuery(Article.class).filter("id",123),ds.createUpdateOperations(Article.class).addAll("keywords",keys,false));
//          // 列出所有数据
//         Query<Article> query = ds.find(Article.class);
//         displayQueryRusult(query);

    }

//     private static void printLine() {
//         System.out.println("-------------------------------------");
//     }
//
//     private static void displayArticleList(List<Article> articles) {
//         for (Article article : articles) {
//             System.out.println("article = " + article);
//         }
//     }
//
//     private static void displayQueryRusult(Query<Article> query) {
//         Iterator<Article> it = query.fetch().iterator();
//         while (it.hasNext()) {
//             Article article =  it.next();
//             System.out.println("article.toString() = " + article.toString());
//         }
//     }

    
private   static   void  addManyArticles(Datastore ds) {
        
for ( int  i  =   0 ;i  <   10 ;i ++ ){
            Hotle hotle 
=   new  Hotle();

            Address address
= new  Address();
            address.setCity(
" 北京 " );
            address.setCountry(
" 中国 " );
            address.setPostCode(
" 100080 " );
            address.setStreet(
" 海淀中街 " );
            
            
            hotle.setId(i);
            hotle.setName(
" 悦来酒店 " );
            hotle.setStars(
0 );
            hotle.setAddress(address);
            ds.save(hotle);
        }
    }
}


applicationContext.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:p = " http://www.springframework.org/schema/p "
    xsi:schemaLocation
= " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd " >
    
< bean id = " propertyConfigurer "
        
class = " org.springframework.beans.factory.config.PropertyPlaceholderConfigurer " >
        
< property name = " locations " >
            
< list >
                
< value >/ config.properties </ value >
            
</ list >
        
</ property >
    
</ bean >
    
< bean id = " mongo "   class = " com.mongodb.Mongo " >
        
< constructor - arg type = " String "  value = " 219.239.88.200 " ></ constructor - arg >
        
< constructor - arg type = " int "  value = " 27017 " ></ constructor - arg >
    
</ bean >
    
< bean id = " morphia "   class = " test_yu.morphiaSpring.MorphiaBean " >
        
< property name = " mongo "  ref = " mongo " ></ property >
        
< property name = " dbName "  value = " address " ></ property >
    
</ bean >
</ beans >