一个精简的 Android ORM 框架

网友投稿 825 2022-10-20

一个精简的 Android ORM 框架

一个精简的 Android ORM 框架

Introduction

简体中文

Did you used sqlite to save your data on Android? If you did, you may be puzzled for the complexity of mechanism. Now the Android ORM (Aorm) coming which armed to make it simple for the developers. If you have the interesting, please join us.

Features

Brief ORM mapping, only add a @Column annotation for the property of Java beans to enable mapping to sqlite table field. Native and ConntentProvider support, easy to share content, initialize/upgrade database using Android ContentProvider. Powerful Forward Engineering supporting, both Eclipse Android ADT-extensions and Android Studio Android ORM Tool plugin to generate DDL and ContentProvider automatically. Useful Assist feature, create your Activity/Service/BroadcastReceiver with a wizard and configure them in AndroidManifest.xml automatically. ...

More feature, please experience it for your self.

Usage

Use in Eclipse

Put aorm-core-$latest.jar to libs/

Recommended to install Android ADT-extensions plugin, and add ORM capapility to enable Aorm.

Use in Android Studio

Aorm has been published to jcenter, so you can just add dependence of aorm in your build.gradle.

dependencies { compile 'cn.ieclipse.aorm:aorm-core:$latest'}

Recommended to install Android ORM Tool plugin to generate code quickly.

Comparision Results

These are the results for the Simple trial:

And these are the results for the Complex trial:

More detail please see https://github.com/Raizlabs/AndroidDatabaseLibraryComparison

Code samples

Create mapping

Simply add @Table class annotation and @Column field annotation to mapping.

@Table(name = "student")public class Student implements Serializable { @Column(name = "_id", id = true) public long id; //id is Primary key. @Column(name="_name", order = 1) 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-.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; } }

Query

Session session = ExampleContentProvider.getSession();// simplest query, query all student table.Criteria criteria = Criteria.create(Student.class);// add restrication: id equalscriteria.add(Restrictions.eq("id", 1));// add restriction: name like Jamlingcriteria.add(Restrictions.like("name", "Jaming"));// add restriction: age < 30criteria.add(Restrictions.lt("age", 30));// add ordercriteria.addOrder(Order.asc("age"));// set districtcriteria.setDistinct(true);// set limit from row 10 to 20criteria.setLimit(10, 10);List 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 querycriteria.addChild(StudentMore.class, "m", Criteria.INNER_JOIN, Restrictions.geProperty("s.id", "m.id"));// query to cursorc = session.query(criteria);// convert to list.List 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];//

Other

Session session = ExampleContentProvider.getSession();// insertStudent s = new Student();s.setName("Jamling");long rowId = session.insert(s, null);// update student's name to Jame whose id is 1s.setId(1);s.setName("Jame");int rows = session.update(s);// delete student whose id is 2session.deleteById(Student.class, 2);// query student whose id is 4s = session.get(Student.class, 4);

Docs

Refer: http://ieclipse-/p/Android-ORM/userguide.html

Author

Jamling Jamling (li.jamling@gmail.com)

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:JVM源码分析之堆外内存完全解读
下一篇:JVM源码分析之不可控的堆外内存
相关文章

 发表评论

暂时没有评论,来抢沙发吧~