不阅读源码之--GreenDao使用
好久没来了,因为平时都记在云笔记里,偶然想起51自己早期有写博客,不忘初心,今天来这留一篇吧,最近项目需要写数据库,所以这边对greendao做一些记录,也加深学习。
创新互联长期为上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为永昌企业提供专业的成都网站设计、做网站,永昌网站改版等技术服务。拥有10余年丰富建站经验和众多成功案例,为您定制开发。
配置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 ListqueryUserList() { UUserDao userDao = geReadDaoSession().getUUserDao(); QueryBuilder qb = userDao.queryBuilder(); List list = qb.list(); return list; } } 好懒啊,哎。
网页题目:不阅读源码之--GreenDao使用
网站网址:http://myzitong.com/article/igpeie.html