greenDAO数据库框架
GreenDao是一个为了帮助Android开发者操作SqLite数据库的开源项目。SQLite数据库是一个轻量级的关系型数据库。但是,对SQLite数据库操作需要大量的额外工作,例如,写SQL语句和解析查询出来的结果是一个很繁琐的任务。GreenDao将可以为你解决这一切工作:它可以把Java对象转换成数据库表(也就是所谓大家熟悉的ORM,做过Java的同学应该知道Hibernate框架),用GreenDao可以通过java简单对象的API对数据进行更新、删除以及查询获取的java对象,让你花更多时间解决真的的业务问题。
greenDAO is an open source project to help Android developers working with data stored in SQLite. SQLite is an awesome embedded relational database. However, developing for it requires alot of additional work. Writing SQL and parsing query results are quite tedious tasks. greenDAO will do the work for you: it maps Java objects to database tables (often called ORM). This way you can store, update, delete, and query for Java objects using a simple object oriented API. Save time and focus on real problems!
DAO Code Generation Project
Core Classes
一、 GreenDao的设计目标
最大的性能(最快的Android ORM解决方案)
易用的API
高度优化
最小的内存消耗
最小的项目文件,专注于最需要部分
Maximum performance (probably the fastest ORM for Android)
Easy to use APIs
Highly optimized for Android
Minimal memory consumption
Small library size, focus on the essentials
特征
1 | `对象/映射关系(ORM) |
``greenDAO’s essence is offer an object oriented interface to data stored in the relational database SQLite. Just define for data model, and greenDAO will create Java data objects (entities) and DAOs (data access objects). This will save you a lot of boring code that just moves data back and forth. Beyond that, greenDAO offers some advanced ORM features like a session cache, eager loading, and active entities.
1 |
|
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:design:22.2.0' compile 'com.android.support:appcompat-v7:22.2.0' compile 'com.android.support:recyclerview-v7:22.2.0' compile 'com.android.support:cardview-v7:22.2.0' compile 'com.github.bumptech.glide:glide:3.6.0' compile 'de.hdodenhof:circleimageview:1.3.0' compile 'com.android.support:support-v4:22.2.0' compile 'com.github.traex.rippleeffect:library:1.3' compile 'de.greenrobot:greendao:2.0.0' compile 'com.umeng.analytics:analytics:latest.integration' }
1 |
|
sourceSets { main { java { srcDir 'src/main/java' srcDir 'src/main/java-gen' } } }
1 |
|
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'de.greenrobot:greendao-generator:2.0.0' }
1 |
|
`public class GreenDaoGeneraterDemo {
public static void main(String[] args) throws Exception {
Schema schema = new Schema(2, "com.bluesky.gzrszp");
addArticle(schema);
new DaoGenerator().generateAll(schema, "../AisenWeiBo/gzrszp/src/main/java-gen");
}
/**
- 数据库对应表实体
- @param schema
*/
private static void addArticle(Schema schema) {
Entity note = schema.addEntity("GreenArticle");
note.addIdProperty();
note.addStringProperty("title").notNull();
note.addStringProperty("href");
note.addStringProperty("date");
note.addStringProperty("content");
note.addStringProperty("color");
note.addIntProperty("position");
}
}
`
1 | - 8、在相应的Android项目java-gen文件夹下看到生成对应的java对象、javaDAO、DAOSession和DAOMaster类 |
sourceSets { main { java { srcDir 'src/main/java' srcDir 'src/main/java-gen' } } }
- 4、如果新增字段、修改字段或者删除字段,要重新删除数据库或者表。这样后面操作才不会出现错误。