今天一大早收到GreenDao 3.0 正式发布的消息,自从2014年接触GreenDao至今,项目中一直使用GreenDao框架处理数据库操作,本人使用数据库路线 Sqlite---->OrmLite---->GreenDao。今天白天一直在忙着公司的项目需求,只能晚上加班来学习最新的GreenDao 3.0使用方式了。
greenDAO是一个对象关系映射(ORM)的框架,能够提供一个接口通过操作对象的方式去操作关系型数据库,它能够让你操作数据库时更简单、更方便。如下图所示:
官网地址:http://greenrobot.org/greendao/
github:https://github.com/greenrobot/greenDAO
使用过GreenDao的同学都知道,3.0之前需要通过新建GreenDaoGenerator工程生成Java数据对象(实体)和DAO对象,非常的繁琐而且也加大了使用成本。
GreenDao 3.0最大的变化就是采用注解的方式通过编译方式生成Java数据对象和DAO对象。
buildscript { repositories { mavenCentral() } dependencies { classpath 'org.greenrobot:greendao-gradle-plugin:3.0.0' } } apply plugin: 'org.greenrobot.greendao' dependencies { compile 'org.greenrobot:greendao:3.0.1' }
@Entity public class User { @Id private long id; private String name; private int age; //下面省去了 setter/getter }
此时编译一下自动生成DaoMaster 、DaoSession、Dao,如图所示 默认位置:
比如上面想指定生成DaoMaster 、DaoSession、Dao位置
greendao { targetGenDir 'src/main/java' }
这里声明一个数据库管理者单例
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; }
/** * 插入一条记录 * * @param user */ public void insertUser(User user) { DaoMaster daoMaster = new DaoMaster(getWritableDatabase()); DaoSession daoSession = daoMaster.newSession(); UserDao userDao = daoSession.getUserDao(); userDao.insert(user); } /** * 插入用户集合 * * @param users */ public void insertUserList(List<User> users) { if (users == null || users.isEmpty()) { return; } DaoMaster daoMaster = new DaoMaster(getWritableDatabase()); DaoSession daoSession = daoMaster.newSession(); UserDao userDao = daoSession.getUserDao(); userDao.insertInTx(users); }
/** * 删除一条记录 * * @param user */ public void deleteUser(User user) { DaoMaster daoMaster = new DaoMaster(getWritableDatabase()); DaoSession daoSession = daoMaster.newSession(); UserDao userDao = daoSession.getUserDao(); userDao.delete(user); }
/** * 更新一条记录 * * @param user */ public void updateUser(User user) { DaoMaster daoMaster = new DaoMaster(getWritableDatabase()); DaoSession daoSession = daoMaster.newSession(); UserDao userDao = daoSession.getUserDao(); userDao.update(user); }
/** * 查询用户列表 */ public List<User> queryUserList() { DaoMaster daoMaster = new DaoMaster(getReadableDatabase()); DaoSession daoSession = daoMaster.newSession(); UserDao userDao = daoSession.getUserDao(); QueryBuilder<User> qb = userDao.queryBuilder(); List<User> list = qb.list(); return list; } /** * 查询用户列表 */ public List<User> queryUserList(int age) { DaoMaster daoMaster = new DaoMaster(getReadableDatabase()); DaoSession daoSession = daoMaster.newSession(); UserDao userDao = daoSession.getUserDao(); QueryBuilder<User> qb = userDao.queryBuilder(); qb.where(UserDao.Properties.Age.gt(age)).orderAsc(UserDao.Properties.Age); List<User> list = qb.list(); return list; }
DBManager dbManager = DBManager.getInstance(this); for (int i = 0; i < 5; i++) { User user = new User(); user.setId(i); user.setAge(i * 3); user.setName("第" + i + "人"); dbManager.insertUser(user); } List<User> userList = dbManager.queryUserList(); for (User user : userList) { Log.e("TAG", "queryUserList--before-->" + user.getId() + "--" + user.getName() +"--"+user.getAge()); if (user.getId() == 0) { dbManager.deleteUser(user); } if (user.getId() == 3) { user.setAge(10); dbManager.updateUser(user); } } userList = dbManager.queryUserList(); for (User user : userList) { Log.e("TAG", "queryUserList--after--->" + user.getId() + "---" + user.getName()+"--"+user.getAge()); }
运行结果
总结:
本文主要介绍了GreenDao 3.0使用注解的情况,以及实现了简单的增删改查,接下来会更一步学习GreenDao更为复杂的使用的方式。