美文网首页面试
GreenDao 应用

GreenDao 应用

作者: 01_小小鱼_01 | 来源:发表于2018-04-07 21:16 被阅读2次

    1. 简介

    屏幕快照 2018-04-14 10.36.40.png

    2. 使用方式

    1. 1 在Project的buil.gradle里面添加配置项
    buildscript {  
        repositories {        
            jcenter()       
            mavenCentral()   
        }    
        dependencies {        
            classpath 'com.android.tools.build:gradle:2.1.0'         
            classpath 'org.greenrobot:greendao-gradle-plugin:3.1.0'  
        }
    }
    

    2.2 在Module里面的dependencies里面添加

    apply plugin: 'org.greenrobot.greendao'
    ...
    
    greendao{
            schemaVersion 1
            targetGenDir 'src/main/java/'
    }
    compile'org.greenrobot:greendao:3.1.0'
    compile'org.greenrobot:greendao-generator:3.1.0'
    

    2.3 新建User.java

    @Entity
    public class User {
        @Id
        private Long id;
        @Property(nameInDb = "USERNAME")
        private String username;
        @Property(nameInDb = "NICKNAME")
        private String nickname;
    }
    

    2.4 在Application类之中添加DaoSession对象

    private DaoSession daoSession;
    @Override
    public void onCreate() {
        super.onCreate();
        DaoMaster.DevOpenHelper helper = new 
        DaoMaster.DevOpenHelper(this,"test.db",null);
        Database db = helper.getWritableDb();
        daoSession = new DaoMaster(db).newSession();
    }
    
    public DaoSession getDaoSession() {
        return daoSession;
    }
    

    2.5 数据库的CURD操作

    private DaoSession daoSessin;
    private UserDao dao;
    
    daoSessin = ((MyApplication) getApplication()).getDaoSession();
    dao = daoSessin.getUserDao();
    // 增加
    private void add() {
        User addUser = new User();
        addUser.setUsername("xiaoming");
        addUser.setNickname("angle");
        dao.insert(addUser);
        Toast.makeText(this,"add success.",Toast.LENGTH_SHORT).show();
    }
    
    //删除
    private void delete() {
        List<User> deleteList = dao.queryBuilder().where(
        UserDao.Properties
        .Username.eq("xiaoming"))
        .build().list();
        for (User u : deleteList) {
            dao.delete(u);
        }
        Toast.makeText(this,"delete success.",Toast.LENGTH_SHORT).show();
    }
    
    //修改
    private void modify() {
        List<User> updateList = dao.queryBuilder().where(UserDao.Properties.Username
                                .eq("xiaoming"))
                                .build().list();
        for (User u : updateList) {
            u.setNickname("updated angle");
            dao.update(u);
        }
        Toast.makeText(this,"modify success.",Toast.LENGTH_SHORT).show();
    }
    
    //查找
    private void select() {
        Query<User> userQuery = dao.queryBuilder()
                               .orderAsc(UserDao.Properties.Id)
                               .build();
        List<User> users = userQuery.list();
        String res = "";
        for (User u : users) {
            res += u.toString();
            res += "\r\n";
        }
        message.setText(res);
    }
    

    更多内容

    1. GreenDAO3.0应用指南
    2. Android SQLite详解

    相关文章

      网友评论

        本文标题:GreenDao 应用

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