greenrobot.greendao

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/user_app/article/details/80318136

导入依赖

Module-gradle:
apply plugin: 'org.greenrobot.greendao'

自定义版本,包名,路径
greendao{
        schemaVersion 1
        daoPackage 'com.fan.greendao.gen'
        targetGenDir 'src/main/java'
    }

    dependencies {
    implementation 'org.greenrobot:greendao:3.2.2'
            }
Project-gradle:
buildscript {

    repositories {
        google()
        jcenter()
        mavenCentral()//添加的新方法
    }

    dependencies {

        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
        }

//——————————–简单应用——————-

//  创建dbmanager类初始化数据库
 DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(getApplicationContext(), "fan.user.db", null);
        // 添加写的权限
        DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDb());
        // 获取userDao
        daoSession = daoMaster.newSession();

点击事件
 case R.id.add:
                String name = mWhritText1.getText().toString();
                String age = mWhritText2.getText().toString();
                String id = mWhritText3.getText().toString();
                insertuser(daoSession.getUsersDao(), name, age, Integer.parseInt(id));
                Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
                queryDatas(daoSession.getUsersDao());
                break;
            case R.id.remove:
                String s = mWhritText3.getText().toString();
                removeDatas(daoSession.getUsersDao(), Integer.parseInt(s));
                Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
                queryDatas(daoSession.getUsersDao());
                break;
            case R.id.update:
                String names = mWhritText1.getText().toString();
                String ages = mWhritText2.getText().toString();
                String ids = mWhritText3.getText().toString();
                updateDatas(daoSession.getUsersDao(), names, ages, Integer.parseInt(ids));
                Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
                queryDatas(daoSession.getUsersDao());
                break;
            case R.id.qureen:
                Toast.makeText(this, "查询成功", Toast.LENGTH_SHORT).show();
                queryDatas(daoSession.getUsersDao());
                break;

//——————————–增删改查——————-

                //    添加
    public void insertuser(UsersDao usersDao, String name, String age, long id) {
        Users users = new Users(id, name, age);
        usersDao.insert(users);
    }
//    删除
    public void removeDatas(UsersDao usersDao, long key) {
        usersDao.deleteByKey(key);
    }
//    修改
    public void updateDatas(UsersDao usersDao, String name, String age, long id) {
        Users users = new Users(id, name, age);
        usersDao.update(users);
    }
//    查询
    public void queryDatas(UsersDao usersDao) {
        List<Users> usersList = usersDao.loadAll();
        list.clear();
        for (int i = 0; i < usersList.size(); i++) {
            String name1 = usersList.get(i).getName();
            String age1 = usersList.get(i).getAge();
            long id1 = usersList.get(i).getId();

            list.add(new Users(id1, name1, age1));
        }
        adapter.notifyDataSetChanged();
    }

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页