Android Afinal架構學習(一) FinalDb 資料庫操作

來源:互聯網
上載者:User

Android Afinal架構學習(一) FinalDb 資料庫操作

架構地址:https://github.com/yangfuhai/afinal

對應源碼:

net.tsz.afinal.annotation.sqlite.*

net.tsz.afinal.db.sqlite.*

net.tsz.afinal.db.table.*

net.tsz.afinal.utils.ClassUtils、net.tsz.afinal.utils.FieldUtils

FinalDb

建庫

FinalDb db = FinalDb.create(context, "mytest.db", true);
有實體bean

@Table(name = "user") //@Table 表示orm(對象關係映射)的表名public class User {    private int id;    private String name;    private String email;    private Date registerDate;    private Double money;         /////////////getter and setter 不能省略哦///////////////    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getEmail() {        return email;    }    public void setEmail(String email) {        this.email = email;    }    public Date getRegisterDate() {        return registerDate;    }    public void setRegisterDate(Date registerDate) {        this.registerDate = registerDate;    }    public Double getMoney() {        return money;    }    public void setMoney(Double money) {        this.money = money;    }}

建表

db.save(user);

主鍵註解:

必須有一個主鍵。預設列名為id,並自增。使用註解@Id(column="id")

實際bean中沒有id屬性,使用@id(column="name") 使name成主鍵 ,非integer等整數類型,不會自增

屬性註解

@Property(column=“uname") , 將屬性name映射成表中的uname欄位

取消orm的註解

@Transient 表示不將某屬性對應到表中

一對多關聯性

@OneToMany(manyColumn="parentid")

多對一關聯性

@ManyToOne(column="parentid")

FinalDB OneToMany懶載入使用方法:

模型定義:

public class Parent{    private int id;    @OneToMany(manyColumn = "parentId")    private OneToManyLazyLoader children;    /*....*/}public class Child{    private int id;    private String text;    @ManyToOne(column = "parentId")    private  Parent  parent;    /*....*/}

使用:

List all = db.findAll(Parent.class);        for( Parent  item : all){            if(item.getChildren ().getList().size()>0)                Toast.makeText(this,item.getText() + item.getChildren().getList().get(0).getText(),Toast.LENGTH_LONG).show();        }

FinalDb對象,有很多方法,CRUD等操作。不詳列了


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.