GreenDAO是一款非要流行的android平台上的数据库框架,性能优秀,代码简洁。
GreenDao的github地址为:https://github.com/greenrobot/greenDAO
GreenDao的官方网站为:http://greendao-orm.com/
我们下载回来的文件中,有一个项目是DaoExampleGenerator,专门用来生成Android使用的GreenDao文件。我们也可以自己创建一个Java项目,导入greenDao的
的freemarker.jar和greenDAO-generator.jar。这两个包是相当于生成器,用来生成你需要的文件,DaoMaster.java、DaoSession.java、Entity.java和EntityDao.java四个文件。
在Java项目中,我打算建立一个简单的个人资料实体类。
public class ExampleDaoGenerator {
public static void main(String[] args) throws Exception {
// 创建一个数据库版本号为1,默认包路径为:shawn.greenrobot.personinfo
Schema schema = new Schema(1, "shawn.greenrobot.personinfo");
CreateEntity(schema);
// 生成文件,指定文件所在的路径,这个路径必须存在,否则会出错
new DaoGenerator().generateAll(schema, "../DaoExample/personinfo");
}
/**
* 创建一个个人信息实体类
*
* @param schema
*/
private static void CreateEntity(Schema schema) {
// 添加实体类,一个实体类可以看成一张表,类名即是表名,属性名即是列名
Entity personInfo = schema.addEntity("PersonInfo");// 个人信息实体
personInfo.addIdProperty();// id
personInfo.addStringProperty("name").notNull();// 姓名
personInfo.addStringProperty("idNumber");// 身份证号
personInfo.addIntProperty("sex");// 性别
personInfo.addDateProperty("birthday");// 生日
personInfo.addIntProperty("age");// 性别
personInfo.addStringProperty("comment");// 备注
}
}
运行之后,该工程会在等级的目录DaoExample/personinfo文件夹下生成我们需要的几个文件。
生成我们需要的greenDao文件之后,就可以导入Android项目中使用了,当然同时还要在工程中引入greenDAO.jar文件。
- DaoMaster保存了SqliteDatabase对象,同时管理一些DAO类。
- DaoSession管理所有可用的DAO对象,我们通过get方法来获取。
- EntityDao是数据访问对象,用于实体的增删改查。
- Entity是实体类。
具体的功能我们直接进去查看代码就一目了然了。
这样我们就可以在Android工程下使用GreenDao了。
private void test() {
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "person_info-db", null);
SQLiteDatabase database = helper.getWritableDatabase();
DaoMaster personDaoMaster = new DaoMaster(database);
DaoSession personDaoSession = personDaoMaster.newSession();
PersonInfoDao personInfoDao = personDaoSession.getPersonInfoDao();
DaoMaster.createAllTables(database, true);
// 插入十组人员数据
for (int i = 0; i < 10; i++) {
PersonInfo personInfo = new PersonInfo(null, "person:" + i, "idNumber" + i, i % 2, new Date(), 16 + i,
"ADD on " + new Date().toLocaleString());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.e("personInfo", "_id: " + personInfoDao.insert(personInfo) + "; name:" + personInfo.getName());// 打印出 id
}
// 查询,获取性别为1的所有人员
List<PersonInfo> infos = personInfoDao.queryBuilder().where(PersonInfoDao.Properties.Sex.eq(1)).list();
Log.e("personInfo", "count: " + personInfoDao.queryBuilder().count());
Log.e("personInfo", "sex = 1: " + infos.size());
}
上面代码的功能是建一个名为“person_info-db”的数据库,通过PersonInfoDao对象向数据库插入了十个人员的资料,进行了一些查询。
运行的结果如下:
插入时返回id。查到表里的总数为10人,性别为1的人数为5。
我们也可以用传统的方法进行操作,比如,我想获取所有人员的信息,通过Cursor来实现。
private List<PersonInfo> getAllPersonInfos() {
DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "person_info-db", null);
SQLiteDatabase database = helper.getWritableDatabase();
DaoMaster personDaoMaster = new DaoMaster(database);
DaoSession personDaoSession = personDaoMaster.newSession();
PersonInfoDao personInfoDao = personDaoSession.getPersonInfoDao();
String nameColumn = PersonInfoDao.Properties.Name.columnName;
String orderBy = nameColumn + " COLLATE LOCALIZED ASC";// 按本地语言升序
Cursor personCursor = database.query(PersonInfoDao.TABLENAME, personInfoDao.getAllColumns(), null, null, null,
null, orderBy);
List<PersonInfo> personInfos = new ArrayList<PersonInfo>();
PersonInfo info = null;
while (personCursor.moveToNext()) {
info = new PersonInfo();
info.setName(personCursor.getString(personCursor.getColumnIndex(PersonInfoDao.Properties.Name.columnName)));
info.setId(personCursor.getLong(personCursor.getColumnIndex(PersonInfoDao.Properties.Id.columnName)));
info.setAge(personCursor.getInt(personCursor.getColumnIndex(PersonInfoDao.Properties.Age.columnName)));
info.setSex(personCursor.getInt(personCursor.getColumnIndex(PersonInfoDao.Properties.Sex.columnName)));
info.setIdNumber(personCursor.getString(personCursor
.getColumnIndex(PersonInfoDao.Properties.IdNumber.columnName)));
info.setBirthday(new Date(personCursor.getLong(personCursor
.getColumnIndex(PersonInfoDao.Properties.Birthday.columnName))));
info.setComment(personCursor.getString(personCursor
.getColumnIndex(PersonInfoDao.Properties.Comment.columnName)));
personInfos.add(info);
Log.e("personInfo", "name:" + info.getName() + "; birthday:" + info.getBirthday().toLocaleString());
}
personCursor.close();
return personInfos;
}
Dao里面集成了增删改查的方法,我们也可以按照自己的具体需求进行拓展。