AndroidGreenDao3的基本使⽤及版本升级更新先上图:
使⽤流程:
1、
①在App的adle⽂件内添加内容进⾏配置
apply plugin: 'com.android.application'
apply plugin: 'dao'
android {
...
greendao{
schemaVersion 1  //数据库版本号,升级数据库需要修改版本号
daoPackage''  //⼀般为app包名+⽣成⽂件的⽂件夹名
targetGenDir 'src/main/java'  //⾃动⽣成的greendao代码存放路径
}
}
dependencies {
...
compile 'obot:greendao:3.2.2' // add library
compile 'obot:greendao-generator:3.2.2'
}
②在项⽬的adle⽂件内添加内容进⾏配置
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'ls.build:gradle:3.2.0'
classpath 'obot:greendao-gradle-plugin:3.2.2' // add plugin
}
2、数据库初始化管理类(初始化时直接调⽤)
public class DbManager {
public static final boolean ENCRYPTED = true; // 是否加密
private static final String DB_NAME = "Wallet.db"; //数据库名
private static DbManager mDbManager;
private static DaoMaster.DevOpenHelper mDevOpenHelper; // 需要升级时⽤MySQLiteOpenHelper替换DaoMaster.DevOpenHelper,保障更新版本时保存⽼数据
private static DaoMaster mDaoMaster;
private static DaoSession mDaoSession;
private Context mContext;
private DbManager(Context context) {
this.mContext = context; // 初始化数据库信息
mDevOpenHelper = new DaoMaster.DevOpenHelper(context, DB_NAME);
getDaoMaster(context);
getDaoSession(context);
}
public static DbManager getInstance(Context context) {
if (null == mDbManager) {
synchronized (DbManager.class) {
if (null == mDbManager) {
mDbManager = new DbManager(context); } } }
return mDbManager;
}
/** * 获取可读数据库 *
* @param context
* @return */
public static SQLiteDatabase getReadableDatabase(Context context) {
if (null == mDevOpenHelper) {
getInstance(context); }
ReadableDatabase();
}
/** * 获取可写数据库 *
* @param context
* @return */
public static SQLiteDatabase getWritableDatabase(Context context) { if (null == mDevOpenHelper) { getInstance(context); }
WritableDatabase();
}
/** * 获取DaoMaster *
* 判断是否存在数据库,如果没有则创建数据库
* @param context
* @return */
public static DaoMaster getDaoMaster(Context context) {
if (null == mDaoMaster) { synchronized (DbManager.class) {
if (null == mDaoMaster) {
//升级对应的代码,请关注下⾯的⼀个类
MyOpernHelper helper = new MyOpernHelper(context,DB_NAME,null);
mDaoMaster = new WritableDatabase()); } } } return mDaoMaster;
}
/** * 获取DaoSession *
* @param context
* @return */
public static DaoSession getDaoSession(Context context) {
if (null == mDaoSession) {
synchronized (DbManager.class) {
mDaoSession = getDaoMaster(context).newSession(); } }
return mDaoSession; }
}
如:调⽤⽅式可在继承Application的类中初始化时调⽤:
3、定义实体类内容(对应于数据库的表)
//定义好变量和注解后直接在android studio 中Build Make project就能⾃动⽣成get 和set⽅法以及如下图gen⽂件夹的内容
@Entity
public class WalletInfo {
@Id
private Long id;
private String name;
}
4、操作本地数据库
//调⽤  DaoSession().getWalletInfoDao() //返回对象的Dao类进⾏增删改查
private void insertData(WalletInfo walletInfo) {DaoSession(context).getWalletInfoDao().insert(walletInfo);}
private void deleteAll(){ DaoSession(context).getWalletInfoDao().deleteAll();}
//这⾥有个坑得注意:需保证⾃增长id不为空
private void updateData(WalletInfo walletInfo){DaoSession(context).getWalletInfoDao().update(walletInfo);}
private List<WalletInfo> selectData(){DaoSession(context).getWalletInfoDao().loadAll();}
5、数据库升级防⽌数据丢失问题
// ⾃定义openHelper类,继承DaoMaster.DevOpenHelper,重写onUpgrade(Database db, int oldVersion, int newVersion)⽅法,在该⽅法中使⽤MigrationHelper进⾏数据库升级以及数据迁移。
public class MySQLiteOpenHelper extends DaoMaster.OpenHelper{
public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
super(context, name, factory);
}
@Override
public void onUpgrade(Database db, int oldVersion, int newVersion) {
//把需要管理的数据库表DAO作为最后⼀个参数传⼊到⽅法中(只传更改过的表,新增的不⽤)
MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() {
@Override
public void onCreateAllTables(Database db, boolean ifNotExists) {
}
@Override
public void onDropAllTables(Database db, boolean ifExists) {
DaoMaster.dropAllTables(db, ifExists);
}
define的基本用法},  WalletInfoDao.class);//可添加多个改动过的表对象Dao
}
}
/**
* Created by xiaopao on 2018/10/25.
* 解决greenDao的数据库更新升级(不删除原数据)
*/
public class MigrationHelper {
public static boolean DEBUG = false;
private static String TAG = "MigrationHelper";
private static final String SQLITE_MASTER = "sqlite_master";
private static final String SQLITE_TEMP_MASTER = "sqlite_temp_master";
private static WeakReference<ReCreateAllTableListener> weakListener;
public interface ReCreateAllTableListener{
void onCreateAllTables(Database db, boolean ifNotExists);
void onDropAllTables(Database db, boolean ifExists);
}
public static void migrate(SQLiteDatabase db, Class<? extends AbstractDao<?, ?>>... daoClasses) {
printLog("【The Old Database Version】" + db.getVersion());
Database database = new StandardDatabase(db);
migrate(database, daoClasses);
}
public static void migrate(SQLiteDatabase db, ReCreateAllTableListener listener, Class<? extends AbstractDao<?, ?>>... daoClasses) {        weakListener = new WeakReference<>(listener);
migrate(db, daoClasses);
}
public static void migrate(Database database, ReCreateAllTableListener listener, Class<? extends AbstractDao<?, ?>>... daoClasses) {        weakListener = new WeakReference<>(listener);
migrate(database, daoClasses);
}
public static void migrate(Database database, Class<? extends AbstractDao<?, ?>>... daoClasses) {
printLog("【Generate temp table】start");

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。