case R.id.add:   //添加数据Person item=new Person(null,"wang","18");PersonHelper.getInstance(this).addInfo(item);break;case R.id.delete://删除id是1的数据PersonHelper.getInstance(this).deleteInfo(1);break;case R.id.delete_table:// 删除这个表PersonHelper.getInstance(this).clearInfo();break;case R.id.update:      //修改 id为1的数据//id需要强转为longPerson item2=new Person((long)1,"up","10");PersonHelper.getInstance(this).updateInfo(item2);break;case R.id.query:   //查询id为1的name字段的值String name=PersonHelper.getInstance(this).getTypeValue(1);text.setText(name);break;case R.id.query_list: //查询所有数据List<Person> list = PersonHelper.getInstance(this).getInfoList2();Person person = new Person();StringBuilder str = new StringBuilder();//和String 它这个必须new对象。不然就是空指针。for(int i=0;i<list.size();i++){person=list.get(i);String names=person.getName();String ages=person.getAge();Long id=person.getId();str.append(names+id+"");    System.out.println("---查询---->"+names + id+"");}if(TextUtils.isEmpty(str)){text.setText("没有数据");}else{text.setText(str);}break;case R.id.query_have:  //查询有没有id为1的数据boolean flag=PersonHelper.getInstance(this).isHave(1);if(flag==true){text.setText("true");}else{text.setText("false");}break;}
import android.content.Context;import com.example.greendaos_dao.DaoMaster;
import com.example.greendaos_dao.DaoMaster.OpenHelper;
import com.example.greendaos_dao.DaoSession;public class DbHelper {private static DaoMaster daoMaster;private static DaoSession daoSession;public DbHelper() {}/** * 取得DaoMaster   ** @param context    * @return*/public static DaoMaster getDaoMaster(Context context)   {   if (daoMaster == null)  {OpenHelper helper = new DaoMaster.DevOpenHelper(context, "my_db", null);daoMaster = new DaoMaster(helper.getWritableDatabase());    }   return daoMaster;   }/** * 取得DaoSession*   * @param context    * @return*/public static DaoSession getDaoSession(Context context) {if (daoSession == null) {   if (daoMaster == null)  {   daoMaster = getDaoMaster(context);  }   daoSession = daoMaster.newSession();    }return daoSession;}
}
import java.util.List;
import java.util.Map;import com.example.greendaos_dao.DaoSession;
import com.example.greendaos_dao.Person;
import com.example.greendaos_dao.PersonDao;
import com.example.greendaos_dao.PersonDao.Properties;import de.greenrobot.dao.query.DeleteQuery;
import de.greenrobot.dao.query.QueryBuilder;
import android.content.Context;
//10个方法
public class PersonHelper {private static Context mContext;private static PersonHelper instance;private PersonDao personDao;      //先去掉import包 然后改 PersonDao personDao Person; ctrl+f全修改public PersonHelper() {// TODO Auto-generated constructor stub}public static PersonHelper getInstance(Context context) {   if (instance == null){   instance = new PersonHelper();if (mContext == null)   {   mContext = context; }// 数据库对象    DaoSession daoSession = DbHelper.getDaoSession(mContext);instance.personDao = daoSession.getPersonDao();}   return instance;    }/** 添加数据 */public void addInfo(Person item){personDao.insert(item);}/** 删除  一条数据,根据id删除 */public void deleteInfo(int Id){personDao.deleteByKey((long) Id);//删除一条note:非常简单明,在onListItemClick方法中可以看到        }/** 删除    可修改变成删除一组数据 */public void deleteInfoList(int id){QueryBuilder<Person> qb = personDao.queryBuilder();//删除的条件DeleteQuery<Person> bd = qb.where(Properties.Id.eq(id)).buildDelete();bd.executeDeleteWithoutDetachingEntities();}/** 删除表内所有数据*/public void clearInfo() {personDao.deleteAll();  }/** 修改*///item中有id,根据id进行修改。下面两中方法都可以用。public void updateInfo(Person item){personDao.insertOrReplace(item);//personDao.update(note2);}/** 多条数据查询 */public List<Person> getInfoList1(){QueryBuilder<Person> qb = personDao.queryBuilder();return qb.list();}/** 查询   获取整个表的数据集合,一句代码就搞定!*/public List<Person> getInfoList2(){return personDao.loadAll();}/** 查询某个Id是否存在 */public boolean isHave(int Id){QueryBuilder<Person> qb = personDao.queryBuilder();qb.where(Properties.Id.eq(Id));qb.buildCount().count();return qb.buildCount().count() > 0 ? true : false;// 查找收藏表}/**单条查询  通过id查找其类型字段值 */public String getTypeValue(int id)  {   QueryBuilder<Person> qb = personDao.queryBuilder(); qb.where(Properties.Id.eq(id));      //通过id字段查询,具体使用可修改别的字段if (qb.list().size() > 0)   {   return qb.list().get(0).getName(); //根据id获取它的name字段的值。具体用,具体修改}   else    {   return "0"; }   }/** 多条件重查询 */public List<Person> getTJList(int id){QueryBuilder<Person> qb = personDao.queryBuilder();//这是查询条件qb.where(qb.and(Properties.Id.eq(id), Properties.Age.eq(20)));    //eq等于。gt大于。ge大于等于qb.orderAsc(Properties.Id);// 排序依据                                                           ASC表示升序排序,DESC表示降序排序return qb.list();}//名字叫“乔”和出生年份(大于1970或(出生年份是1970年,出生月等于或大于10(10月)。
//  QueryBuilder qb = userDao.queryBuilder();
//  qb.where(Properties.FirstName.eq("Joe"),
//  qb.or(Properties.YearOfBirth.gt(1970),
//  qb.and(Properties.YearOfBirth.eq(1970), Properties.MonthOfBirth.ge(10))));
//  List youngJoes = qb.list();//  比如:查找所有以“Joe”为first name 的用户,并以last name排序://  List joes = userDao.queryBuilder()
//  .where(Properties.FirstName.eq("Joe"))
//  .orderAsc(Properties.LastName)
//  .list();}

GreenDao封装使用相关推荐

  1. 使用反射+注解封装一个基于Sqlite极简的android数据库框架

    数据库 背景 GreenDao 封装 创建数据库 对象映射表 数据库操作 扩展 总结 背景 目前市面上已经有比较好用的数据库框架,比如GreenDao和OrmLite,而且功能也很齐全,那为什么还要多 ...

  2. greenDao的使用与封装

    1. greenDao的使用步骤: 第一步:  在项目的.gradle文件里面添加 buildscript {repositories {mavenCentral()jcenter()}depende ...

  3. java basedao 封装,GreenDao的实用封装

    前面简单介绍了下GreenDao的使用,从前面的介绍看来是不是觉得有点 so easy.对就是这么简单.曾经有位大神说作为一位合格的程序员就要在学习别人的东西时,有点自己的想法.为了表示我是位比较合格 ...

  4. GreenDao自带异步操作类简析

    AsyncSession: GreenDao提供一个异步操作的统一接口类AsyncSession,它提供了你所需要的所有异步操作方法. 你可以通过调用DaoSession#startAsyncSess ...

  5. Android ORM 框架之 greenDAO 使用心得

    前言 我相信,在平时的开发过程中,大家一定会或多或少地接触到 SQLite.然而在使用它时,我们往往需要做许多额外的工作,像编写 SQL 语句与解析查询结果等.所以,适用于 Android 的ORM  ...

  6. Android持久化存储(4)greenDAO的使用

    1.背景 在上篇博客介绍SQLite的使用我们能感觉到,虽然Android已经简化了一些SQLite的操作,提供了较为方便的API,但使用中还是需要开发者写一些SQL语言,简单而不简约,有时候只想存储 ...

  7. GreenDao 工具类 --- 使用 Json 快速生成 Bean、表及其结构,炒鸡快!

    作者:林冠宏 / 指尖下的幽灵 掘金:https://juejin.im/user/587f0dfe128fe100570ce2d8 博客:http://www.cnblogs.com/linguan ...

  8. GreenDao 配置和使用

    作为github上star超8k fork超2k的强大框架,很有必要来用一用嘛~~~~~~ 首先奉上GreenDao的github地址https://github.com/greenrobot/gre ...

  9. Android GreenDao

    离线开发 开发工具: androidStudio ,SQLite3 使用开源库:GreenDao 3.2.2 离线已实现: 1.  增.删.改.查 2. 新增表,字段,更改字段,升级数据迁移 3. 从 ...

最新文章

  1. win2012服务器硬盘分区,Windows Server 2008/2012更改磁盘分区大小教程
  2. margin的塌陷现象
  3. python中的range_python中range()与xrange()用法分析
  4. vue实现对数据的增删改查(CURD)
  5. 厦门理工学院c语言实验循环,厦门理工学院c语言实验4_循环答案).doc
  6. 【Java 编程】文件操作,文件内容的读写—数据流
  7. java 最佳主键_最佳Java 8书籍
  8. [Head First设计模式]云南米线馆中的设计模式——模版方法模式
  9. oracle库怎么样查询gp数据库,GP数据库分布键查询
  10. mysql服务什么意思_mysql数据库服务是什么意思
  11. Word文档标题自动增加序号
  12. 建站系列:有云服务器和域名怎么建设网站?
  13. 为知笔记 | 3 分钟创建格式美美的笔记
  14. 学python有必要用固态硬盘吗-pythonssd
  15. 推荐一款广告过滤软件
  16. 微机原理—可编程计数器/定时器8253概念详解
  17. 精品课 - Python 基础
  18. 远程控制软件哪个好?
  19. KV260开箱初体验
  20. IT 学习资源大汇总

热门文章

  1. 算法精品讲解(2)——DP问题入门(适合零基础者,一看就会)
  2. fflush函数使用
  3. 利用WebBrowser彻底解决Web打印问题
  4. 优盘复制进来为空_为什么复制后文件夹u磁盘为空
  5. Struts——开源MVC框架
  6. 用户行为分析(如何用数据驱动增长)-读书笔记1
  7. 微信支付结算费率怎么降低至0.2~0.35操作方法
  8. day1 704.二分查找 27.移除元素
  9. 使用TreeMap实现ASCII排序
  10. 解决找不到gpedit.msc文件方法