Android常用的數(shù)據(jù)庫框架OrmLite,GreenDao,Realm等等,當(dāng)然數(shù)據(jù)庫語句熟悉的完全可以使用原生的SQLite,但是個人還是喜歡使用GreenDao,方便輕量,支持緩存,支持?jǐn)?shù)據(jù)庫加密,稍微記錄一下,畢竟不是所有的項目都使用數(shù)據(jù)庫,很多項目SP就能完全夠用,
環(huán)境配置:
在project的build.gradle文件里
dependencies {
.....
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
在App的build.gradle文件下進(jìn)行如下配置 (注釋的地方就是要添加的東西)
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao' // apply plugin
android {
........
defaultConfig {
........
}
greendao {
schemaVersion 1 //數(shù)據(jù)庫版本
targetGenDir 'src/main/java' //指定生成代碼的目錄
daoPackage //生成代碼到具體包下
}
}
dependencies {
...............
implementation 'org.greenrobot:greendao:3.2.2' // add library
}
環(huán)境配置完畢,然后同步一下,ok了
在Application里面配置創(chuàng)建數(shù)據(jù)庫
public class MyApplication extends Application {
private static DaoSession daoSession;
@Override
public void onCreate() {
super.onCreate();
......
initDatebase();
}
private void initDatebase(){
//創(chuàng)建數(shù)據(jù)庫user.db
DaoMaster.DevOpenHelper helper=new DaoMaster.DevOpenHelper(this,"user.db",null);
//獲取可寫數(shù)據(jù)庫
SQLiteDatabase db=helper.getWritableDatabase();
//獲取數(shù)據(jù)庫對象
DaoMaster daoMaster=new DaoMaster(db);
//獲取Dao對象管理者
daoSession=daoMaster.newSession();
}
public static DaoSession getDaoInstant(){
return daoSession;
}
}
數(shù)據(jù)庫創(chuàng)建好了,然后就是建表,GreenDao3.0之前還是有點麻煩,3.0之后就直接寫實體類,用注解就好了
@Entity //@Entity:告訴GreenDao該對象為實體矾瑰,只有被@Entity注釋的Bean類才能被dao類操作
public class User {
//@Id:對象的Id,使用Long類型作為EntityId隘擎,否則會報錯殴穴。(autoincrement = true)表示主鍵會自增,如果false就會使用舊值
@Id(autoincrement = true)
private Long id;
//@Unique:該屬性值必須在數(shù)據(jù)庫中是唯一值
//@NotNull:屬性不能為空
@Unique @NotNull
private int UserId;
private String userName;
private String userHead;
private int age;
private String Token;
//@Property:可以自定義字段名嵌屎,注意外鍵不能使用該屬性
//@Transient:使用該注釋的屬性不會被存入數(shù)據(jù)庫的字段中
//@Generated:編譯后自動生成的構(gòu)造函數(shù)、方法等的注釋恍涂,提示構(gòu)造函數(shù)宝惰、方法等不能被修改
}
寫好注解build一下實體類會生成get,set方法和DaoMaster,DaoSession,HistoryDataDao類
----增刪改查
增
MyApplication.getDaoInstant().getUserDao().insert(user);
刪
MyApplication.getDaoInstant().getUserDao().deleteByKey(id);
改
MyApplication.getDaoInstant().getUserDao().update(user);
查
MyApplication.getDaoInstant().getUserDao().loadAll();
對了數(shù)據(jù)庫創(chuàng)建新表什么的,在配置數(shù)據(jù)庫版本的時候要+1
一對一建表,通過外鍵與另外一個表建立關(guān)系,在上面User表中添加
@Entity //@Entity:告訴GreenDao該對象為實體,只有被@Entity注釋的Bean類才能被dao類操作
public class User {
//@Id:對象的Id再沧,使用Long類型作為EntityId尼夺,否則會報錯。(autoincrement = true)表示主鍵會自增炒瘸,如果false就會使用舊值
@Id(autoincrement = true)
private Long id;
//@Unique:該屬性值必須在數(shù)據(jù)庫中是唯一值
//@NotNull:屬性不能為空
@Unique @NotNull
private int UserId;
private String userName;
private String userHead;
private int age;
private String Token;
private long BI_ID ;
@ToOne(joinProperty = "BI_ID") //1對1,當(dāng)然也有 一對多的 @ToMany
private BankInfo bankinfo;
//@Property:可以自定義字段名淤堵,注意外鍵不能使用該屬性
//@Transient:使用該注釋的屬性不會被存入數(shù)據(jù)庫的字段中
//@Generated:編譯后自動生成的構(gòu)造函數(shù)、方法等的注釋顷扩,提示構(gòu)造函數(shù)拐邪、方法等不能被修改
}
bankInfo實體類
@Entity
public class BankInfo {
@Id
private Long id;
private int bankId;
private String bankName;
}
查詢語句通過
MyApplication.getDaoInstant().getBankInfoDao().queryBuilder()
.where(BankInfoDao.Properties.Id.eq(BI_ID)).list();
能查詢到對應(yīng)BI_ID 的銀行卡信息數(shù)據(jù)