日期:2014-05-16 浏览次数:20498 次
/** * 代表了数据库中的记录 */ @Entity class SimpleBean { @PrimaryKey private String key; private String value; public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } }
/** * 用DPL保存和获取数据 * @author mengyang * */ public class HelloWorldByDPL { private File file = new File("C:/Users/mengyang/workspace/je"); private Environment env; private EntityStore store; //建立环境 private void setUp(){ EnvironmentConfig envConfig = new EnvironmentConfig(); envConfig.setAllowCreate(true); //环境的文件夹必须存在,否则设置这个允许创建也仍然会报错 env = new Environment(file, envConfig); StoreConfig storeConfig = new StoreConfig(); storeConfig.setAllowCreate(true); store = new EntityStore(env, "DPLDemo", storeConfig); } //保存数据 private void save(){ SimpleBean entity = new SimpleBean(); entity.setKey("DPL"); entity.setValue("Hello World!"); PrimaryIndex<String, SimpleBean> pi = store.getPrimaryIndex(String.class, SimpleBean.class); pi.put(entity); } //检索数据 private void get(){ PrimaryIndex<String, SimpleBean> pi = store.getPrimaryIndex(String.class, SimpleBean.class); SimpleBean entity = pi.get("DPL"); System.out.println("key:DPL,value:"+entity.getValue()); } //关闭环境 private void shutDown(){ store.close(); env.close(); } /** * @param args */ public static void main(String[] args) { HelloWorldByDPL myCase = new HelloWorldByDPL(); myCase.setUp(); myCase.save(); myCase.get(); myCase.shutDown(); } }