ORM框架ActiveAndroid简介及使用教程:
ActiveAndroid框架是是一个轻量级的数据库框架,对于数据库的CUDR操作进行了很好的封装,使用起来很是方便,使得数据库的操作不再是用原始的API。
activeandroid的github地址:https://github.com/pardom/ActiveAndroid
具体的使用教程在其github地址上有很详细的教程:https://github.com/pardom/ActiveAndroid/wiki
使用ActiveAndroid遇到的坑:
这里只是记一下我使用的时候遇到的坑:
在导入jar包之后根据wiki上的教程开始学习,在完成了准备工作,调用自定义的modle的save()方法保存数据时始终报空指针异常的错误:
java.lang.RuntimeException: Unable to create application com.activeandroid.app.Application: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.reflect.Field.getType()' on a null object reference
当是使用的是Android Studio2.1.2,用的Gradle是’com.android.tools.build:gradle:1.1.2’
原因及解决办法:
后来在stackoverflow上找到了问题的原因及解决办法:
原因:
因为现在大部分Android Studio 都已经是2.0版本或者更高,所以对应的使用的Gradle版本也在2.0+,但是ActiveAndroid和Gradle2.0+的兼容性不好,所以导致了两者不能愉快的的一起玩耍。
解决办法一:
解决办法二:
解决办法三:
附上stackoverflow中关于该问题的连接:
http://stackoverflow.com/questions/36490573/why-active-android-is-not-working-with-gradle-2-0
ORM框架GreenDao简介及使用教程:
GreenDao是一个比ActiveAndroid更加高效快捷的ORM数据库。他可以通过自带的工具自动的帮我们生成DAO(Data Access Object)层的Java代码。
GreenDao3使用入门:
首先根据给出的教程添加相关的依赖:
在project的的gradle中添加:
repositories {
jcenter()
mavenCentral()//需要添加的
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'//需要添加的
}
在App的gradle中添加:
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'//需要添加的
compile 'org.greenrobot:greendao:3.2.0'//需要添加的
在Application中初始化greenDao:
package com.wei.greendaodemo;
import android.app.Application;
import org.greenrobot.greendao.database.Database;
/**
* Created by WQC on 2016/11/24.
*/
public class App extends Application{
//是否加密
public static final boolean ENCRYPTED = false;
DaoSession mDaoSession;
@Override
public void onCreate() {
super.onCreate();
//根据是否加密获取不同的数据库
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, ENCRYPTED ? "notes-db-encrypted" : "notes-db");
Database db = ENCRYPTED ? helper.getEncryptedWritableDb("super-secret") : helper.getWritableDb();
mDaoSession = new DaoMaster(db).newSession();
}
//获取DaoSession的方法
public DaoSession getDaoSession() {
return mDaoSession;
}
}
然后创建所要映射的表对应的实体类:
package com.wei.greendaodemo;
import org.greenrobot.greendao.annotation.Convert;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Index;
import org.greenrobot.greendao.annotation.NotNull;
import org.greenrobot.greendao.annotation.Property;
import org.greenrobot.greendao.annotation.Transient;
import org.greenrobot.greendao.annotation.Unique;
/**
* Entity mapped to table "NOTE".
* 这个实体对应着数据库中的note表
*/
@Entity(
//指定索引方式
indexes = {@Index(value = "text, date DESC", unique = true)}
//在数据库中的名字
,nameInDb = ""
//可以在这里定义关于表的一些具体的细节信息
)
//将Java实体转化为数据库支持的实体,greenDao也是根据这个注解来自动生成相应的代码
public class Note {
@Id(autoincrement = true)//ID是否自增
@Unique//为字段添加唯一约束,同时greenDao会自动为该字段添加默认方式的索引
private Long id;
//字段非空
@NotNull
private String text;
//自定义在数据库表中的字段名
@Property(nameInDb = "comment")
private String comment;
private java.util.Date date;
//将自定义的类型转换成数据库支持的类型,其中需要自己集成一个转换类,并实现其中的装换的方法
@Convert(converter = NoteTypeConverter.class, columnType = String.class)
private NoteType type;
//声明字段并不持久化保存,只是一个临时状态,这会是自动在构建代码的时候生成的
@Transient
private String temp;
//同样不需要关注,这是在构建时自动生成的,一般情况下不需要修改
@Generated(hash = 1272611929)
public Note() {
}
public Note(Long id) {
this.id = id;
}
@Generated(hash = 1686394253)
public Note(Long id, @NotNull String text, String comment, java.util.Date date, NoteType type) {
this.id = id;
this.text = text;
this.comment = comment;
this.date = date;
this.type = type;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@NotNull
public String getText() {
return text;
}
/** Not-null value; ensure this value is available before it is saved to the database. */
public void setText(@NotNull String text) {
this.text = text;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public java.util.Date getDate() {
return date;
}
public void setDate(java.util.Date date) {
this.date = date;
}
public NoteType getType() {
return type;
}
public void setType(NoteType type) {
this.type = type;
}
}
里边用到的一些类:
NoteType:
//一个枚举类,用于进行分类
public enum NoteType {
TEXT, LIST, PICTURE
}
NoteTypeConverter:
//实现PropertyConverter<P,D>接口中的方法,用于将NoteType这个非基本类型的数据转化为数据库可以保存的值
class NoteTypeConverter implements PropertyConverter<NoteType, String> {
//该方法将数据库中保存的String值转化为实体属性,String转化为NoteType类型
@Override
public NoteType convertToEntityProperty(String databaseValue) {
return NoteType.valueOf(databaseValue);
}
//将实体属性转化为数据库中保存的String,NoteType类型转化为String
@Override
public String convertToDatabaseValue(NoteType entityProperty) {
return entityProperty.name();
}
}
在定义完实体类之后就可以重新build一下项目,build过程中,GreenDao插件将会给我们自动生成NoteDao这个类,其中封装了Note类的数据库操作。其他的表也是相同的步骤。
增删改查:
定义完实体类之后就可以对这个进行对应的增删改查操作了。
需要修改的实体对象:
Note mNote = new Note(1L, "你好greenDao", "初次见面", new Date(System.currentTimeMillis()), NoteType.TEXT);
增:
//向数据库中增加一条数据
mNoteDao.insert(mNote);
删:
//从数据库中删除一条数据
mNoteDao.delete(mNote);
改:
//修改更新实体类
mNoteDao.update(mNote);
查:
//从数据库中查找文本为“你好greenDao”的记录,并将结果直接转化成List集合
QueryBuilder<Note> mNoteQueryBuilder = mNoteDao.queryBuilder();
mNoteQueryBuilder.where(NoteDao.Properties.Text.eq("你好greenDao"));
//同时QueryBuilder可以做与运算,或运算等
mNoteQueryBuilder.and(NoteDao.Properties.Id.eq(1),NoteDao.Properties.Id.eq(12));
List<Note> list = mNoteQueryBuilder.list();
Log.i(TAG, "onCreate: "+list.size());