greendao的基本操作
1.先配置项目的builder.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
classpath 'org.greenrobot:greendao-gradle-plugin:3.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
} allprojects {
repositories {
google()
jcenter()
}
} task clean(type: Delete) {
delete rootProject.buildDir
}
2.配置module.app的builder.gradle
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'
android {
compileSdkVersion
defaultConfig {
applicationId "com.lingdangmao.demo_zidingyi_textview"
minSdkVersion
targetSdkVersion
versionCode
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
} dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' //greendao
api 'org.greenrobot:greendao:3.0.1'
api 'org.greenrobot:greendao-generator:3.0.0'
}
greendao {
schemaVersion
daoPackage 'com.anye.greendao.gen'
targetGenDir 'src/main/java'
}
3.创建一个用户的实例类,创建完之后,编译一下,会自动生成文件
@Entity
public class User {
@Id
private Long id;
private String name;
}
4.编写myapplication类继承application
public class MyApplication extends Application {
private DaoMaster.DevOpenHelper mHelper;
private SQLiteDatabase db;
private DaoMaster mDaoMaster;
private DaoSession mDaoSession;
public static MyApplication instance; public static MyApplication getInstance(){
return instance;
} @Override
public void onCreate() {
super.onCreate();
instance =this;
setDataBase();
}
private void setDataBase(){
mHelper = new DaoMaster.DevOpenHelper(this, "notes-db", null);
db = mHelper.getWritableDatabase();
// 注意:该数据库连接属于 DaoMaster,所以多个 Session 指的是相同的数据库连接。
mDaoMaster = new DaoMaster(db);
mDaoSession = mDaoMaster.newSession(); } public DaoSession getmDaoSession() {
return mDaoSession;
} public SQLiteDatabase getDb() {
return db;
}
}
5.在activity中操作
//根据条件查询
UserDao mUserdao = MyApplication.getInstance().getmDaoSession().getUserDao();
Query<User> query = mUserdao.queryBuilder().where(UserDao.Properties.Id.eq()).build();
User u= query.unique(); //没有就是null
Log.d(TAG, "onCreate11: "+u);
Log.d(TAG, "onCreate11: "+u.getId());
Log.d(TAG, "onCreate11: "+u.getName()); //添加
User mUser=new User((long),"mike");
//mUserdao.insert(mUser); //删除
//long id= 2;
//mUserdao.deleteByKey(id); //改
mUser = new User((long),"anye0803");
mUserdao.update(mUser); //查询
List<User> users = mUserdao.loadAll();
for(int i=;i<users.size();i++){
Log.d(TAG, "onCreate: "+users.get(i).getId());
Log.d(TAG, "onCreate: 1"+users.get(i).getName());
}
完成,上面是最基本的,实际开发中用到再看
greendao的基本操作的更多相关文章
- ORM对象关系映射之GreenDAO源码解析
上一篇我们学习了GreenDAO的CRUD基本操作,可以说是非常的方便的,而且GreenDAO的效率和性能远远高于其它两款流行的ORM框架,下面是我从官网找的一副它们三个ORM框架之间的性能测试的直观 ...
- GreenDao 3.X之基本使用
在GreenDao 3.X之注解已经了解到GreenDao 3.0的改动及注解.对于数据库的操作,无异于增删改查等四个操作.下面我们将了解GreenDao 3.X如何使用? AbstractDao 所 ...
- GreenDao 数据库:使用Raw文件夹下的数据库文件以及数据库升级
一.使用Raw文件夹下的数据库文件 在使用GreenDao框架时,数据库和数据表都是根据生成的框架代码来自动创建的,从生成的DaoMaster中的OpenHelper类可以看出: public sta ...
- Key/Value之王Memcached初探:二、Memcached在.Net中的基本操作
一.Memcached ClientLib For .Net 首先,不得不说,许多语言都实现了连接Memcached的客户端,其中以Perl.PHP为主. 仅仅memcached网站上列出的语言就有: ...
- Android Notification 详解(一)——基本操作
Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...
- Android Notification 详解——基本操作
Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...
- 安卓GreenDao框架一些进阶用法整理
大致分为以下几个方面: 一些查询指令整理 使用SQL语句进行特殊查询 检测表字段是否存在 数据库升级 数据库表字段赋初始值 一.查询指令整理 1.链式执行的指令 return mDaoSession. ...
- 三、Redis基本操作——List
小喵的唠叨话:前面我们介绍了Redis的string的数据结构的原理和操作.当时我们提到Redis的键值对不仅仅是字符串.而这次我们就要介绍Redis的第二个数据结构了,List(链表).由于List ...
- 二、Redis基本操作——String(实战篇)
小喵万万没想到,上一篇博客,居然已经被阅读600次了!!!让小喵感觉压力颇大.万一有写错的地方,岂不是会误导很多筒子们.所以,恳请大家,如果看到小喵的博客有什么不对的地方,请尽快指正!谢谢! 小喵的唠 ...
随机推荐
- Apache Struts最新漏洞 远程代码执行漏洞预警 2018年11月08日
2018年11月8日,SINE安全监控检测中心,检测到Apache Struts官方更新了一个Struts漏洞补丁,这个漏洞是Apache Struts目前最新的漏洞,影响范围较广,低于Apache ...
- Linux Shell 与Linux常用命令
Linux的人际交互分为图形界面方式和命令行方式. Linux本身只是一个操作系统内核,而由X Window图形用户接口为Linux提供图形用户界面功能.可以把X Window理解为一个运行在Linu ...
- B -- POJ 1208 The Blocks Problem
参考:https://blog.csdn.net/yxz8102/article/details/53098575 https://www.cnblogs.com/tanjuntao/p/867892 ...
- python2.7入门---运算符 &案例
已经分享过变量类型的基本概念了,接下来就研究了一下运算符的基础知识.接下来我们就来看一下内容.举个简单的例子 4 +5 = 9 .例子中,4 和 5 被称为操作数,"+" ...
- ethtool speed HowTo : Change Speed and Duplex of Ethernet card in Linux
To change Speed and Duplex of an ethernet card, we can use ethtool - a Linux utility for Displaying ...
- 【廖雪峰老师python教程】——OOP
概述 面向对象编程——Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 数据封装.继承和多态 ...
- Linux-Shell脚本编程-学习-5-Shell编程-使用结构化命令-if-then-else-elif
if-then语句 if-then语句格式如下 if comman then command fi bash shell中的if语句可鞥会和我们接触的其他if语句的工作方式不同,bash shell的 ...
- 初探Qt Opengl【1】
最近一直在学习Qt的opengl绘图,看到好多资源都是关于以前的旧版本的, 我将我这几天学的的部分关于opengl的做个总结,也希望对需要学习的人有一定的帮助 在我的学习中,我主要用到一下三个方法 # ...
- MySQL☞where与like
1.无条件查询语句(查询表中所有数据) select * from 表名 2.查询某些列的数据(指定列) select 列名1,列名2,列名3 from 表名 3.条件查询语句 select 列名1, ...
- 51单片机实现外部中断00H-FFH、000-255、0000-1023
外部中断00H-FFH #include< reg51.h> #define uint unsigned int #define uchar unsigned char sfr P0M0 ...