AndroidStudio学习笔记(GreenDao)

試著忘記壹切 提交于 2020-02-15 06:00:14

Android开发中,一说到数据库,刚开始接触的肯定是LitePal
LitePal用起来确实很方便,但这次我们要学习的是GreenDao
GreenDao的优点网上一查就知道,学习GreenDao最直观的区别就是新建DaoManager和DaoUtil的工具类,增删改查操作实现比较直观

添加依赖

build.gradle(整个项目)中

buildscript {
    repositories {
        jcenter()
        mavenCentral() // add repository
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.1'
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin
    }
}

build.gradle(app)中

apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao' // apply plugin
 
dependencies {
    implementation 'org.greenrobot:greendao:3.2.2' // add library
}

创建model类

在创建的model类上加上 @Entity,定义一个long类型的Id加上**@Id**,点击sync,便会自动生成构造函数,getter/setter等函数

@Entity
public class User {
    @Id(autoincrement = true)//自增
    private long Id;
    private String login;
    //....
    }

创建DaoManager和DaoUtil类

public class DaoManager {
    private static final String DB_NAME = "user.db";//数据库名称
    private volatile static DaoManager mDaoManager;//多线程访问
    private static DaoMaster.DevOpenHelper mHelper;
    private static DaoMaster mDaoMaster;
    private static DaoSession mDaoSession;
    private Context context;

    //使用单例模式获得操作数据库的对象
    public static DaoManager getInstance() {
        DaoManager instance = null;
        if (mDaoManager == null) {
            synchronized (DaoManager.class) {
                if (instance == null) {
                    instance = new DaoManager();
                    mDaoManager = instance;
                }
            }
        }
        return mDaoManager;
    }

    //初始化Context对象
    public void init(Context context) {
        this.context = context;
    }

    //判断数据库是否存在,如果不存在则创建
    public DaoMaster getDaoMaster() {
        if (null == mDaoMaster) {
            mHelper = new DaoMaster.DevOpenHelper(context, DB_NAME, null);
            mDaoMaster = new DaoMaster(mHelper.getWritableDatabase());
        }
        return mDaoMaster;
    }

    //完成对数据库的增删查找
    public DaoSession getDaoSession() {
        if (null == mDaoSession) {
            if (null == mDaoMaster) {
                mDaoMaster = getDaoMaster();
            }
            mDaoSession = mDaoMaster.newSession();
        }
        return mDaoSession;
    }

    //设置debug模式开启或关闭,默认关闭
    public void setDebug(boolean flag) {
        QueryBuilder.LOG_SQL = flag;
        QueryBuilder.LOG_VALUES = flag;
    }

    //关闭数据库
    public void closeDataBase() {
        closeHelper();
        closeDaoSession();
    }

    public void closeDaoSession() {
        if (null != mDaoSession) {
            mDaoSession.clear();
            mDaoSession = null;
        }
    }

    public void closeHelper() {
        if (mHelper != null) {
            mHelper.close();
            mHelper = null;
        }
    }
}
public class UserDaoUtil {
    private static final boolean DUBUG = true;
    private DaoManager manager;
    private UserDao userDao;
    private DaoSession daoSession;

    public UserDaoUtil(Context context) {
        manager = DaoManager.getInstance();
        manager.init(context);
        daoSession = manager.getDaoSession();
        manager.setDebug(DUBUG);
    }

    //添加数据,如果有重复则覆盖
    public void insertUser(User user) {
        manager.getDaoSession().insertOrReplace(user);
    }

    //添加多条数据,需要开辟新的线程
    public void insertMultUser(final List<User> users) {
        manager.getDaoSession().runInTx(new Runnable() {
            @Override
            public void run() {
                for (User user : users) {
                    manager.getDaoSession().insertOrReplace(user);
                }
            }
        });
    }


    //删除数据
    public void deleteUser(User user) {
        manager.getDaoSession().delete(user);
    }

    //删除全部数据
    public void deleteAll(Class cls) {
        manager.getDaoSession().deleteAll(cls);
    }

    //更新数据
    public void updateUser(User user) {
        manager.getDaoSession().update(user);
    }

    //按照主键返回单条数据
    public User listOneUser(long key) {
        return manager.getDaoSession().load(User.class, key);
    }

    //查询全部数据
    public List<User> queryAll() {
        return manager.getDaoSession().loadAll(User.class);
    }
}

然后就可以在新建工具类实例来增删改查数据库的内容,还是很方便的
刚刚接触GreenDao,只是学到了皮毛,以后有机会还是要多用多练,学无止境啊

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!