SugarORM 是 Android 平台专用ORM。提供简单易学的APIs。可以很容易的处理1对1和1对多的关系型数据,并通过3个函数save(), delete() 和 find() (或者 findById()) 来简化CRUD基本操作。

使用步骤:

 

1.准备jar包(可到git下载源码作为lib使用)。https://github.com/satyan/sugar 

      或者:compile 'com.github.satyan:sugar:1.3'

2.设置application和meta-data数据

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:name="com.anxpp.lib.android.orm.SugarApp"
        android:theme="@android:style/Theme.Black.NoTitleBar" >
        <meta-data android:name="DATABASE" android:value="my_database.db" />
        <meta-data android:name="VERSION" android:value="3" />
        <meta-data android:name="QUERY_LOG" android:value="true" />
        <meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.anxpp" />
        <activity
        ......

 3.创建实体(此处测试了3种):

package com.anxpp.phyg.entity;
import com.anxpp.lib.android.orm.SugarRecord;
import com.anxpp.lib.android.orm.dsl.Table;
/**
 * Created by anxpp on 2015/10/29.
 *
 */
@Table
public class Book extends SugarRecord {
    public Long id;
    private String title;
    private String edition;
    public Book(){
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getEdition() {
        return edition;
    }
    public void setEdition(String edition) {
        this.edition = edition;
    }
}
package com.anxpp.phyg.entity;
import com.anxpp.lib.android.orm.SugarRecord;
/**
 * Created by u on 2015/10/29.
 *
 */
public class Pook extends SugarRecord {
    int _id;    //不会作为id
    String title;
    String edition;
    public Pook(){
    }
    public Pook(String title, String edition){
        this.title = title;
        this.edition = edition;
    }
}
package com.anxpp.phyg.entity;
import com.anxpp.lib.android.orm.SugarRecord;
import com.anxpp.lib.android.orm.dsl.Table;
/**
 * Created by anxpp on 2015/10/29.
 * 商品实体类
 */
@Table
public class Commodity extends SugarRecord {
    public Long id;
    private String name;
    public Commodity(){    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

4.使用:

        Commodity commodity = new Commodity();
        commodity.setName("asdfasdf");
        commodity.save();
        for(int i = 0;i<50;i++){
            Book b = new Book();
            b.setTitle("title"+i);
            b.save();
            new Pook("asdf","sadf").save();
        }

(下面引用官方的说明:http://satyan.github.io/sugar/

Gradle:

compile 'com.github.satyan:sugar:1.3'

It requires minimal configuration.

All you need to do is, specify SugarApp as your application class in AndroidManifest.xml. You do that by changing the android:nameattribute of the application tag.

AndroidManifest.xml
<application android:label="@string/app_name" android:icon="@drawable/icon" android:name="com.orm.SugarApp"> . . <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" /> . . </application>

The meta-data tags are all optional and serve the following purpose:

MetadataDescription
DATABASE Name of the generated sqlite database file. eg: app_name.db
VERSION Version of your database schema.
QUERY_LOG Logs the generated Select queries.
DOMAIN_PACKAGE_NAME Specify a package name where your domain/entity classes are present. This helps in smoother table creation.

Extend SugarRecord for all the classes that you need persisted. That's it. Sugar takes care of table creation for you.

Note: Please retain the default constructor.

public class Book extends SugarRecord<Book> { String title; String edition; public Book(){ } public Book(String title, String edition){ this.title = title; this.edition = edition; } }

Performing CRUD operations are very simple. Functions like save()delete() andfindById(..) are provided to make the work easy.

Note: Record indexes start at index 1.

Save Entity:
Book book = new Book(ctx, "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);