greenDao的基本使用
2017-08-18 18:44
381 查看
关于数据操作相关的库有不少,之前有写过用谷歌官方的一些工具类对数据库进行操作的demo,工程中需要使用greenDao来对数据库进行操作,因此写了个简单的demo,以及自己的一些理解。
前言
Android中的数据库使用的是Sqlite,至于这个玩意到底什么,不需要详细分析。你只需要知道,这是一种简单小巧轻量级的数据库类型,便于对一些简单的数据进行管理即可。这里不对谷歌官方操作的相关代码进行演示,只对greenDao的使用进行讲解。
PS:本笔记演示的greenDao版本为3.2.0
使用步骤
1 配置gradle相关
Project的build.gradle下:缺什么补什么不知道怎么加的自己琢磨...
实体类存在的Module的build.gradle下
先写实体类User
greenDao多使用注解,如果你要将某一实体存储到数据库中,需要先对实体进行编写。
@Entity表明该类是持久化的类【持久化含义,存入数据库文件中,作本地化处理】
@Id选择一个long或Long类型的属性作为该实体所对应数据库中数据表的主键【类型要是long】
@Generated写在构造方法前
将以上注解写好后,点击AS上的Make Project即可
然后可以发现,你的Module中多了个包,点开可以看到如下
![](http://upload-images.jianshu.io/upload_images/1780352-c9b28dcd2d517fc4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
greenDao自动生成的操作类
然后接下来就可以对数据库进行操作了
先上布局文件
再来个图
![](http://upload-images.jianshu.io/upload_images/1780352-ae7e6ad6ab7b3604.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/360)
greenDao_demo
然后是MainActivity
以上有一些判断是否成功的操作,因为greenDao并没有提供操作后的回调,所以我这里就只是手动判断一下
依次存入1/usher 、2/tom、3/harry 三组数据,然后提取数据库文件如下
![](http://upload-images.jianshu.io/upload_images/1780352-83ee29131f302a61.gif?imageMogr2/auto-orient/strip)
Demo演示
![](http://upload-images.jianshu.io/upload_images/1780352-eaec45a0725e7c06.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
作者:HELLO丶GUY
链接:http://www.jianshu.com/p/00d93c2d511f
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
前言
Android中的数据库使用的是Sqlite,至于这个玩意到底什么,不需要详细分析。你只需要知道,这是一种简单小巧轻量级的数据库类型,便于对一些简单的数据进行管理即可。这里不对谷歌官方操作的相关代码进行演示,只对greenDao的使用进行讲解。
PS:本笔记演示的greenDao版本为3.2.0
使用步骤
1 配置gradle相关
Project的build.gradle下:缺什么补什么不知道怎么加的自己琢磨...
buildscript { repositories { mavenCentral() } dependencies { classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1' } }
实体类存在的Module的build.gradle下
apply plugin: 'org.greenrobot.greendao' android { /*针对greenDao的一些配置*/ greendao { schemaVersion 1 //数据库版本号 daoPackage 'com.usher.greendao_demo.greendao.gen' //自动生成的工具类的包名 targetGenDir 'src/main/java' //路径 } } dependencies { /*greenDao框架*/ compile 'org.greenrobot:greendao:3.2.0' }
先写实体类User
@Entity public class User { @Id private long id; private String name; @Generated(hash = 586692638) public User() { } @Generated(hash = 1144922831) public User(long id, String name) { this.id = id; this.name = name; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
greenDao多使用注解,如果你要将某一实体存储到数据库中,需要先对实体进行编写。
@Entity表明该类是持久化的类【持久化含义,存入数据库文件中,作本地化处理】
@Id选择一个long或Long类型的属性作为该实体所对应数据库中数据表的主键【类型要是long】
@Generated写在构造方法前
将以上注解写好后,点击AS上的Make Project即可
然后可以发现,你的Module中多了个包,点开可以看到如下
![](http://upload-images.jianshu.io/upload_images/1780352-c9b28dcd2d517fc4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
greenDao自动生成的操作类
然后接下来就可以对数据库进行操作了
先上布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_margin="10dp" android:text="greenDao lib test" android:textColor="@android:color/black" /> <EditText android:id="@+id/etId" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户id" android:inputType="number" android:textSize="15sp" /> <EditText android:id="@+id/etName" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名" android:textSize="15sp" /> <Button android:id="@+id/btnAdd" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="新增" /> <Button android:id="@+id/btnDelete" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="删除" /> <Button android:id="@+id/btnQuery" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="查询" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_margin="10dp" android:text="查询结果" android:textColor="@android:color/black" /> <TextView android:id="@+id/tvQuery" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_margin="10dp" android:textColor="@android:color/black" /> </LinearLayout>
再来个图
![](http://upload-images.jianshu.io/upload_images/1780352-ae7e6ad6ab7b3604.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/360)
greenDao_demo
然后是MainActivity
public class MainActivity extends AppCompatActivity { UserDao userDao; private EditText etId; private EditText etName; private Button btnAdd; private Button btnDelete; private Button btnQuery; private TextView tvQuery; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); initView(); initDbHelp(); /*新增一条数据*/ btnAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String id = etId.getText().toString(); String name = etName.getText().toString(); if (isNotEmpty(id) && isNotEmpty(name)) { QueryBuilder qb = userDao.queryBuilder(); ArrayList<User> list = (ArrayList<User>) qb.where(UserDao.Properties.Id.eq(id)).list(); if (list.size() > 0) { Toast.makeText(MainActivity.this, "主键重复", Toast.LENGTH_SHORT).show(); } else { userDao.insert(new U 9b17 ser(Long.valueOf(id), name)); Toast.makeText(MainActivity.this, "插入数据成功", Toast.LENGTH_SHORT).show(); } } else { if (isEmpty(id) && isNotEmpty(name)) { Toast.makeText(MainActivity.this, "id为空", Toast.LENGTH_SHORT).show(); } if (isEmpty(name) && isNotEmpty(id)) { Toast.makeText(MainActivity.this, "姓名为空", Toast.LENGTH_SHORT).show(); } if (isEmpty(id) && isEmpty(name)) { Toast.makeText(MainActivity.this, "请填写信息", Toast.LENGTH_SHORT).show(); } } etId.setText(""); etName.setText(""); } }); /*删除指定数据*/ btnDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String id = etId.getText().toString(); if (isNotEmpty(id)) { userDao.deleteByKey(Long.valueOf(id)); QueryBuilder qb = userDao.queryBuilder(); ArrayList<User> list = (ArrayList<User>) qb.where(UserDao.Properties.Id.eq(id)).list(); if (list.size() < 1) { Toast.makeText(MainActivity.this, "删除数据成功", Toast.LENGTH_SHORT).show(); etId.setText(""); etName.setText(""); } } else { Toast.makeText(MainActivity.this, "id为空", Toast.LENGTH_SHORT).show(); } } }); /*查询数据*/ btnQuery.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String id = etId.getText().toString(); if (isNotEmpty(id)) { QueryBuilder qb = userDao.queryBuilder(); ArrayList<User> list = (ArrayList<User>) qb.where(UserDao.Properties.Id.eq(id)).list(); if (list.size() > 0) { String text = ""; for (User user : list) { text = text + "\r\n" + user.getName(); } tvQuery.setText(text); } else { tvQuery.setText(""); Toast.makeText(MainActivity.this, "不存在该数据", Toast.LENGTH_SHORT).show(); } etId.setText(""); etName.setText(""); } else { Toast.makeText(MainActivity.this, "id为空", Toast.LENGTH_SHORT).show(); } } }); } /*初始化数据库相关*/ private void initDbHelp() { DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "recluse-db", null); SQLiteDatabase db = helper.getWritableDatabase(); DaoMaster daoMaster = new DaoMaster(db); DaoSession daoSession = daoMaster.newSession(); userDao = daoSession.getUserDao(); } private void initView() { etId = (EditText) findViewById(R.id.etId); etName = (EditText) findViewById(R.id.etName); btnAdd = (Button) findViewById(R.id.btnAdd); btnDelete = (Button) findViewById(R.id.btnDelete); btnQuery = (Button) findViewById(R.id.btnQuery); tvQuery = (TextView) findViewById(R.id.tvQuery); } private boolean isNotEmpty(String s) { if (s != null && !s.equals("") || s.length() > 0) { return true; } else { return false; } } private boolean isEmpty(String s) { if (isNotEmpty(s)) { return false; } else { return true; } } }
以上有一些判断是否成功的操作,因为greenDao并没有提供操作后的回调,所以我这里就只是手动判断一下
依次存入1/usher 、2/tom、3/harry 三组数据,然后提取数据库文件如下
![](http://upload-images.jianshu.io/upload_images/1780352-83ee29131f302a61.gif?imageMogr2/auto-orient/strip)
Demo演示
![](http://upload-images.jianshu.io/upload_images/1780352-eaec45a0725e7c06.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
作者:HELLO丶GUY
链接:http://www.jianshu.com/p/00d93c2d511f
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
相关文章推荐
- GreenDao 基本使用
- GreenDao 3.2.0 的基本使用
- greenDao--3.2基本使用
- ORM框架--GreenDao 3.0基本使用指南
- 安卓 GreenDao 基本使用
- GreenDao 3.X之基本使用
- GreenDao的基本使用及实例
- greenDao的基本使用及用法详解
- GreenDao 3.X之基本使用
- Android操作数据库的框架-GreenDao基本使用记录
- GreenDao-基本使用
- Android GreenDao基本使用
- GreenDao的基本使用
- GreenDao的使用思路,greendao的基本和概念的讲解
- GreenDao基本使用_
- GreenDao之一基本使用(ToOne,ToMany都附源代码)
- Android数据库之greendao的基本使用
- greenDao 3.0基本使用
- GreenDAO 3.0 基本使用
- GreenDAO框架(二)基本使用篇