GreenDao的使用
来源:互联网 发布:mac用什么做ppt 编辑:程序博客网 时间:2024/06/05 16:06
今天来学习下GreenDao的使用。
简单介绍下GreenDao:
性能高,号称Android最快的关系型数据库
- 内存占用小
库文件比较小,小于100K,编译时间低,而且可以避免65K方法限制
支持数据库加密 greendao支持SQLCipher进行数据库加密
- 简洁易用的API
GreenDao的使用步骤:
1、在gradle添加:classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
2、在app的Gradle中做如下配置
apply plugin: 'com.android.application'//使用greendaoapply plugin: 'org.greenrobot.greendao'android { compileSdkVersion 26 buildToolsVersion "26.0.1" defaultConfig { applicationId "demo.zouyan.com.greendaodemo" minSdkVersion 19 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } //greendao配置 greendao { //版本号,升级时可配置 schemaVersion 1//GreenDao自动生成代码的包名 daoPackage 'demo.zouyan.com.greendaodemo.gen'//自动生成代码的目录 targetGenDir 'src/main/java' } 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:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' //greendao依赖 compile 'org.greenrobot:greendao:3.2.0'}
3.gradle的配置完成后新建一个实体类
@Entitypublic class Person { //不能用int @Id(autoincrement = true) private Long id; @Unique private String name;}
@Entity :定义实体类,默认类名即为表名(这里为Person)
@Id :标明主键,括号里可以指定是否自增(注意:要注解为@Id的字段最好用Long型)
@NotNull : 不为null
@Unique :唯一约束
@ToMany : 一对多
@OrderBy : 排序
@Transient :不会创建到数据库表里
@Id :标明主键,括号里可以指定是否自增(注意:要注解为@Id的字段最好用Long型)
@NotNull : 不为null
@Unique :唯一约束
@ToMany : 一对多
@OrderBy : 排序
@Transient :不会创建到数据库表里
然后点击 build -> Make Moudle 'app',就会在gradle里设置的gen包下自动生成DaoMaster,DaoSession,PersonDao等文件以及Person类的有参无参构造还有getter、setter。
4、创建数据库
我的习惯做法是直接放在Application的onCreate里:
//创建数据库person.db" DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "person.db", null); //获取可写数据库 SQLiteDatabase db = helper.getWritableDatabase(); //获取数据库对象 DaoMaster daoMaster = new DaoMaster(db); //获取Dao对象管理者 session = daoMaster.newSession();
5、封装个Person表的操作类
public class DaoHelper { /** * 添加数据,如果有重复则覆盖 * */ public static void insert(Person person) { BaseAppliaction.getDaoInstant().getPersonDao().insertOrReplace(person); } /** * 删除数据 * */ public static void delete(long id) { BaseAppliaction.getDaoInstant().getPersonDao().deleteByKey(id); } /** * 更新数据 * */ public static void update(Person person) { BaseAppliaction.getDaoInstant().getPersonDao().update(person); } /** * 查询所有数据 * @return */ public static List<Person> queryAll() { return BaseAppliaction.getDaoInstant().getPersonDao().loadAll(); }}6、试着使用下:
Person person1 = new Person(1l,"张三"); DaoHelper.insert(person1); person1 = new Person(2l,"张四"); DaoHelper.insert(person1); person1 = new Person(3l,"张五"); DaoHelper.insert(person1); person1 = new Person(4l,"张六"); DaoHelper.insert(person1); List<Person> persons = DaoHelper.queryAll(); person1.setName("张器"); person1.setId(2l); DaoHelper.update(person1); persons = DaoHelper.queryAll(); DaoHelper.delete(1l); persons = DaoHelper.queryAll();
GreenDao数据库升级:
数据库升级的时候,直接修改实体类,然后去增加Gradle里的schemaVersion的值就行。但是这么做会导致原来的表被清空。如何避免这问题?前人已经有解决的方法,我在这再次记录下。
1、在reprositories里添加maven
allprojects { repositories { ...... maven { url "https://jitpack.io" } }}2、加一条依赖
compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v2.0.0'3.重写DaoMaster.OpenHelper里onUpgrade:
public class MySQLiteOpenHelper extends DaoMaster.OpenHelper{ public MySQLiteOpenHelper(Context context, String name) { super(context, name); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { super.onUpgrade(db, oldVersion, newVersion); MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() { @Override public void onCreateAllTables(Database db, boolean ifNotExists) { DaoMaster.createAllTables(db, ifNotExists); } @Override public void onDropAllTables(Database db, boolean ifExists) { DaoMaster.dropAllTables(db, ifExists); } //这里是个可变参数,有几个表,后面就可以跟跟几个class },PersonDao.class); }}4、修改Application里的:
//创建数据库person.db"// DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "person.db", null); MigrationHelper.DEBUG = true; //if you want see the log info,default is false MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this, "person.db");做完上面几步在数据库升级时就不会在销毁原来的表在重新创建表了,这样原有数据就能保存了。
最后附上Demo地址
阅读全文
1 0
- 关于greenDao的使用
- greenDao的使用教程
- GreenDAO的简单使用
- GreenDao的使用
- GreenDao的使用
- GreenDao的使用
- GreenDao的使用
- greendao的使用
- greenDAO的使用
- GreenDao的使用心得
- Greendao的使用
- GreenDao的使用详解
- GreenDao数据库的使用
- GreenDao的使用
- greenDAO的使用
- GreenDao的使用
- GreenDao的使用
- greenDAO的使用
- 计算机二级考试第三章知识点
- jsp九大内置对象 四大域
- Merge Two Binary Trees
- 后台date类型转换为json字符串时,返回前台页面的是long time值问题解决
- 神经网络与深度学习(4)
- GreenDao的使用
- LVS基础
- 一个使用 Python 的人工智能聊天机器人框架
- Python 字符串
- 贝塞尔曲线
- zookeeper原理
- Git免输入账号密码设置方法
- 【转】文本格式导入物料凭证模板
- 正则表达式