}
@Keep
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student(Long id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@Keep
public Long getId() {
return id;
}
@Keep
public void setId(Long id) {
this.id = id;
}
@Keep
public String getName() {
return name;
}
@Keep
public void setName(String name) {
this.name = name;
}
@Keep
public int getAge() {
return age;
}
@Keep
public void setAge(int age) {
this.age = age;
}
@Keep
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Student)) return false;
Student student = (Student) o;
return name.equals(student.name);
}
@Keep
@Override
public int hashCode() {
return (int) (id ^ (id >>> 32));
}
@Keep
@Override
public String toString() {
return “Student{” +
“id=” + id +
“, name=’” + name + ‘’’ +
“, age=” + age +
‘}’;
}
}
修复一下工程,自动生成greendao包下的类(就是点击一下小锤子),你会自动生成上图中greendao包中的类。
开始使用,创建管理类
package com.yintong.secure.simple.encryptiongreendao.dao;
import android.content.Context;
import com.yintong.secure.simple.encryptiongreendao.greendao.DaoMaster;
import com.yintong.secure.simple.encryptiongreendao.greendao.DaoSession;
import org.greenrobot.greendao.database.Database;
public class DbManager {
// 是否加密
public static final boolean ENCRYPTED = true;
private static DbManager mDbManager;
private static DaoMaster.DevOpenHelper mDevOpenHelper;
private static DaoMaster mDaoMaster;
private static DaoSession mDaoSession;
private DbManager(Context context, String dbName, String passwprd) {
// 初始化数据库信息
mDevOpenHelper = new DaoMaster.DevOpenHelper(context, dbName);
getDaoMaster(context, dbName, passwprd);
getDaoSession(context, dbName, passwprd);
}
public static DbManager getInstance(Context context, String dbName, String passwprd) {
if (null == mDbManager) {
synchronized (DbManager.class) {
if (null == mDbManager) {
mDbManager = new DbManager(context, dbName, passwprd);
}
}
}
return mDbManager;
}
/**
获取可读数据库
@param context
@return
*/
public static Database getReadableDatabase(Context context, String dbName, String passwprd) {
if (null == mDevOpenHelper) {
getInstance(context, dbName, passwprd);
}
if (ENCRYPTED) {//加密
return mDevOpenHelper.getEncryptedReadableDb(passwprd);
} else {
return mDevOpenHelper.getReadableDb();
}
}
/**
获取可写数据库
@param context
@param dbName
@param passwprd
@return
*/
public static Database getWritableDatabase(Context context, String dbName, String passwprd) {
if (null == mDevOpenHelper) {
getInstance(context, dbName, passwprd);
}
if (ENCRYPTED) {//加密
return mDevOpenHelper.getEncryptedWritableDb(passwprd);
} else {
return mDevOpenHelper.getWritableDb();
}
}
/**
获取DaoMaster
@param context
@param dbName
@param passwprd
@return
*/
public static DaoMaster getDaoMaster(Context context, String dbName, String passwprd) {
if (null == mDaoMaster) {
synchronized (DbManager.class) {
if (null == mDaoMaster) {
mDaoMaster = new DaoMaster(getWritableDatabase(context, dbName, passwprd));
}
}
}
return mDaoMaster;
}
/**
获取DaoSession
@param context
@param dbName
@param passwprd
@return
*/
public static DaoSession getDaoSession(Context context, String dbName, String passwprd) {
if (null == mDaoSession) {
synchronized (DbManager.class) {
// mDaoSession = getDaoMaster(context,dbName,passwprd).newSession();
mDaoSession = getDaoMaster(context, dbName, passwprd).newDevSession(context, dbName);
}
}
return mDaoSession;
}
}
package com.yintong.secure.simple.encryptiongreendao.dao;
import android.content.Context;
import com.yintong.secure.simple.encryptiongreendao.bean.Student;
import com.yintong.secure.simple.encryptiongreendao.greendao.StudentDao;
import org.greenrobot.greendao.query.QueryBuilder;
import java.util.List;
public class StudentDaoOpe {
private static final String DB_NAME = “test.db”;
private static final String PASSWPRD = “password”;
/**
添加数据至数据库
@param context
@param stu
*/
public static void insertData(Context context, Student stu) {
// DbManager.getDaoSession(context, DB_NAME,PASSWPRD).getStudentDao().insert(stu);
DbManager.getDaoSession(context, DB_NAME,PASSWPRD).getStudentDao().insertOrReplace(stu);
}
/**
将数据实体通过事务添加至数据库
@param context
@param list
*/
public static void insertData(Context context, List list) {
if (null == list || list.size() <= 0) {
return;
}
// DbManager.getDaoSession(context, DB_NAME, PASSWPRD).getStudentDao().insertInTx(list);
DbManager.getDaoSession(context, DB_NAME, PASSWPRD).getStudentDao().insertOrReplaceInTx(list);
}
/**
添加数据至数据库,如果存在,将原来的数据覆盖
内部代码判断了如果存在就update(entity);不存在就insert(entity);
@param context
@param student
*/
public static void saveData(Context context, Student student) {
DbManager.getDaoSession(context, DB_NAME, PASSWPRD).getStudentDao().save(student);
}
/**
删除数据至数据库
@param context
@param student 删除具体内容
*/
public static void deleteData(Context context, Student student) {
DbManager.getDaoSession(context, DB_NAME, PASSWPRD).getStudentDao().delete(student);
}
/**
根据id删除数据至数据库
@param context
@param id 删除具体内容
*/
public static void deleteByKeyData(Context context, long id) {
DbManager.getDaoSession(context, DB_NAME, PASSWPRD).getStudentDao().deleteByKey(id);
}
/**
删除全部数据
@param context
*/
public static void deleteAllData(Context context) {
DbManager.getDaoSession(context, DB_NAME, PASSWPRD).getStudentDao().deleteAll();
}
/**
更新数据库
@param context
@param student
*/
public static void updateData(Context context, Student student) {
DbManager.getDaoSession(context, DB_NAME, PASSWPRD).getStudentDao().update(student);
}
/**
查询所有数据
@param context
@return
*/
public static List queryAll(Context context) {
QueryBuilder builder = DbManager.getDaoSession(context, DB_NAME, PASSWPRD).getStudentDao().queryBuilder();
return builder.build().list();
}
/**
根据id,其他的字段类似
@param context
@param id
@return
*/
public static List queryForId(Context context, long id) {
QueryBuilder builder = DbManager.getDaoSession(context, DB_NAME, PASSWPRD).getStudentDao().queryBuilder();
/**
返回当前id的数据集合,当然where(这里面可以有多组,做为条件);
这里build.list();与where(StudentDao.Properties.Id.eq(id)).list()结果是一样的;
在QueryBuilder类中list()方法return build().list();
*/
// Query build = builder.where(StudentDao.Properties.Id.eq(id)).build();
// List list = build.list();
return builder.where(StudentDao.Properties.Id.eq(id)).list();
}
}
xmlns:tools=“http://schemas.android.com/tools” android:id="@+id/activity_main" android:layout_width=“match_parent” android:layout_height=“match_parent” tools:context=".MainActivity"> android:layout_width=“match_parent” android:layout_height=“wrap_content” android:orientation=“horizontal”>