BerkeleyDB-JE Hello World(使用DPL)
现在使用JE中的DPL来演示Hello World,使用DPL非常像Hibernate之类的ORM框架,把数据库中的每条记录都用一个bean来表示,其他的CRUD操作想较于BaseAPI也简单了很多。
/** * 用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();}}