Android Afinal framework Learning (I) FinalDb database operations
Frame address: https://github.com/yangfuhai/afinal
Source code:
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
Database creation
FinalDb db = FinalDb.create(context, "mytest.db", true);
Entity bean
@ Table (name = "user") // @ Table indicates the public class User {private int id; private String name; private String email of the orm (Object link ing) Table name; private Date registerDate; private Double money; /// // getter and setter cannot be omitted. // 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 ;}}
Create a table
db.save(user);
Primary Key annotation:
A primary key is required. The default column name is id and auto-increment. Use annotation @ Id (column = "id ")
The actual bean does not have the id attribute. Use @ id (column = "name") to make name a primary key, non-integer or other integer types, without auto-increment.
Attribute Annotation
@ Property (column = "uname"): maps the attribute name to the uname field in the table.
Cancel orm Annotation
@ Transient: Do not map an attribute to a table
One-to-multiple relationship
@ Onetoworkflow (manyColumn = "parentid ")
Multi-to-one relationship
@ ManyToOne (column = "parentid ")
How to Use FinalDB OneToMany for lazy loading:
Model Definition:
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; /*....*/}
Usage:
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(); }
The FinalDb object has many methods, such as CRUD. Not detailed