美文网首页
GreenDao的配置以及简单实现增删改查

GreenDao的配置以及简单实现增删改查

作者: TASTUYA | 来源:发表于2018-12-27 11:38 被阅读0次
    配置GreenDao

    1.在项目的build.grade文件里加入一行代码
    classpath 'org.greenrobot:greendao-gradle-plugin:3.2.0'

    2.在app的build.grade里面加上代码
    apply plugin: 'org.greenrobot.greendao'

    3.在app的build.grad里面Android{}里加上代码

     greendao{
     schemaVersion 1 //指定数据库schema版本号,迁移等操作会用到
     //包名是活的这是变动的
      daoPackage 'soexample.umeng.com.greendaodemo.mydao' //dao的包名,包名默认是entity所在的包;
     targetGenDir 'src/main/java'//生成数据库文件的目录
      }
    

    4.导入依赖
    需要新建一个实体类 通过@Entity
    通过锤子(make Project)
    1:DaoMaster
    2:DaoSession
    3:StudentDao

    GreenDao代码:

    在App中新建数据库 在MainActivity中调用,一定要早AndroidManifest.xml中注册!

    public class MyApp extends Application {
    
        private static DaoSession daoSession;
    
        @Override
        public void onCreate() {
            super.onCreate();
            initGreenDao();
        }
    
        private void initGreenDao() {
            //创建OpenHelper类
            DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(this, "user.db");
            //开启一个可写的数据库类
            SQLiteDatabase writableDatabase = openHelper.getWritableDatabase();
            //通过DaoMaster封装
            DaoMaster master = new DaoMaster(writableDatabase);
            daoSession = master.newSession();
        }
    
        public static DaoSession getDaoSession() {
            return daoSession;
        }
    }
    

    在MainActivity中的具体使用

     public class MainActivity extends AppCompatActivity {
    
        @BindView(R.id.insert)
        Button insert;
        @BindView(R.id.delete)
        Button delete;
        @BindView(R.id.update)
        Button update;
        @BindView(R.id.select)
        Button select;
        @BindView(R.id.Get_Content)
        TextView GetContent;
        private StudentDao studentDao;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ButterKnife.bind(this);
            studentDao = MyApp.getDaoSession().getStudentDao();
    
        }
    
        @OnClick({R.id.insert, R.id.delete, R.id.update, R.id.select})
        public void onViewClicked(View view) {
            switch (view.getId()) {
                case R.id.insert:
                    insertData();
                    break;
                case R.id.delete:
                    deleteData();
                    break;
                case R.id.update:
                    updateData();
                    break;
                case R.id.select:
                    selectData();
                    break;
            }
        }
    
        private void updateData() {
            Student student = studentDao.load(3l);
            student.setAge(18);
            student.setSex("美");
            studentDao.update(student);
            Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
        }
    
        private void deleteData() {
            studentDao.deleteByKey(2l);
            Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
        }
    
        private void selectData() {
            GetContent.setText("");
            List<Student> students = studentDao.loadAll();
            GetContent.setText(students.toString());
        }
    
        private void insertData() {
            Student student = new Student("哈哈哈", "帅", 3);
            long insert = studentDao.insert(student);
            if (insert > 0) {
                Toast.makeText(this, "插入成功", Toast.LENGTH_SHORT).show();
            }
        }
    }
    

    运行效果:


    image.png

    相关文章

      网友评论

          本文标题:GreenDao的配置以及简单实现增删改查

          本文链接:https://www.haomeiwen.com/subject/fokrlqtx.html