Android开发:ActiveAndroid的使用

ActiveAndroid是采用活动记录(Active Record)架构模式设计的适用于Android平台的轻量级ORM架构。

github主页:https://github.com/pardom/ActiveAndroid

1.导入ActiveAndroid包

在Project的build.gradle中配置:

allprojects {
    repositories {
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
    }
}

在app的build.gradle中配置:

compile 'com.michaelpardo:activeandroid:3.1.0-SNAPSHOT'

2.配置与初始化

首先在AndroidManifest.xml文件中配置数据库名称和数据库版本号。

  1. <manifest ...>
  2.     <application android:name="com.activeandroid.app.Application" ...>
  3.  
  4.         ...
  5.  
  6.         <meta-data android:name="AA_DB_NAME" android:value="Pickrand.db" />
  7.         <meta-data android:name="AA_DB_VERSION" android:value="5" />
  8.     </application>
  9. </manifest>
  • AA_DB_NAME:数据库名

  • AA_DB_VERSION:数据库版本号,默认是1。

接着,在AndroidManifest.xml文件中指定application元素的name为 com.activeandroid.app.Application,如果需要自定义Application,需要让你的Application对象继 承自com.activeandroid.app.Application而不是android.app.Application。如果你需要继承其他库 的Application,则需要在Application中初始化和处理ActiveAndroid。

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

3.创建Model

创建数据库模型非常简单,创建的模型必须继承Model类,这样你的类名就是你的表名。如果不想使用类名做表名,则可以使用@Table定义表名。@Column用于定义列名。Model类使用无参的构造函数,如果定义自己的构造函数必须定义一个无参的构造函数。

  1. @Table(name = "Categories")
  2. public class Category extends Model {
  3.     @Column(name = "Name")
  4.     public String name;
  5. }
  6.  
  7. @Table(name = "Items")
  8. public class Item extends Model {
  9.     @Column(name = "remote_id", unique = true, onUniqueConflict = Column.ConflictAction.REPLACE)
  10.     public int remoteId;
  11.     @Column(name = "Name")
  12.     public String name;
  13.  
  14.     @Column(name = "Category")
  15.     public Category category;
  16.  
  17.         public Item(){
  18.                 super();
  19.         }
  20.         public Item(String name, Category category){
  21.                 super();
  22.                 this.name = name;
  23.                 this.category = category;
  24.         }
  25. }

4.增删改查

4.1 插入

4.1.1 单条插入

保存一条记录,只需要创建一个模型的实例,并为每个字段指定值,然后调用save()方法。

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

4.1.2批量插入

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

4.2 更新

更新一个字段

  1. new Update(Person.class).set( "name=?",  name).execute();

更新两个字段

  1. new Update(Person.class).set("age=?," + "name=?", age, name).execute();

更新满足name 和 sex条件下的age字段

  1. new Update(Person.class).set("age=?" , age).where("name=? and sex=?",name , sex).execute();

4.3 删除

调用delete()方法就可以删除一条记录,下面的例子中,通过id加载一个Item对象,并且删除他。

  1. Item item = Item.load(Item.class, 1);
  2. item.delete();

也可以通过静态方法删除

  1. Item.delete(Item.class, 1);

也可以创建调用Delete对象删除

  1. new Delete().from(Item.class).where("Id = ?", 1).execute();
  1. new Delete().from(Item.class).where("Id = ? and name =?", 1,“小明”).execute();

4.4 查询

查询一条:

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

查询所有:

  1. public static List<Item> getAll(Category category) {
  2.     return new Select()
  3.         .from(Item.class)
  4.         .where("Category = ?", category.getId())
  5.         .orderBy("Name ASC")
  6.         .execute();
  7. }

猜你喜欢

转载自blog.csdn.net/android157/article/details/81630533