Andorid之GreenDao
来源:互联网 发布:java中main函数 编辑:程序博客网 时间:2024/04/26 01:45
第一步是配置:
在gradle下配置
apply plugin: 'com.android.application'android { compileSdkVersion 23 buildToolsVersion "23.0.3" defaultConfig { applicationId "com.example.myapplication" minSdkVersion 10 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { /**********配置*********/ sourceSets{ main{ java.srcDirs =['src/main/java','src/main/java-gen'] } /*******************/ } release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }}dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.4.0' /********配置***********/ compile 'de.greenrobot:greendao:2.1.0' compile 'de.greenrobot:greendao-generator:2.1.0' /*******************/}
第二步创建一个在main目录下java-gen
第三步创建一个java项目
如下配置下build
package com.example;import de.greenrobot.daogenerator.DaoGenerator;import de.greenrobot.daogenerator.Entity;import de.greenrobot.daogenerator.Schema;public class DaoMaker { public static void main(String[] args) { int version = 1; String defaultPackage = "com.student.entity"; String pathDao="D:\\AndroidStudioProjects\\ApplicationTrunk\\mediamyself\\src\\main\\java-gen"; //生成数据库的实体类XXentity 对应的是数据库的表 Schema schema = new Schema(version, defaultPackage); addStudent(schema); schema.setDefaultJavaPackageDao("com.student.dao"); try { new DaoGenerator().generateAll(schema,pathDao); } catch (Exception e){ e.printStackTrace(); } } //创建数据库的表 private static void addStudent(Schema schema) { Entity entity = schema.addEntity("Student"); entity.addIdProperty();//主键是int 类型 entity.addStringProperty("name");//对应的数据库的列 entity.addStringProperty("address");//对应数据库的列 entity.addIntProperty("age");//对应数据库的列 }}
运行下,右键,运行之后就会出现在java-gen文件夹下多出一些文件
文件如图自动生成
在项目中操作数据库~
package com.example.myapplication.dbmanager;import android.content.Context;import com.student.dao.DaoMaster;import com.student.dao.DaoSession;import de.greenrobot.dao.query.QueryBuilder;/** * 1.创建数据库 * 2.创建数据库的表 * 3.包含对数据库的CRUD * 4.对数据库的升级 * Created by Administrator on 2016/8/16. */public class DaoManager { private static final String TAG = DaoManager.class.getSimpleName(); private static final String DB_NAME = "mydb.sqlite";//数据库名称 private volatile static DaoManager manager;//多线程访问 private static DaoMaster.DevOpenHelper helper; private static DaoMaster daoMaster; private DaoSession daoSession; private Context context; /** * 使用单列模式获得操作数据库的对象 */ public static DaoManager getInstance(){ DaoManager instance = null; if(manager == null){ synchronized (DaoManager.class) { if (instance == null) { instance = new DaoManager(); manager = instance; } } } return instance; } public void init(Context context) { this.context = context; } /** * 判断是否存在数据库,如果没有数据库则创建数据库 * */ public DaoMaster getDaoMaster(){ if (daoMaster == null) { DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, DB_NAME, null); daoMaster = new DaoMaster(helper.getWritableDatabase()); } return daoMaster; } /** * 完成对数据库的添加,删除,修改,查询的操作,仅仅是一个接口 * */ public DaoSession getDaoSession(){ if (daoSession == null) { if (daoMaster==null){ daoMaster = getDaoMaster(); } daoSession = daoMaster.newSession(); } return daoSession; } /** * 打开输出日志的操作,默认是关闭的 */ public void setDebug(){ QueryBuilder.LOG_SQL = true; QueryBuilder.LOG_VALUES = true; } /** * 关闭所有的操作,数据库开启的时候,使用完必须要关闭 * */ public void closeConnection(){ closeHelper(); closeDaoSession(); } private void closeDaoSession() { if (daoSession != null) { daoSession.clear(); daoSession = null; } } private void closeHelper() { if(helper!=null){ helper.close(); helper = null; } }}
package com.example.myapplication.dbmanager;import android.content.Context;import android.util.Log;import com.student.dao.StudentDao;import com.student.entity.Student;import java.util.List;import de.greenrobot.dao.query.QueryBuilder;/** * 完成对某一张表的具体操作,ORM操作的对象,Student * Created by Administrator on 2016/8/16. */public class CommonUtils { private static final String TAG = CommonUtils.class.getSimpleName(); private DaoManager manager; public CommonUtils(Context context){ manager = DaoManager.getInstance(); manager.init(context); } /** * 完成对数据库中student表的插入 操作 */ public boolean insetStudent(Student student){ boolean flag = false; flag = manager.getDaoSession().insert(student) != -1 ? true : false; Log.i(TAG, "insetStudent: "+flag); return flag; } /** * 插入多条记录,需要开辟新的线程 * */ public boolean insetMultStudent(final List<Student> students){ boolean flag = false; try { manager.getDaoSession().runInTx(new Runnable() { @Override public void run() { for (Student s : students) { manager.getDaoSession().insertOrReplace(s); } } }); flag = true; }catch (Exception e){ } return flag; } /** * 完成对student的某一条记录的修改 * */ public boolean updateStudent(Student student){ boolean flag = false; try{ manager.getDaoSession().update(student); }catch (Exception e){ e.printStackTrace(); } return flag; } /** * 删除数据 */ public boolean deleteStudent(Student s){ boolean flag = false; try{//按照指定的id 进行删除 delete from student where id = ? manager.getDaoSession().delete(s); //managere.getDaosession().deleteAll();//删除所有记录 }catch (Exception e){ e.printStackTrace(); } return flag; } /** * 返回多行记录 * */ public List<Student> listAll(){ return manager.getDaoSession().loadAll(Student.class); } /** * 按照主键返回单行记录 * */ public Student listOneStudent(long key){ return manager.getDaoSession().load(Student.class, key); } public void query1(){ //使用native sql 进行查询操作 List<Student> list = manager.getDaoSession().queryRaw(Student.class, "where like ? and _id > ?", new String[]{"%李%", "1002"}); Log.i(TAG, "query1: "+ list); } /** * select * from student where name like ? or name =? or * < <= != in between and * select *from student where age > 23 and address like "江西" * */ public void query2(){ //查询构建器 QueryBuilder<Student> builder = manager.getDaoSession().queryBuilder(Student.class); List<Student> list = builder.where(StudentDao.Properties.Age.ge(23)).where(StudentDao.Properties.Address.like("江西")).list(); Log.i(TAG, "query2: " + list); } public void query3(){ //逻辑与 和 逻辑或 是双目运算符 QueryBuilder<Student> builder = manager.getDaoSession().queryBuilder(Student.class); //select * from student where (address = '北京' or age > 50 and name like '%张%') builder.whereOr(StudentDao.Properties.Address.eq("北京"), StudentDao.Properties.Age.eq(50)); builder.whereOr(StudentDao.Properties.Id.ge(2), StudentDao.Properties.Age.ge(10)).limit(3);//取前三条数据 List<Student> list = builder.list(); Log.i(TAG, "query3: "+ list); }}
1 0
- Andorid之GreenDao
- Andorid开源框架greenDAO
- andorid 开源数据库 greenDao
- GreenDAO之「01.初始GreenDAO」
- android 数据库之greendao
- Android ORM之greenDao
- GreenDao之数据库升级
- ORM 框架之 GreenDao
- 数据库框架之GreenDao
- andorid之OpenGL基本原理
- andorid 之 activity
- Andorid之SlideMenuView
- Andorid之ActivityManager
- Andorid之ListView优化
- Andorid架构之BaseActivity
- android_orm框架之greenDAO(二)
- Android ORM之GreenDao学习
- Android之greenDao的使用
- 为什么视频用udp不用tcp
- C# 线程间操作无效
- 删除或者覆盖掉git远端库里面的某部分文件
- LeetCode--No.223--Rectangle Area
- 一个模板方法的例子
- Andorid之GreenDao
- 带GPG签名的Git tag
- Android Lib层打印log之------bionic库打印log
- 求解线性方程最小二乘解
- mapreduce yarn内存参数
- 面试题--HashMap和HashTable的区别
- 如何选择以后的方向
- 像素设定
- 存储过程优缺点