BerkeleyDB-JE Hello World(使用DPL)

現在使用JE中的DPL來演示Hello World,使用DPL非常像Hibernate之類的ORM框架,把數據庫中的每條記錄都用一個bean來表示,其他的CRUD操作想較於BaseAPI也簡單了很多。

/**
* 代表了數據庫中的記錄
*/
@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();
}

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章