DB4O--java對象數據庫

特色

  • 直接存儲java對象,拋棄對象映射的繁瑣
  • 能夠使用簡單的查詢語句

致命缺點

  • 與java語言綁定

demo

gitee地址

https://gitee.com/ichiva/db4o-demo.git

主要依賴

<dependency>
            <groupId>org.fudaa.com.db4o</groupId>
            <artifactId>db4o-java5</artifactId>
            <version>5.5.1</version>
        </dependency>

編寫測試用例

官方實體類

@Data
public class Car {
	private Integer id;
	private String name;
}
@Data
public class People {
	private Integer id;
	private String name;
	private String address;
	private List<Car> cars;
}

鏈接數據庫

private ObjectContainer container;
    @Before
    public void before(){
        this.container = Db4o.openFile("auto.yap");
        System.out.println("before:");
    }

寫入數據

@Test
    public void add(){
        Car car = new Car();
        car.setName("byd");
        car.setId((int) (Math.random() * Integer.MAX_VALUE));

        container.set(car);
        container.commit();
    }

獲取並修改數據

@Test
    public void update(){
        ObjectSet query = container.query(Car.class);

        query.forEach(e ->{
            Car car = (Car)e;
            car.setName("bmw");
            container.set(car);
        });
    }

遍歷

@Test
    public void showCars() {
        ObjectSet query = container.query(Car.class);
        query.forEach(System.out::println);
    }

別忘了關閉數據庫

@After
    public void after(){
        if(null != container) container.close();
    }

總結,db4o總體能夠看做jdk序列化的升級版 PS.這貨有稀奇古怪的bug,請在老司機的監視下上生產java

gitee地址

https://gitee.com/ichiva/db4o-demo.git
相關文章
相關標籤/搜索