GreenDao的使用
来源:互联网 发布:淘宝新手开店怎么装修 编辑:程序博客网 时间:2024/05/22 13:01
1.创建一个Java工程
1-1.引包:
加入jar包:
右键:add to build path:
编译后:
(eclipse需要两个包,Android studio只需要后一个包)
1-2.创建Java文件
1-2-1.建包(最好和你的工程包名相同)
1-2-2.在与src同级的目录下创建一个名为src-gen的包
1-2-3.创建java文件
package com.example.greendao;import java.io.IOException;import de.greenrobot.daogenerator.DaoGenerator;import de.greenrobot.daogenerator.Entity;import de.greenrobot.daogenerator.Schema;public class GreenDao { public static void main(String args[]) { String path = "src-gen"; Schema schema = new Schema(1, "com.example.greendao");//生成的类的路径 addEntity(schema);//调用静态方法 try { new DaoGenerator().generateAll(schema, path); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void addEntity(Schema schema) { Entity entity = schema.addEntity("Student");//Student:生成的实体类类名 entity.addLongProperty("_id").primaryKey().autoincrement(); //entity.addIdProperty().primaryKey().autoincrement();//主键的两种不同的写法 entity.addStringProperty("name"); entity.addStringProperty("sex"); entity.addStringProperty("age"); entity.addStringProperty("number"); entity.addLongProperty("grade"); //一个表一个实体类,如果还要创建其他的表,可以在这里接着创建Entity对象 }}
1-2-4.编译后的结果
注:如果是eclipse,可以直接移到Android工程与src同级目录下.
1.
2.
3.
如果是Android studio,可以直接将生成的文件拷贝到studio里.
2.创建Android工程
2-1.引包
compile 'de.greenrobot:greendao:2.0.0'
2-2.在BaseApplication中创建DaoMaster单例模式对象并获取DaoSession对象
package com.example.greendaodemo;import android.app.Application;import android.content.Context;public class BaseApplication extends Application { private static DaoMaster daoMaster; private static DaoSession daoSession; private static Context mContext; @Override public void onCreate() { super.onCreate(); if (mContext == null) { mContext = getApplicationContext(); } } public static Context getmContext() { return mContext; }// 获取DaoMaster单例模式对象 public static DaoMaster getDaoMaster() { if (daoMaster == null) { DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(mContext, Constants.DATABASE_NAME, null); daoMaster = new DaoMaster(helper.getWritableDatabase()); } return daoMaster; } //获取DaoSession对象 public static DaoSession getDaoSession() { if (daoSession == null) { if (daoMaster == null) { daoMaster = getDaoMaster(); } daoSession = daoMaster.newSession(); } return daoSession; }}
2-3.创建dbHelper类
package com.example.greendaodemo;import java.util.List;import de.greenrobot.dao.query.QueryBuilder;public class StudentDBHelper { //创建单例模式 private static StudentDBHelper instance; //从BaseApplication获得的DaoSession单例对象 private static DaoSession daoSession; //与单例模式搭配使用,保证只有一个实例对象 private StudentDBHelper() { } public static StudentDBHelper getInstance() { if (instance == null) { instance = new StudentDBHelper(); daoSession = getDaoSession(); } return instance; } public static DaoSession getDaoSession() { if (daoSession == null) { daoSession = BaseApplication.getDaoSession(); } return daoSession; } //返回数据库表的条数,返回值是long型 public long getCount() { return daoSession.getStudentDao().count(); } //插入 //1.插入集合,无返回值 public void insertList(List<Student> entities) { daoSession.getStudentDao().insertOrReplaceInTx(entities); } //2.插入对象,返回值是long型 public long insert(Student entity) { return daoSession.getStudentDao().insert(entity); } //删除方法,无返回值 public void delete(Student entity) { daoSession.getStudentDao().delete(entity); } //根据某一字段查询,返回值为一个集合 public List<Student> queryType(String username) { QueryBuilder<Student> db = daoSession.getStudentDao().queryBuilder(); //限制每次取多少数据 db.limit(20); //定义查询条件 and关系 db.where(StudentDao.Properties.Name.eq(username)); return db.list(); } public List<Student> queryByName(String name, String sex) { QueryBuilder<Student> db = daoSession.getStudentDao().queryBuilder(); db.where(StudentDao.Properties.Name.eq(name), StudentDao.Properties.Age.eq(sex)); return db.list(); } public List<Student> query(String name) { return daoSession.getStudentDao().queryBuilder().where(StudentDao.Properties.Name.eq(name)).list(); }}
3.用法
0 0
- 关于greenDao的使用
- greenDao的使用教程
- GreenDAO的简单使用
- GreenDao的使用
- GreenDao的使用
- GreenDao的使用
- GreenDao的使用
- greendao的使用
- greenDAO的使用
- GreenDao的使用心得
- Greendao的使用
- GreenDao的使用详解
- GreenDao数据库的使用
- GreenDao的使用
- greenDAO的使用
- GreenDao的使用
- GreenDao的使用
- greenDAO的使用
- android SQLite实现本地登录注册功能,SQLite简单应用(android studio)
- 日经春秋 20151110
- DM385 IPNC GPMC+EDMA驱动学习笔记
- startService的生命周期
- 关于爬虫的一些东西
- GreenDao的使用
- Block与数组排序,局部变量,字面量
- 嵌入式系统安全复习(二)
- 解答:第11周“知原理”检验题目
- 基于DCT的JPEG图像压缩编码过程及举例
- Torch学习——开始
- 从头认识java-8.1 内部类
- 【拔苗计划】linux学习笔记——信号处理机制及kill、du、df命令
- 数据库介绍与分类