Berkeley 中两种数据入库(通过文件形式存储)方式

通过同事了解Berkeley,被这个小db给强强吸引了。

简单说一下刚了解到berkeley的两种入库方式

1、普通型-->存储任意类型

package org.db;


import java.io.File;
import java.io.Serializable;
import java.util.Iterator;
import java.util.Set;


import com.sleepycat.bind.serial.SerialBinding;
import com.sleepycat.bind.serial.StoredClassCatalog;
import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.collections.StoredMap;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;


public class Test3 {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub


EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setAllowCreate(true);
envConfig.setTransactional(true);
Environment myEnv = new Environment(new File("D:\\Berkeley\\demodb"),
envConfig);


DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setAllowCreate(true);
dbConfig.setTransactional(true);
dbConfig.setSortedDuplicates(false);
Database mydb = myEnv.openDatabase(null, "myTest.db", dbConfig);

Database ssdb=myEnv.openDatabase(null, "mystore.db", dbConfig);
StoredClassCatalog catalog=new StoredClassCatalog(ssdb);

TupleBinding<String> key=TupleBinding.getPrimitiveBinding(String.class);

SerialBinding<Dog3> datas=new SerialBinding(catalog, Dog3.class);

StoredMap<String, Dog3> map=new StoredMap<String, Dog3>(mydb, key, datas, true);

Dog3 dog=new Dog3();
dog.setName("多吉");
dog.setAge(1);
dog.setAddress("西藏");
map.put("多吉", dog);

}


}


class Dog3 implements Serializable{

private String name;
private String address;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "[ name is "+name+"; address is "+address+"; age is "+age+"]";
}
}

这种形式可以保存任意类型数据也可以通过把对象进行序列化进行保存到键值对中(需要进行序列化数据绑定)。

第二种方式:实用性-->通过注解实体类中

package org.db;


import java.io.File;
import java.util.Iterator;


import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.persist.EntityCursor;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.PrimaryIndex;
import com.sleepycat.persist.StoreConfig;
import com.sleepycat.persist.model.Entity;
import com.sleepycat.persist.model.PrimaryKey;


public class Test1 {


private static PrimaryIndex<String, Dog> index=null;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub


EnvironmentConfig myConfig=new EnvironmentConfig();
myConfig.setAllowCreate(true);
myConfig.setTransactional(true);

Environment env=new Environment(new File("d:\\Berkeley\\demodb"), myConfig);

StoreConfig sConfig=new StoreConfig();
sConfig.setAllowCreate(true);
sConfig.setTransactional(true);

EntityStore ss=new EntityStore(env, "mystore", sConfig);
index=ss.getPrimaryIndex(String.class, Dog.class);

//进行放入数据
Dog dog=new Dog();
dog.setName("藏獒多吉");
dog.setAge(11);
dog.setAddress("xizang");
index.put(dog);


showData();

}


static void showData(){
EntityCursor< Dog> cursors=index.entities();
Iterator<Dog> dogs=cursors.iterator();
while(dogs.hasNext()){
Dog dog=dogs.next();
System.out.println(dog);
}
}
}
@Entity
class Dog{
@PrimaryKey
private String name;
private String address;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "[ name is "+name+"; address is "+address+"; age is "+age+"]";
}



}

注:两种实例都缺少进行关闭信息。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值