设为首页 加入收藏

TOP

使用morphia对基于mongodb应用开发 (四)
2014-11-24 07:14:02 来源: 作者: 【 】 浏览:13
Tags:使用 morphia 基于 mongodb 应用开发
;
double y = Math.round(Math.random() * 10000)/100.0D;
Place p = new Place("Place_"+x+"_"+y,new double[]{x,y});
Store s = new Store(STORE_TYPE[i%5],STORE_TYPE[i%5]+"@"+p.getName(),p);
daoHolder.storeDao.save(s);
}
System.out.println(System.currentTimeMillis() - start);
}
//测试删除
public void testDeleteAll(){
System.out.println("Before delete the number of stores is: " + daoHolder.storeDao.count());
daoHolder.storeDao.deleteAllStore();
System.out.println("After delete the number of stores is: " + daoHolder.storeDao.count());
}
//根据地理位置查找
public void testFindNearPlace(){
Place p = new Place("somewhere",new double[]{23.5,67.8});
System.out.println("Find 5 stores near "+ p.toString());
List list = daoHolder.storeDao.findNearPlace(p);
for( Store s : list)
System.out.println(s.toString());

System.out.println("Find 5 KFC stores near "+ p.toString());
list = daoHolder.storeDao.findKFCNearPlace(p);
for( Store s : list)
System.out.println(s.toString());
}
//查找所有store
public void testFindAll(){
long start = System.currentTimeMillis();
List list = daoHolder.storeDao.find().asList();
for( Store s : list)
System.out.println(s.toString());
System.out.println(System.currentTimeMillis() - start);
}

static class DaoHolder{
PlaceDao placeDao;
StoreDao storeDao;
public DaoHolder(){
try {
Mongo mongo = new Mongo("localhost",27017);
Morphia morphia = new Morphia();
Datastore ds = morphia.createDatastore(mongo, "testDB");
placeDao = new PlaceDao(ds);
storeDao = new StoreDao(ds);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

package mongo.morphia.test;
import java.util.List;
import morphia.dao.PlaceDao;
import morphia.dao.StoreDao;
import morphia.model.Place;
import morphia.model.Store;
import com.google.code.morphia.Datastore;
import com.google.code.morphia.Morphia;
import com.mongodb.Mongo;
public class StoreDaoTest {
public static String[] STORE_TYPE = {"肯德基","麦当劳","必胜客","吉野家","蒸功夫"};
public static StoreDaoTest m = new StoreDaoTest();
static DaoHolder daoHolder = new DaoHolder();
//测试保存,准备一千家店铺的数据
public void testSave(){
long start = System.currentTimeMillis();
for( int i = 0; i < 1000; i++){
double x = Math.round(Math.random() * 10000)/100.0D;
double y = Math.round(Math.random() * 10000)/100.0D;
Place p = new Place("Place_"+x+"_"+y,new double[]{x,y});
Store s = new Store(STORE_TYPE[i%5],STORE_TYPE[i%5]+"@"+p.getName(),p);
daoHolder.storeDao.save(s);
}
System.out.println(System.currentTimeMillis() - start);
}
//测试删除
public void testDeleteAll(){
Syst
首页 上一页 1 2 3 4 5 下一页 尾页 4/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇基于输出的数据库审计和三层数据.. 下一篇基于MongoDB GridFS的图片存储

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis 分布式锁全解 (2025-12-25 17:19:51)
·SpringBoot 整合 Red (2025-12-25 17:19:48)
·MongoDB 索引 - 菜鸟 (2025-12-25 17:19:45)
·What Is Linux (2025-12-25 16:57:17)
·Linux小白必备:超全 (2025-12-25 16:57:14)