greenDao数据库框架
1.greendao的引入
在project 目录下的build.gradle中添加插件包
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
在App模块下的gradle文件中添加如下依赖
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'
android {
...
greendao{
schemaVersion 4 //当前所建立的数据库的版本号
targetGenDir "src/main/java" 类所生成的目录
daoPackage "session.gddemo.db" 类所在包名
}
}
dependencies {
compile 'org.greenrobot:greendao:3.2.0'
}
3.建立实体所对应的表
@Entity//(nameInDb = "students" /** 设置表名,*/)
public class Student {
private String name;
@Id
private Long ID;
private int age;
}
@Entity(nameInDb ="要设置的表名")
@Property(nameInDb = "要设置的列名") //设置列名
4.在数据库报下面建立一个数据库管理类如下
package session.gddemo.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import org.greenrobot.greendao.database.Database;
/**
* Created by wukai on 2016/12/19.
*/
public class DbCore {
private static final String db_name = "demo.db";
static class DemoOpenHelper extends DaoMaster.OpenHelper{
public DemoOpenHelper(Context context, String name) {
super(context, name);
}
public DemoOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
super(context, name, factory);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
super.onUpgrade(db, oldVersion, newVersion);
System.out.println("数据库正在升级...");
DaoMaster.createAllTables(wrap(db),true);
//数据库升级新增一列
db.execSQL("alter table animal add column GENDER text");
}
}
public static DaoSession getDaoSession(Context context){
DemoOpenHelper helper = new DemoOpenHelper(context,db_name);
return new DaoMaster(helper.getWritableDb()).newSession();
}
5.使用数据库进行增删改查
1.获取dao session
2.根据daosession获取对应表的操作
daoSession = DbCore.getDaoSession(this);
6.用dao进行数据的新增操作
public void insertData(View view) {
Student student = new Student("周星驰",new Long(1),18);
//调用save方法新增一条记录
daoSession.getStudentDao().save(student); //如果要更改的数据已经存在,则更新此条数据
//Fruit fruit = new Fruit("apple",5.0f,null);
student.setAge(28);
daoSession.getStudentDao().insertOrReplace(student);
// daoSession.getFruitDao().save(fruit);
}
//批量插入
public void insertDataBatch(View view) {
List<Student> students = new ArrayList<>();
Random random = new Random();
for (int i = 0; i < 100; i++) {
Student student = new Student("朱元璋"+i,null,random.nextInt(40));
students.add(student);
}
daoSession.getStudentDao().insertInTx(students);
}
//批量插入或者更新
public void insertOrReplace(View view) {
List<Student> students = new ArrayList<>();
Random random = new Random();
for (int i = 0; i < 100; i++) {
Student student = new Student("王宝宝"+i,new Long(i),random.nextInt(40));
students.add(student);
}
daoSession.getStudentDao().insertOrReplaceInTx(students);
}
7.数据库的更新和删除操作
//单条记录更新
public void updateData(View view) {
Student student = new Student("徐达",new Long(0),18);
mDaoSession.getStudentDao().update(student);
}
//批量更新
public void updateDataTx(View view) {
List<Student> students = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Student student = new Student("蓝玉",new Long(i+1),10086);
students.add(student);
}
Student student1 = new Student("王祖蓝",new Long(10+1),10086);
Student student2 = new Student("张曼玉",new Long(10+2),10086);
Student student3 = new Student("赵丽颖",new Long(10+3),10086);
mDaoSession.getStudentDao().updateInTx(students);
mDaoSession.getStudentDao().updateInTx(student1,student2,student3);
}
//根据条件更新
public void updateByCondition(View view){
WhereCondition condition = StudentDao.Properties.ID.between(14,20);
//根据条件删除
//condition = StudentDao.Properties.Name.eq("张曼玉");
//把名称中包含朱元璋的人名找出来更新为成吉思汗
condition = StudentDao.Properties.Name.like("%朱元璋%");
List<Student> students =mDaoSession.getStudentDao().queryBuilder().
where(condition).build().list();
for (Student stu:students){
stu.setName("成吉思汗");
}
//批量更新
mDaoSession.getStudentDao().updateInTx(students);
}
//删除数据
public void deleteData(View view) {
//删除所有
// daoSession.getStudentDao().deleteAll();
//
// Student student = new Student("student",new Long(1),80);
//
// daoSession.getStudentDao().delete(student);//student对象中的主键来删除记录
//
// //批量删除
// daoSession.getStudentDao().deleteInTx();
//mDaoSession.getStudentDao().deleteByKey(new Long(20));
//mDaoSession.getStudentDao().deleteByKeyInTx(new Long(11),new Long(19));
List<Long> longs = new ArrayList<>();
longs.add(new Long(15));
longs.add(new Long(16));
mDaoSession.getStudentDao().deleteByKeyInTx(longs);
//根据条件来删除
WhereCondition w = StudentDao.Properties.Name.like("成吉思汗");
mDaoSession.getStudentDao().queryBuilder().where(w).buildDelete().executeDeleteWithoutDetachingEntities();
}
8.数据库的查询操作
//自定义where查询
//查询的第一种写法
String where = "where _id<? and age>?";
ArrayList<Object> values = new ArrayList<>();
values.add("10");
values.add("20");
List<Student> results = mDao.getStudentDao().queryRawCreateListArgs(where,values).list();
// //查询的第二种写法
// results = mDao.getStudentDao().queryRawCreate(where,"10","20").list();
//
// //查询的第三种写法
// results = mDao.getStudentDao().queryRaw(where,"10","20");
//
//无条件的queryBuidler查询
// results = mDao.getStudentDao().queryBuilder().list();
//
// // 有条件的QueryBuilder查询
// WhereCondition wc1 = StudentDao.Properties.ID.le(20);
// WhereCondition wc2 = StudentDao.Properties.Age.gt(20);
//
// results = mDao.getStudentDao().queryBuilder().where(wc1,wc2).list();
//
// //查询表中所有的数据
// results = mDao.getStudentDao().loadAll();
//
// results = mDao.getStudentDao().queryBuilder().orderDesc(StudentDao.Properties.ID).list();
//
// for (Student student:results){
//
// System.out.println(student.toString());
// }
//
// Student student = mDao.getStudentDao().load(new Long(18));
//
// System.out.println(student.toString());
//
// //查询表中有多少条数据
// long count = mDao.getStudentDao().queryBuilder().where(
// StudentDao.Properties.ID.gt(90)
// ).buildCount().count();
//
// System.out.println("long count="+count);
//
// count = mDao.getStudentDao().queryBuilder().count();
// System.out.println("long count="+count);
//
// //判断在某种条件查询出的结果只能有一条
// Student student = mDao.getStudentDao().queryBuilder().where(
// StudentDao.Properties.Name.like("王宝宝21")
//
// ).unique();
//
// System.out.println(student.toString());
WhereCondition w = StudentDao.Properties.ID.in(10,15,20,30);
mDao.getStudentDao().queryBuilder().where(w).list();
/**
* select * from student offset 0 limit 10
* select * from student offset 10 limit 10
* select * from student offset 20 limit 10
*
*/
List<Student> students = mDao.getStudentDao().queryBuilder().offset(page*10).limit(count).list();
for (Student student1 :students){
System.out.println(student1);
}
page++;
}
private int page = 0;
private int count =10;
9.显示调试信息
static {
QueryBuilder.LOG_SQL= true;
QueryBuilder.LOG_VALUES = true;
}