Add library dependencies
- Android studio
dependencies {
compile 'cn.ieclipse.aorm:aorm-core:1.1.2'
}
- Eclipse
Install Android ADT-extension plugin
Settings
Optional settings, setting the Aorm globally.
Aorm.enableDebug(true);// Enable/Disable debug to print SQL
Aorm.allowExtend(false);// Disable model bean extend
Aorm.setExactInsertOrUpdate(true);//Set use actuarial insertOrUpdate. If true, will query the object from database, insert if not exists or update if exist, otherwise insert when PK is 0 or update when PK more than 0 (maybe update fail)
Create model bean
Add mapping table
Add @Table annotation in your java bean
@Table(name = "student")
public class Student implements java.ioSerializable{
//...
}
Add column mapping
Add @Column annotation for java bean field
- pk (primary key field)
id is true
@Column(name = "_id", id = true)
private long id;
- normal field
@Column(name = "_name")
private String name;
@Column()
private int age;
- un-mapping field
No Aorm annotation assigned.
private String address;
The bean must generate getter/setter methods
Create table
If Android ADT-extensions install in Eclipse. Select the java element includes Aorm beans and Open New ORM Provider to generate ContentPrivider in wizards.
Or create manually.
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);
}
};
Usage
Query
Simple query
Query all students
Session session = ExampleContentProvider.getSession();
// simplest query, query all student table.
Criteria criteria = Criteria.create(Student.class);
List<Student> list = session.list(Student.class);
Query to cursor, so used it in CursorAdapter
Cursor c = session.query(criteria);
Query to single object
// query student whose id is 4
s = session.get(Student.class, 4);
Restriction query
Equals
// add restrication: id equals
criteria.add(Restrictions.eq("id", 1));
Like
// add restriction: name like Jamling
criteria.add(Restrictions.like("name", "Jaming"));
Compare
// add restriction: age > 30
criteria.add(Restrictions.gt("age", 30));
Order
// add order
criteria.addOrder(Order.asc("age"));
Distinct
// set district
criteria.setDistinct(true);
Limit
// set limit from row 10 to 20
criteria.setLimit(10, 10);
Join query
The Criteria support four join
- Left Join
- Left outer join
- Inner join
- Cross join
Criteria criteria = Criteria.create(Student.class, "s")
.addChild(Grade.class, "g")
.add(Restrictions.eqProperty("s.id", "g.sid"));
// query to list.
List<Object[]> ret = session.listAll(criteria);
Object[] item = ret.get(0);
Student s = (Student) item[0];
Grade g = (Grade) item[1];
Insert
Session session = ExampleContentProvider.getSession();
// insert
Student s = new Student();
s.setName("Jamling");
long rowId = session.insert(s, null);
Update
// update student's name to Jame whose id is 1
s.setId(1);
s.setName("Jame");
int rows = session.update(s);
Delete
// delete student whose id is 2
session.deleteById(Student.class, 2);