當(dāng)存在較多的數(shù)據(jù)庫(kù)處理時(shí),使用grrendao可以更方便,迅速,簡(jiǎn)捷的添加,查詢數(shù)據(jù).
Jafir作者 - greendao3.0 多表關(guān)聯(lián)
1.將項(xiàng)目中g(shù)radle中修改為:
2.將app中的build.gradle配置為:
apply plugin:'org.greenrobot.greendao'
android{
greendao {
?? schemaVersion42//數(shù)據(jù)庫(kù)版本號(hào)
? ? daoPackage'com.lijian.posmanager.database.greendao.gen' //自動(dòng)生成的工具類的包名
? ? targetGenDir'src/main/java' //路徑
}
}
dependcies{
compile 'org.greenrobot:greendao:3.2.0'
// 使用數(shù)據(jù)庫(kù)加密時(shí)添加
compile'net.zetetic:android-database-sqlcipher:3.5.6'
// 使用數(shù)據(jù)庫(kù)升級(jí)輔助GreenDaoUpgradeHelper時(shí)添加compile'com.github.yuweiguocn:GreenDaoUpgradeHelper:v2.0.1'
}
3.通過數(shù)據(jù)庫(kù)
public class iSqlite {
private MySqliteHelper mHelper =null;
private DaoMaster? ? ? mDaoMaster;
private DaoSession? ? mDaoSession;
private static iSqlite giSqlite =null;
private Database db;
public synchronized static iSqlite getInstance(Context context){
if(giSqlite==null )
giSqlite =new iSqlite(context);
return giSqlite;
}
public iSqlite(Context context){
init(context);
}
public boolean init(Context context){
if(mHelper==null ) {
mHelper =new MySqliteHelper(context,"/data/ljpos/data/database/ljpos.db",null);
//? ? ? ? ? ? ? ? mHelper = new MySqliteHelper(context,"ljpos.db", null);
? ? ? ? ? ? ? ? db =mHelper.getWritableDb();
mDaoMaster =new DaoMaster(db);
mDaoSession =mDaoMaster.newSession();
Log.e("www","database..............................");
}
return true;
}
public DaoSession getDaoSession() {
return mDaoSession;
}
public Database getDb() {
return db;
}
private static boolean mainTmpDirSet =false;
3.開始創(chuàng)建對(duì)象,點(diǎn)擊make project,將自動(dòng)生成EmployeeDao,EmployeeDaoMaster等類;
eg:
@Entity //實(shí)體
public class Employee {
?@Id(autoincrement =true) //id,自動(dòng)增長(zhǎng),特別注意是引用數(shù)據(jù)類型Long
? private? Long? ? ? ? ? userId;???
?? @Unique @NotNull?? //唯一,不為空
? ? private String? ? ? ? userName;
??? private String? ? ? ? password;
}
a.增刪改查代碼:
EmployeeDao employeeDao =sqlite.getDaoSession().getEmployeeDao();
1,employeeDao .insert(T t);
?2.employeeDao .deleteByKey(userId);? deleteAll();
? 3.employeeDao.loadAll() //查詢所有的
? employeeDao .queryBuilder()
?.where(EmployeeDao.Properties.UserId.eq(userId))//between(,)//在什么之間,like
.whereor()
.build()
.unique();//對(duì)象
.list();//集合對(duì)象
b.數(shù)據(jù)庫(kù)關(guān)聯(lián)
1.一對(duì)一
//員工權(quán)限與Employee一對(duì)一
@Entity
public class EmployeePermission {
@Id(autoincrement =true)
private Long perId;
private Long userId;
@ToOne(joinProperty ="userId")
private Employee employee;
private int permissionId;
}
2.一對(duì)多
#teacher類中
@ToMany(referencedJoinProperty ="tid")//指定與之關(guān)聯(lián)的其他類的id
private List studnets;
#student類中
@Id
privateLong id;
privateLong tid;//這個(gè)就是外鍵 就是person的id
如何在代碼中添加以及查詢呢?
添加只需添加關(guān)聯(lián)的id,不需set對(duì)象;
查詢:直接get對(duì)象就可以;
如果出現(xiàn)對(duì)關(guān)聯(lián)id對(duì)象的條件查詢,如下:Bill與Member關(guān)聯(lián)
billQueryBuilder =billDao.queryBuilder().where(BillDao.Properties.MemberId.gt(0),
BillDao.Properties.BillType.in(BillConstant.SALE_KEY,BillConstant.RETURN_SALE_KEY));
Join memberJoin=billQueryBuilder
?????????????????????????????? .join(MemberDao.Properties.MemberId,Member.class);
memberJoin.whereOr(MemberDao.Properties.CardNo.like("%" + strScreen +"%"),
MemberDao.Properties.MemberName.like("%" + strScreen +"%"));
billList =billQueryBuilder
.offset(offset)
.limit(limit)
.build()
.list();
5.更新數(shù)據(jù)庫(kù)時(shí)
public class MySqliteHelper extends DaoMaster.OpenHelper{
private static boolean mainTmpDirSet =false;
public MySqliteHelper(Context context,String name) {
???? super(context, name);
}
public MySqliteHelper(Context context,String name,SQLiteDatabase.CursorFactory factory) {
?????? super(context, name, factory);
}
@Override
? ? public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion) {
super.onUpgrade(db, oldVersion, newVersion);
???? MigrationHelper.migrate(db,BusinessDao.class,BillDao.class);//修改的數(shù)據(jù)Dao
}
}