主流的ORM框架
greenDAO3.1 安装
配置build.gradle
在model的build.gradle文件添加如下配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| buildscript { repositories { mavenCentral() } dependencies { classpath 'org.greenrobot:greendao-gradle-plugin:3.1.0' } }
apply plugin: 'org.greenrobot.greendao'
dependencies { compile 'org.greenrobot:greendao:3.1.0' }
|
数据库的设置(可选项)
在model的build.gradle文件添加如下配置
1 2 3 4 5 6 7 8
| android { ... } greendao { schemaVersion 2 ... }
|
参数解释
- schemaVersion: 数据库schema版本,也可以理解为数据库版本号(默认1)
- daoPackage:设置DaoMaster 、DaoSession、Dao包名(默认为你实体的名字)
- targetGenDir:设置DaoMaster 、DaoSession、Dao目录(默认build/generated/source/greendao)
- testsGenSrcDir:设置生成单元测试目录(默认src/androidTest/java)
- generateTests:设置自动生成单元测试用例
新建实体
添加注解@
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Entity public class test { @Id(autoincrement = true) private long tes;
public long getTes() { return tes; }
public void setTes(long tes) { this.tes = tes; } }
|
编译运行
- targetGenDir目录(默认build/generated/source/greendao)下自动生成自动生成DaoMaster.java 、DaoSession.java、Dao.java
1 2 3 4 5 6 7 8
| @Generated(hash = 838475940) public test(long tes) { this.tes = tes; }
@Generated(hash = 1102163179) public test() { }
|
简单的使用
新建DBManager.java管理类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| 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); }
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; }
public void insertUser(test test) { DaoMaster daoMaster = new DaoMaster(getWritableDatabase()); DaoSession daoSession = daoMaster.newSession(); testDao userDao = daoSession.getTestDao(); userDao.insert(test); }
public List<test> queryUserList() { DaoMaster daoMaster = new DaoMaster(getReadableDatabase()); DaoSession daoSession = daoMaster.newSession(); testDao userDao = daoSession.getTestDao(); QueryBuilder<test> qb = userDao.queryBuilder(); List<test> list = qb.list(); return list; } }
|
测试代码
1 2 3 4 5
| private void daotest(){ DBManager dbManager=DBManager.getInstance(this); dbManager.insertUser(new test(10)); Log.i("sssss","ddddd"+dbManager.queryUserList().get(0).getTes()); }
|
参考
Android数据存储之GreenDao 3.0 详解
官方教程