Android轻量级ORM框架ActiveAndroid入门教程

本文详细介绍了ActiveAndroid这一轻量级ORM框架的使用方法,包括配置步骤、数据库模型创建、数据保存与更新、批量插入、删除记录及数据库查询等方面的内容。

ActiveAndroid算是一个轻量级的ORM框架,简单地通过如save()和delete()等方法来做到增删改查等操作。配置起来也还算简单。

开始

在AndroidManifest.xml中我们需要添加这两个

  • AA_DB_NAME (这个name不能改,但是是可选的,如果不写的话 是默认的"Application.db"这个值)
  • AA_DB_VERSION (optional – defaults to 1)

    ...

这个<application>是必须指定的,但你也可以使用自己的Application,继承自com.activeandroid.app.Application

?
1
public class MyApplication extends com.activeandroid.app.Application { ...

如果你不想或者不能继承com.activeandroid.app.Application的话,那么就这样

?
1
2
3
4
5
6
7
8
9
10
11
12
public class MyApplication extends SomeLibraryApplication {
@Override
public void onCreate() {
     super .onCreate();
     ActiveAndroid.initialize( this );
}
@Override
public void onTerminate() {
     super .onTerminate();
     ActiveAndroid.dispose();
}
}

ActiveAndroid.initialize(this);做初始化工作,ActiveAndroid.dispose();做清理工作

创建数据库模型

我们使用@Table(name = "Items")来表示表,使用@Column(name = "Name")来表示列,ActiveAndroid会使用自增长的ID作为主键,然后按照注解描述,将类对应映射为数据库表。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Table (name = "Items" )
public class Item extends Model {
@Column (name = "Name" )
public String name;
@Column (name = "Category" )
public Category category;
     public Item(){
             super ();
     }
     public Item(String name, Category category){
             super ();
             this .name = name;
             this .category = category;
     }
}

依赖关系的数据库表

假如Item和Category是多对一的关系,那么我们可以这样子创建他们的类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Table (name = "Items" )
public class Item extends Model {
@Column (name = "Name" )
public String name;
@Column (name = "Category" )
public Category category;
}
 
<!-- lang: java -->
@Table (name = "Categories" )
public class Category extends Model {
@Column (name = "Name" )
public String name;
public List<Item> items() {
     return getMany(Item. class , "Category" );
}
}

如何保存和更新数据到数据库

单挑插入

保存Category对象

?
1
2
3
Category restaurants = new Category();
restaurants.name = "Restaurants" ;
restaurants.save();

分配了一个category并且保存到数据库

?
1
2
3
4
Item item = new Item();
item.category = restaurants;
item.name = "Outback Steakhouse" ;
item.save();

批量插入

如果你要批量插入数据,最好使用事务(transaction)。

?
1
2
3
4
5
6
7
8
9
10
11
12
ActiveAndroid.beginTransaction();
try {
         for ( int i = 0 ; i < 100 ; i++) {
             Item item = new Item();
             item.name = "Example " + i;
             item.save();
         }
         ActiveAndroid.setTransactionSuccessful();
     }
finally {
     ActiveAndroid.endTransaction();
}

使用事务的话只用了 40ms,不然的话需要4秒。

删除记录

我们有三种方式删除一条记录

?
1
2
3
4
5
6
7
8
9
Item item = Item.load(Item. class , 1 );
item.delete();
 
 
<!-- lang: java -->
Item.delete(Item. class , 1 );
 
<!-- lang: java -->
new Delete().from(Item. class ).where( "Id = ?" , 1 ).execute();

很简单吧

查询数据库

作者将查询做的非常像SQLite的原生查询语句,几乎涵盖了所有的指令 com.activeandroid.query包下有以下类

  • Delete
  • From
  • Join
  • Select
  • Set
  • Update

我们举例说明吧

?
1
2
3
4
5
6
7
public static Item getRandom(Category category) {
     return new Select()
         .from(Item. class )
         .where( "Category = ?" , category.getId())
         .orderBy( "RANDOM()" )
         .executeSingle();
}

对应的sqlite查询语句就是 select * from Item where Category = ? order by RANDOM() 当然还支持其他非常多的指令

  • limit
  • offset
  • as
  • desc/asc
  • inner/outer/cross join
  • group by
  • having 等等

大家可以在ActiveAndroid项目下的tests工程找到测试用例,有非常多详细的描述。

来自:http://linkyan.com/2013/05/about-activeandroid/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值