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
文件中配置数据库名称和数据库版本号。
- <manifest ...>
- <application android:name="com.activeandroid.app.Application" ...>
- ...
- <meta-data android:name="AA_DB_NAME" android:value="Pickrand.db" />
- <meta-data android:name="AA_DB_VERSION" android:value="5" />
- </application>
- </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。
- public class MyApplication extends SomeLibraryApplication {
- @Override
- public void onCreate() {
- super.onCreate();
- ActiveAndroid.initialize(this);
- }
- @Override
- public void onTerminate() {
- super.onTerminate();
- ActiveAndroid.dispose();
- }
- }
3.创建Model
创建数据库模型非常简单,创建的模型必须继承Model类,这样你的类名就是你的表名。如果不想使用类名做表名,则可以使用@Table定义表名。@Column用于定义列名。Model类使用无参的构造函数,如果定义自己的构造函数必须定义一个无参的构造函数。
- @Table(name = "Categories")
- public class Category extends Model {
- @Column(name = "Name")
- public String name;
- }
- @Table(name = "Items")
- public class Item extends Model {
- @Column(name = "remote_id", unique = true, onUniqueConflict = Column.ConflictAction.REPLACE)
- public int remoteId;
- @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;
- }
- }
4.增删改查
4.1 插入
4.1.1 单条插入
保存一条记录,只需要创建一个模型的实例,并为每个字段指定值,然后调用save()方法。
- Category restaurants = new Category();
- restaurants.name = "Restaurants";
- restaurants.save();
4.1.2批量插入
- 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();
- }
4.2 更新
更新一个字段
- new Update(Person.class).set( "name=?", name).execute();
更新两个字段
- new Update(Person.class).set("age=?," + "name=?", age, name).execute();
更新满足name 和 sex条件下的age字段
- new Update(Person.class).set("age=?" , age).where("name=? and sex=?",name , sex).execute();
4.3 删除
调用delete()方法就可以删除一条记录,下面的例子中,通过id加载一个Item对象,并且删除他。
- Item item = Item.load(Item.class, 1);
- item.delete();
也可以通过静态方法删除
- Item.delete(Item.class, 1);
也可以创建调用Delete对象删除
- new Delete().from(Item.class).where("Id = ?", 1).execute();
- new Delete().from(Item.class).where("Id = ? and name =?", 1,“小明”).execute();
4.4 查询
查询一条:
- public static Item getRandom(Category category) {
- return new Select()
- .from(Item.class)
- .where("Category = ?", category.getId())
- .orderBy("RANDOM()")
- .executeSingle();
- }
查询所有:
- public static List<Item> getAll(Category category) {
- return new Select()
- .from(Item.class)
- .where("Category = ?", category.getId())
- .orderBy("Name ASC")
- .execute();
- }