GreenDao3
首先GreenDao3是一个中间工具类,它是一个将数据库条目转换成一个操作对象,然后调用这个操作对象执行增删查改,避免写入SQL语句,然后所有的增删查改都是以对象的方式进行;
GreenDao3和GreenDao2的创建方式不同,
GreenDao2需要创建一个Java工程,然后在工程中创建一个Java类,该类用于生成项目所需的Entity、Dao等文件;
GreenDao3不需要创建Java工程,直接创建对象(bean),然后使用注解,再构建一下工程,即可生成Entity,Dao等文件;
使用:
首先,在工程的build.gradle 文件中配置 greendao的gradle构建插件,注意插件的版本和原本gradle的版本如果差别他打可能导致无法build;
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'org.greenrobot:greendao-gradle-plugin:3.1.1'
}
然后有了构建插件后,就要开始使用这个插件了,在Module的build.gradle文件中做以下修改
在文件的最上方添加
apply plugin:'org.greenrobot.greendao'
表示使用这个插件。
然后在该build.gradle文件的根级别写greendao的配置
greendao{
schemaVersion 1 //版本号,升级时可配置
daoPackage'com.jonson.mygreendao3test.dao' //自动生成的文件存放位置
targetGenDir'src/main/java'//自动生成的文件存放的根目录
}
然后在dependencies下 引用 compile 'org.greenrobot:greendao:3.1.1'
即可;
开始使用:
创建一个Student的Bean
类使用@Entity注解,如果哪个变量要作为ID的话,则使用@ID注解;
@Entity
public class Student{
@Id
public String id;
public String name;
public String age;
public String number;
public String score;
}
这样,Student类创建完成了,这个类是将来作为数据库表的字段的。也就是将来创建的数据库有id,name,age,number,score这5个字段;
接着,编译一下项目,就会在项目中生成一个目录:
目录结构为:
─jonson.mygreendao3test.dao
┝┈DaoMaster
┝┈DaoSession
┝┈StudentDao
然后创建一个文件夹db
然后在db
先创建以下文件
1.BaseDbHelper 该文件作为StudentOpenHelper的基类,使用泛型封装了数据库的增删查改方法,后面的实体对象OpenHelper类只需要继承它即可。
public class BaseDbHelper<T , K> {
private AbstractDao<T , K> mDao;
public BaseDbHelper(AbstractDao<T, K> mDao) {
this.mDao = mDao;
}
/**增*/
public void save(T item){
mDao.insert(item);
}
...
public void saveOrUpdate(T item){
mDao.insertOrReplace(item);
}
...
/**删*/
public void deleteByKey(K key){
mDao.deleteByKey(key);
}
...
/**查*/
public T query(K key){
return mDao.load(key);
}
...
/**改*/
public void update(T item){
mDao.update(item);
}
...
/**其他*/
public long count(){
return mDao.count();
}
public void refresh(T item){
mDao.refresh(item);
}
public void detach(T item){
mDao.detach(item);
}
public QueryBuilder<T> queryBuilder(){
return mDao.queryBuilder();
}
}
2.DbCore 该文件作为使用数据库时的核心文件,用于初始化数据库,获取数据库的DaoMaster,DaoSession
public class DbCore {
public static String DEFAULT_DB_NAME = "student_db";
public static Context mContext;
public static String dbName;
private static DaoMaster.OpenHelper openHelper;
private static DaoMaster daoMaster;
private static DaoSession daoSession;
public void init(Context context){
Log.e("aa" , "初始化数据库");
init(context , DEFAULT_DB_NAME);
}
public void init(Context context , String dbName){
this.mContext = context.getApplicationContext();
this.dbName = dbName;
}
public static DaoMaster getDaoMaster(){
if(openHelper == null){
openHelper = new SutdentOpenHelper(mContext, dbName);
daoMaster = new DaoMaster(openHelper.getWritableDatabase());
}
return daoMaster;
}
public static DaoSession getDaoSession(){
if(daoSession == null){
if(daoMaster == null){
daoMaster = getDaoMaster();
}
daoSession = daoMaster.newSession();
}
return daoSession;
}
}
3.DbUtil 该文件用于获取各个实体对象的数据库操作类
public class DbUtil {
private static StudentDbHelper mStudentDbHelper;
public static StudentDbHelper getStudentDbHelper(){
if(mStudentDbHelper == null){
mStudentDbHelper = new StudentDbHelper(DbCore.getDaoSession().getStudentDao());
}
return mStudentDbHelper;
}
}
4.StudentDbHelper 实体对象的数据库操作类实现
public class StudentDbHelper extends BaseDbHelper<Student , Long> {
public StudentDbHelper(AbstractDao<Student, Long> mDao) {
super(mDao);
}
}
5.StudentOpenHelper 实体对象的数据库打开帮助类
public class SutdentOpenHelper extends DaoMaster.OpenHelper{
public SutdentOpenHelper(Context context, String name) {
super(context, name);
}
public SutdentOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
super(context, name, factory);
}
@Override
public void onCreate(Database db) {
super.onCreate(db);
Log.e("db" , "创建数据库 onCreate(Database db)");
}
@Override
public void onCreate(SQLiteDatabase db) {
super.onCreate(db);
Log.e("db" , "创建数据库 onCreate(SQLiteDatabase db)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
super.onUpgrade(db, oldVersion, newVersion);
Log.e("db" , "修改数据库 onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)");
}
@Override
public void onUpgrade(Database db, int oldVersion, int newVersion) {
super.onUpgrade(db, oldVersion, newVersion);
Log.e("db" , "修改数据库 onUpgrade(Database db, int oldVersion, int newVersion)");
}
@Override
public void onConfigure(SQLiteDatabase db) {
super.onConfigure(db);
Log.e("db" , "配置数据库 onConfigure(SQLiteDatabase db)");
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
super.onDowngrade(db, oldVersion, newVersion);
Log.e("db" , "回滚数据库 onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion)");
}
@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
Log.e("db" , "打开数据库 onOpen(SQLiteDatabase db)");
}
@Override
public void onOpen(Database db) {
super.onOpen(db);
Log.e("db" , "打开数据库 onOpen(Database db)");
}
}
在app中使用:
首先在application中初始化数据库
public class MyApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
DbCore dbCore = new DbCore();
dbCore.init(this);//初始化数据库
}
}
然后就可以开始在Activity中使用了
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initDataBase();
}
public void initDataBase(){
StudentDbHelper studentDbHelper = DbUtil.getStudentDbHelper();
/*增*/
studentDbHelper.save(new Student());
studentDbHelper.save(new Student() , new Student());
studentDbHelper.save(new ArrayList<Student>());
/*删*/
studentDbHelper.delete(new Student());
studentDbHelper.delete(new Student() , new Student());
studentDbHelper.delete(new ArrayList<Student>());
Long key = 123L;
List<Long> keys = new ArrayList<>();
studentDbHelper.deleteByKey(key);
studentDbHelper.deleteByKey(key , key);
studentDbHelper.deleteByKey(keys);
/*查*/
studentDbHelper.query(key);
studentDbHelper.query("name" , "张三" , "李四");
studentDbHelper.queryAll();
List<Student> result = studentDbHelper.queryBuilder()
.where(StudentDao.Properties.Score.eq("100"))
.orderDesc(StudentDao.Properties.Age)
.list();
/*改*/
studentDbHelper.update(new Student());
studentDbHelper.update(new Student() , new Student());
studentDbHelper.update(new ArrayList<Student>());
studentDbHelper.saveOrUpdate(new Student());
studentDbHelper.saveOrUpdate(new Student() , new Student());
studentDbHelper.saveOrUpdate(new ArrayList<Student>());
}
}