GreenDao 基本使用
来源:互联网 发布:linux系统怎么上网 编辑:程序博客网 时间:2024/04/26 03:38
GreenDao 基本使用
ONE Goal , ONE Passion !
第一步:将GreenDao引入到项目中(以Studio为例)
- 1.在Project–>budld.gradle中配置如下:
dependencies { classpath 'com.android.tools.build:gradle:2.0.0' // 这是需要配置的地方 classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'//GreenDao }
- 2.在Modle–>build.gradle中配置:
apply plugin: 'com.android.application' // 这是需要配置的地方---1 apply plugin: 'org.greenrobot.greendao'//GreenDao android { compileSdkVersion 23 buildToolsVersion "23.0.3" ... } // 需要配置的地方-----2 greendao { schemaVersion 1 // 当前数据库结构的版本 //daoPackage 'com.greendao.gen' // 生成的DAO,DaoMaster和DaoSession的包名。默认是实体的包名。 //targetGenDir 'src/main/java' //生成源文件的路径。默认源文件目录是在build目录中的(build/generated/source/greendao)。 // targetGenDirTest //生成的单元测试的根目录。 } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.4.0' // 需要配置的地方 -----3 GreenDao compile 'org.greenrobot:greendao:3.2.0'//GreenDao compile'org.greenrobot:greendao-generator:3.0.0' }
- 配置完成后,同步工程就可以了.
第二步:编写实体类
School类:
@Entity public class School { @Id private Long id; private String name; }
编写完这个普通的实体类后.
点击导航栏上的Build–>Make Project.
随后会看到这个实体类变身了. 如下:
/** * ONE Goal,ONE Passion! * Created by ${PK_Night} on 2016/12/12. * comment: */ @Entity public class School { @Id private Long id; private String name; @Transient private int tempUsageCount; // not persisted @Generated(hash = 2095953704) public School(Long id, String name) { this.id = id; this.name = name; } @Generated(hash = 1579966795) public School() { } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } }
帮我们生成了部分代码.
注:生成代码后,不要在更改这个文件
而且GreenDao已经帮我们生成了DAO等.DAO位置如下
第三步:开始增删CRUD
- 第一种:使用SqliteOpenHelper对象去操作.
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "mydb", null); SQLiteDatabase db = helper.getWritableDatabase(); // 原生sql 语句 // String sql = "insert into school values(3,'txx')"; // db.execSQL(sql); // 可执行曾,删,改 //db.rawQuery(); 执行查询
既然拿到了SQLiteDatabase对象,就可以直接使用sql操作了.如果你觉得GreenDao仅仅让我们方便地拿到 SQLiteDatabase那就太小看它了.它也可以像Hibernate一样,直接操作对象来进行CRUD.
- 第二种: 使用DaoSession 直接根据对象进行CRUD.
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "mydb2", null); SQLiteDatabase db = helper.getWritableDatabase(); DaoMaster daoMaster = new DaoMaster(db); DaoSession session = daoMaster.newSession();
增:
school = new School((long)1,"fy"); session.insert(school);
删:
school = new School((long)1,"fy"); session.delete(school);
改:
school = new School((long) 1, "fynight"); session.update(school);
查:
// 条件查询 List<School> schools = session.queryRaw(School.class, "where name = ?", new String[]{"fy"}); for (int i = 0; i < schools.size(); i++) { System.out.println("查询出来数据" + schools.get(i).getName()); } // 查询所有 List<School> school = session.loadAll(School.class); String schoolName = ""; for (int i = 0; i < school.size(); i++) { schoolName += school.get(i).getName() + ","; System.out.println("查询出来数据" + schoolName); }
简单的GreenDao简单demo已经 OK!接下来说一说 实体类注解
0 0
- GreenDao 的基本使用
- GreenDao 基本使用
- greenDao--3.2基本使用
- GreenDao-基本使用
- GreenDAO 3.0 基本使用
- greenDao 3.0基本使用
- greenDao的基本使用
- greenDAO基本使用详解
- GreenDao基本使用
- Android GreenDao基本使用
- Android GreenDao基本使用
- GreenDao基本使用_
- greenDao的基本使用
- GreenDao的基本使用
- greenDao的基本使用
- GreenDAO的使用基本篇
- 安卓 GreenDao 基本使用
- greenDao Orm 基本使用<一>
- Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get
- 剑指Offer---面试题17:合并两个有序链表
- a与b值交换
- hash排序的实现
- Linux Could not get lock 解决方案
- GreenDao 基本使用
- 用数组方法做的图片排序效果
- Android Studio导入外部so库
- myfirst
- Bootstrap图片旋转轮播的实现
- MediaPlayer播放音频讲解
- C语言数据类型和运算符
- 在Vivado HLS中综合时如何指定端口的综合类型
- 从Spyder3降级回Spyder2