好久没来了,因为平时都记在云笔记里,偶然想起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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。