本文主要是介绍3. Think in Berkeley Database Java Edition 实战JE 数据访问器保存和读取多个对象,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
上篇博文我们已经实战JE 保存和读取单个对象,这节课我们将挑战下通过数据访问器来保存和存储多个对象。
数据访问器也许你只是第一次听说,不用担心,它只是对代码做了一定的封装而已
好了,话不多说,我们开始。
首先我们还是需要一个工具类,来管理我们的Environment 和EntityStore 的初始化和关闭。
代码详情如下:
import java.io.File;import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.StoreConfig;/*** Berkeley Database Java Edition * 环境管理器* @author fairy* **/
public class BDBEnvironmentMangager {private Environment myEnvironment;private EntityStore myEntityStore;//空的构造器public BDBEnvironmentMangager() {}/*** 初始化BDB环境* */public void setup(File envHome, boolean readOnly)throws DatabaseException {//创建一个BDB 环境配置对象EnvironmentConfig myEnvConfig = new EnvironmentConfig();//创建一个数据存储配置对象StoreConfig myStoreConfig = new StoreConfig();//设置该环境是否为只读,true 为只读,false 为可读写myEnvConfig.setReadOnly(readOnly);//设置数据存储配置是否为只读,true 为只读,false 为可读写myStoreConfig.setReadOnly(readOnly);//如果该环境不存在是否重建,true 允许重建,false 不可重建myEnvConfig.setAllowCreate(!readOnly);//如果该存储配置不存在是否重建,true 允许重建,false 不可重建myStoreConfig.setAllowCreate(!readOnly);// 打开 environment 和 entity storemyEnvironment = new Environment(envHome, myEnvConfig);myEntityStore = new EntityStore(myEnvironment, "EntityStore", myStoreConfig);}// Close the store and environment.public void close() {//判断存储对象是否为空if (myEntityStore != null) {try {//尝试关闭存储对象myEntityStore.close();} catch(DatabaseException dbe) {System.err.println("Error closing store: " +dbe.toString());System.exit(-1);}}//判断环境是否为空if (myEnvironment != null) {try {// 关闭环境myEnvironment.close();} catch(DatabaseException dbe) {System.err.println("Error closing MyDbEnv: " + dbe.toString());System.exit(-1);}}}//Getter and Setterpublic Environment getMyEnvironment() {return myEnvironment;}public void setMyEnvironment(Environment myEnvironment) {this.myEnvironment = myEnvironment;}public EntityStore getMyEntityStore() {return myEntityStore;}public void setMyEntityStore(EntityStore myEntityStore) {this.myEntityStore = myEntityStore;}
}
然后我们需要一个实体类,我定义了一个名字叫做Product的实体类。
代码如下所示:
import com.sleepycat.persist.model.Entity;
import com.sleepycat.persist.model.PrimaryKey;
import com.sleepycat.persist.model.Relationship;
import com.sleepycat.persist.model.SecondaryKey;@Entity
public class Product {//主键@PrimaryKeyprivate Long productId;private String productName;//一对一的关系@SecondaryKey(relate=Relationship.ONE_TO_ONE)private Integer catelogId;public Long getProductId() {return productId;}public void setProductId(Long productId) {this.productId = productId;}public String getProductName() {return productName;}public void setProductName(String productName) {this.productName = productName;}public Integer getCatelogId() {return catelogId;}public void setCatelogId(Integer catelogId) {this.catelogId = catelogId;}@Overridepublic String toString() {// TODO Auto-generated method stubreturn super.toString();}
}
然后我们开始创建我们的数据访问器
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.PrimaryIndex;
import com.sleepycat.persist.SecondaryIndex;
import com.xingyun.model.Product;public class ProductDA {// Index Accessors 索引访问器public PrimaryIndex<Long,Product> pIdx;//一级索引,主键public SecondaryIndex<Long,Long,Product> sIdx;//二级索引public ProductDA(EntityStore entityStore) {// 获取Product 实体类的主键,即一级索引pIdx = entityStore.getPrimaryIndex(Long.class, Product.class);// 一级索引// 二级索引字段get方法的返回类型// 二级市索引字段的名称//主键,二级索引的字段类型,二级索引字段的名称sIdx = entityStore.getSecondaryIndex(pIdx, Long.class, "catelogId");}
}
然后最后一步开始写我们的调用主方法
import java.io.File;import com.sleepycat.persist.EntityStore;
import com.xingyun.db.ProductDA;
import com.xingyun.model.Product;
import com.xingyun.util.BDBEnvironmentMangager;public class MainTest2 {// 配置数据库环境文件路径,private final static String BDB_ENV_HOME_2_FILE_PATH = "bdb_env_home2";//数据库环境文件private final static File BDB_ENV_HOME_2_File = new File(BDB_ENV_HOME_2_FILE_PATH);public static void main(String[] args) {// TODO Auto-generated method stubBDBEnvironmentMangager myBdbEnvironmentMangager=new BDBEnvironmentMangager();//如果数据库环境文件对象不存在if(!BDB_ENV_HOME_2_File.exists()) {//创建这个文件路径BDB_ENV_HOME_2_File.mkdirs();}//数据库环境初始化操作myBdbEnvironmentMangager.setup(BDB_ENV_HOME_2_File,false);//存放数据EntityStore entityStore=myBdbEnvironmentMangager.getMyEntityStore();//创建数据访问器ProductDA myProductDA=new ProductDA(entityStore);//开始存放数据Product product=new Product();product.setProductId(1L);product.setProductName("Product Name1");product.setCatelogId(6);myProductDA.pIdx.put(product);Product product2=new Product();product2.setProductId(2L);product2.setProductName("Product Name2");product2.setCatelogId(8);myProductDA.pIdx.put(product2);//读取保存的对象Product resultProduct=new Product();resultProduct=myProductDA.pIdx.get(1L);System.out.println(resultProduct.getProductId());System.out.println(resultProduct.getProductName());System.out.println(resultProduct.getCatelogId());Product resultProduct2=new Product();resultProduct2=myProductDA.pIdx.get(2L);System.out.println(resultProduct2.getProductId());System.out.println(resultProduct2.getProductName());System.out.println(resultProduct2.getCatelogId());//关闭EntityStore 和EnvironmentmyBdbEnvironmentMangager.close();}
}
运行结果:
这篇关于3. Think in Berkeley Database Java Edition 实战JE 数据访问器保存和读取多个对象的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!