您好,登錄后才能下訂單哦!
好久沒來了,因為平時都記在云筆記里,偶然想起51自己早期有寫博客,不忘初心,今天來這留一篇吧,最近項目需要寫數據庫,所以這邊對greendao做一些記錄,也加深學習。
配置android studio 和導入green到庫
project的gradle
repositories { jcenter() mavenCentral() // add repository } dependencies { classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin }
module的gradle
apply plugin: 'com.android.application' apply plugin: 'org.greenrobot.greendao' // apply plugin android { compileSdkVersion 25 buildToolsVersion "25.0.2" greendao { schemaVersion 1 targetGenDir 'src/main/java' } defaultConfig { applicationId "com.example.mygreendao" minSdkVersion 15 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.2.0' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' compile 'org.greenrobot:greendao:3.2.2' // add library } 吐槽一下,51cto的博客是真的踏馬不人性化,直接這寫了。 建立一個bean,這寫為UUser,寫完rebuild gradle ,會自動生成DaoMaster,Daosession,UUserDao(你實體 對應的Dao) @Entity public class UUser { String name; int age; @Id private Long id;//默認自增長,修改數據需要這個key,不然報錯 @Generated(hash = 1431141183) public UUser(String name, int age, Long id) { this.name = name; this.age = age; this.id = id; } @Generated(hash = 751422734) public UUser() { } set,get方法忽略 } 建立一個DBManager public class DBManager { private final static String dbName = "test_db"; private static DBManager mInstance; private DaoMaster.DevOpenHelper openHelper; private Context context; public DBManager(Context context) { this.context = context; openHelper = new DaoMaster.DevOpenHelper(context, dbName, null); } /** * 獲取單例引用 * * @param context * @return */ public static DBManager getInstance(Context context) { if (mInstance == null) { synchronized (DBManager.class) { if (mInstance == null) { mInstance = new DBManager(context); } } } return mInstance; } /** * 獲取可讀數據庫 */ private SQLiteDatabase getReadableDatabase() { if (openHelper == null) { openHelper = new DaoMaster.DevOpenHelper(context, dbName, null); } SQLiteDatabase db = openHelper.getReadableDatabase(); return db; } /** * 獲取可寫數據庫 */ private SQLiteDatabase getWritableDatabase() { if (openHelper == null) { openHelper = new DaoMaster.DevOpenHelper(context, dbName, null); } SQLiteDatabase db = openHelper.getWritableDatabase(); return db; } private DaoSession geWriteDaoSession() { DaoMaster daoMaster = new DaoMaster(getWritableDatabase()); DaoSession daoSession = daoMaster.newSession(); return daoSession; } private DaoSession geReadDaoSession() { DaoMaster daoMaster = new DaoMaster(getReadableDatabase()); DaoSession daoSession = daoMaster.newSession(); return daoSession; } /** * UUser * 插入一條記錄 * * @param user */ public void insertUser(UUser user) { UUserDao userDao = geWriteDaoSession().getUUserDao(); userDao.insert(user); } /** * UUser * 刪除一條記錄 * * @param user */ public void deleteUser(UUser user) { UUserDao userDao = geWriteDaoSession().getUUserDao(); userDao.delete(user); } /** * UUser * 更改一條記錄 * * @param user */ public void updateUser(UUser user) { UUserDao userDao = geWriteDaoSession().getUUserDao(); userDao.update(user); } /** * 查詢用戶列表 */ public List<UUser> queryUserList() { UUserDao userDao = geReadDaoSession().getUUserDao(); QueryBuilder<UUser> qb = userDao.queryBuilder(); List<UUser> list = qb.list(); return list; } } 好懶啊,哎。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。