Android Afinal framework Learning (I) FinalDb database operations

Source: Internet
Author: User

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.