当存在较多的数据库处理时,使用grrendao可以更方便,迅速,简捷的添加,查询数据.
雨林雨林作者-GreenDao3 使用说明
Jafir作者 - greendao3.0 多表关联
1.将项目中gradle中修改为:
2.将app中的build.gradle配置为:
apply plugin:'org.greenrobot.greendao'
android{
greendao {
schemaVersion42//数据库版本号
daoPackage'com.lijian.posmanager.database.greendao.gen' //自动生成的工具类的包名
targetGenDir'src/main/java' //路径
}
}
dependcies{
compile 'org.greenrobot:greendao:3.2.0'
// 使用数据库加密时添加
compile'net.zetetic:android-database-sqlcipher:3.5.6'
// 使用数据库升级辅助GreenDaoUpgradeHelper时添加compile'com.github.yuweiguocn:GreenDaoUpgradeHelper:v2.0.1'
}
3.通过数据库
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.开始创建对象,点击make project,将自动生成EmployeeDao,EmployeeDaoMaster等类;
eg:
@Entity //实体
public class Employee {
@Id(autoincrement =true) //id,自动增长,特别注意是引用数据类型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();//对象
.list();//集合对象
b.数据库关联
1.一对一
//员工权限与Employee一对一
@Entity
public class EmployeePermission {
@Id(autoincrement =true)
private Long perId;
private Long userId;
@ToOne(joinProperty ="userId")
private Employee employee;
private int permissionId;
}
2.一对多
#teacher类中
@ToMany(referencedJoinProperty ="tid")//指定与之关联的其他类的id
private List studnets;
#student类中
@Id
privateLong id;
privateLong tid;//这个就是外键 就是person的id
如何在代码中添加以及查询呢?
添加只需添加关联的id,不需set对象;
查询:直接get对象就可以;
如果出现对关联id对象的条件查询,如下:Bill与Member关联
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.更新数据库时
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);//修改的数据Dao
}
}