簡介
您是否在Android上使用sqlite來存儲您的應(yīng)用數(shù)據(jù)呢?如果是,或許您會對Android存取sqlite數(shù)據(jù)感覺有點小復(fù)雜。本人從2010年開始從事Android開發(fā),對于Android的數(shù)據(jù)庫操縱這一塊裁蚁,感覺還是比較復(fù)雜的,尤其是可維護性及或可擴展性方面继准,都是比較差的枉证。
也正是從這時開始,我決定在Android平臺上實現(xiàn)像Hibernate那樣的ORM框架移必,來簡化開發(fā)工作室谚。使得開發(fā)者在使用sqlite數(shù)據(jù)庫時,無需關(guān)注底層數(shù)據(jù)庫實現(xiàn)崔泵,更多的是關(guān)注上層業(yè)務(wù)邏輯實現(xiàn)秒赤。
如果您對此項目感興趣,歡迎您的加入管削!
特性
- 簡潔的ORM映射配置, 只需在Java Bean的屬性中添加類似
@Column(name="_name")
的注解即可完成到數(shù)據(jù)庫字段的映射配置. - 強大的正向工程支持, 自動生成DDL和ContentProvider.
- 給力的輔助工具, 向?qū)叫陆ˋctivity/Service/BroadcastReceiver并且自動配置到AndroidManifest.xml.
- ...
更多特性倒脓,請自行體驗.
使用
, 在使用中請將$latest
替換為具體的版本.
Eclipse
下載aorm-core-$latest
.jar并放入libs/目錄下
推薦安裝Android ADT-extensions插件
Android Studio
Aorm已經(jīng)發(fā)布到j(luò)center,在您的app/build.gradle中添加以下依賴含思。
dependencies {
compile 'cn.ieclipse.aorm:aorm-core:1.1.5'
}
推薦安裝Android Studio上的Android ORM Tool插件,可以快速生成相關(guān)代碼甘晤。
類似orm庫比較
簡單試驗結(jié)果:
復(fù)雜試驗結(jié)果:
測試工程代碼請參考: https://github.com/Raizlabs/AndroidDatabaseLibraryComparison
示例代碼
創(chuàng)建映射
只需添加類@Table
注解及@Column
屬性注解即可完成表與字段的映射含潘。
@Table(name = "student")
public class Student implements Serializable {
@Column(name = "_id", id = true)
public long id; //id is Primary key.
@Column(name="_name")
public String name; //mapping to _name and auto column type
@Column()
public int age; //auto column type and name
@Column(defaultValue="''")
public String phone; // default value in empty
public String address; // no mapping
}
Create database
package cn.ieclipse.aorm.example;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import cn.ieclipse.aorm.Aorm;
import cn.ieclipse.aorm.Session;
import cn.ieclipse.aorm.example.bean.Course;
import cn.ieclipse.aorm.example.bean.Grade;
import cn.ieclipse.aorm.example.bean.Student;
/**
* @author Jamling
*
*/
public class ExampleContentProvider extends ContentProvider {
public static final String AUTH = "cn.ieclipse.aorm.example.provider";
public static final Uri URI = Uri.parse("content://" + AUTH);
private SQLiteOpenHelper mOpenHelper;
private static Session session;
@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
return 0;
}
@Override
public String getType(Uri arg0) {
return null;
}
@Override
public Uri insert(Uri arg0, ContentValues arg1) {
return null;
}
@Override
public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3,
String arg4) {
return null;
}
@Override
public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
return 0;
}
public static Session getSession() {
return session;
}
@Override
public boolean onCreate() {
mOpenHelper = new SQLiteOpenHelper(this.getContext(), "example.db",
null, 1) {
public void onCreate(SQLiteDatabase db) {
// method 3: use AORM to create table
Aorm.createTable(db, Grade.class);
Aorm.createTable(db, Student.class);
Aorm.createTable(db, Course.class);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
// update table, suggested to wrapper in if block
Aorm.updateTable(db, Grade.class);
Aorm.updateTable(db, Student.class);
Aorm.updateTable(db, Course.class);
}
};
session = new Session(mOpenHelper, getContext().getContentResolver());
return true;
}
}
查詢
Session session = ExampleContentProvider.getSession();
// 最簡單的查詢:查詢所有的學(xué)生信息.
Criteria criteria = Criteria.create(Student.class);
// 添加條件: id相等
criteria.add(Restrictions.eq("id", 1));
// 添加條件: name like Jamling
criteria.add(Restrictions.like("name", "Jaming"));
// add restriction: age < 30
criteria.add(Restrictions.lt("age", 30));
// add order
criteria.addOrder(Order.asc("age"));
// set district
criteria.setDistinct(true);
// set limit from row 10 to 20
criteria.setLimit(10, 10);
List<Student> list = session.list(Student.class);
// if you use Android CursorAdapter you can:
Cursor c = session.query(criteria);
// set alias, so the project will be alias.columnn. e.g. s.name
// criteria.setAlias("s");
// multi-table query
criteria.addChild(StudentMore.class, "m", Criteria.INNER_JOIN,
Restrictions.geProperty("s.id", "m.id"));
// query to cursor
c = session.query(criteria);
// convert to list.
List<Object[]> ret = CursorUtils.getFromCursor(c,
new Class[] { Student.class }, new String[] { "s", "m" });
// query to list.
ret = session.listAll(criteria);
Object[] item = ret.get(0);
Student s = (Student) item[0];
StudentMore m = (StudentMore) item[1];
//
其它操作
Session session = ExampleContentProvider.getSession();
// insert
Student s = new Student();
s.setName("Jamling");
long rowId = session.insert(s, null);
// update student's name to Jame whose id is 1
s.setId(1);
s.setName("Jame");
int rows = session.update(s);
// delete student whose id is 2
session.deleteById(Student.class, 2);
// query student whose id is 4
s = session.get(Student.class, 4);