快速接入:
第一步:gradle引入
compile ‘com.github.satyan:sugar:1.5’
第二步:manifest文件的application標(biāo)簽下配置
數(shù)據(jù)庫(kù)的名字,版本號(hào),log脸秽,你的JavaBean所在的包(會(huì)自動(dòng)找到這個(gè)包澳淑,通過(guò)映射關(guān)系邪乍,給你生成對(duì)應(yīng)的表)
<meta-data android:name="DATABASE" android:value="sugar_example.db" />
<meta-data android:name="VERSION" android:value="2" />
<meta-data android:name="QUERY_LOG" android:value="true" />
<meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.example" />
第三步:項(xiàng)目的Application類集成SugarAPP類
public App extends SugarApp{
}
第四步:建表毅舆,如下類名就是表名衰粹。
注意:
1.駝峰命名的類名最終轉(zhuǎn)化成下劃線并小寫(xiě)的表名吏饿,如BookRecord(類名)--->book_record(表名)
2.一定要保留一個(gè)空的構(gòu)造函數(shù)
public class Book extends SugarRecord {
String title;
String edition;
public Book(){
}
public Book(String title, String edition){
this.title = title;
this.edition = edition;
}
}
第五步:簡(jiǎn)單用法踪危,CURD如下:
Save Entity:
Book book = new Book("Title here", "2nd edition")
book.save();
Load Entity:
Book book = Book.findById(Book.class, 1);
Update Entity:
Book book = Book.findById(Book.class, 1);
book.title = "updated title here"; // modify the values
book.edition = "3rd edition";
book.save(); // updates the previous entry with new values.
Delete Entity:
Book book = Book.findById(Book.class, 1);
book.delete();
Bulk Operations:
List<Book> books = Book.listAll(Book.class);
Book.deleteAll(Book.class);
Book.find(Book.class, "name = ? and title = ?", "satya", "title1")