Android 简单GreenDao增删改查

来源:互联网 发布:tensorflow翻译 编辑:程序博客网 时间:2024/05/12 09:32

效果展示

这里写图片描述

一,在as中导入相关的包

compile 'org.greenrobot:greendao:3.2.0'
  • 1

二,在build.gradle中进行配置:

最上面引包

apply plugin: 'org.greenrobot.greendao'
  • 1
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
  • 1

三,自定义路径

在app的build.gradle里面的Android中配置

greendao {        schemaVersion 1        daoPackage '更改自己的包名'        targetGenDir 'src/main/java'    }
  • 1
  • 2
  • 3
  • 4
  • 5

在gradle的根模块中加入上述代码,就完成了我们的基本配置了。
属性介绍:
schemaVersion–> 指定数据库schema版本号,迁移等操作会用到;
daoPackage –> dao的包名,包名默认是entity所在的包;
targetGenDir –> 生成数据库文件的目录;

四,创建一个User的实体类

@Entitypublic class User {    @Id     private Long id;     private String name;     @Transient     private int tempUsageCount; // not persisted  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

五,MakeProject

编译项目,User实体类会自动编译,生成get、set方法并且会在com.anye.greendao.gen目录下生成三个文件;
这里写图片描述

MyApplication(一定要在清单文件里配置它)

public class MyApplication extends Application {    private DaoMaster.DevOpenHelper mHelper;    private SQLiteDatabase db;    private DaoMaster mDaoMaster;    private DaoSession mDaoSession;    public static MyApplication instances;    @Override    public void onCreate() {        super.onCreate();        instances = this;        setDatabase();    }    public static MyApplication getInstances(){        return instances;    }    /**     * 设置greenDao     */    private void setDatabase() {        // 通过 DaoMaster 的内部类 DevOpenHelper,你可以得到一个便利的 SQLiteOpenHelper 对象。        // 可能你已经注意到了,你并不需要去编写「CREATE TABLE」这样的 SQL 语句,因为 greenDAO 已经帮你做了。        // 注意:默认的 DaoMaster.DevOpenHelper 会在数据库升级时,删除所有的表,意味着这将导致数据的丢失。        // 所以,在正式的项目中,你还应该做一层封装,来实现数据库的安全升级。        mHelper = new DaoMaster.DevOpenHelper(this, "notes-db", null);        db = mHelper.getWritableDatabase();        // 注意:该数据库连接属于 DaoMaster,所以多个 Session 指的是相同的数据库连接。        mDaoMaster = new DaoMaster(db);        mDaoSession = mDaoMaster.newSession();    }    public DaoSession getDaoSession() {        return mDaoSession;    }    public SQLiteDatabase getDb() {        return db;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

主MainActivity

 private UserDao userDao;    private Button butten1,butten2,butten3,butten4;    private Long id;    private User anye3;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        butten1=(Button)findViewById(R.id.butten1);        butten2=(Button)findViewById(R.id.butten2);        butten3=(Button)findViewById(R.id.butten3);        butten4=(Button)findViewById(R.id.butten4);        userDao = MyApplication.getInstances().getDaoSession().getUserDao();        /*        * 添加        * */        butten1.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                anye3 = new User(null, "anye3");                userDao.insert(anye3);//添加一个                Log.d("添加了:===",""+ anye3);            }        });         /*        * 删除        * */        butten4.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                userDao.deleteByKey(id);                Log.d("删除了",""+id);            }        });        /*        * 修改        * */        butten3.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                anye3 = new User(id, "anye0803");                userDao.update(anye3);                Log.d("修改了:",""+anye3);            }        });        /*        *查询        * */        butten2.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                List<User> users = userDao.loadAll();                String userName = "";                for (int i = 0; i < users.size(); i++) {                    userName += users.get(i).getName()+",";                    id = users.get(i).getId();                }                Log.d("查询全部数据==>",""+userName);            }        });    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

activity_main

<Button        android:id="@+id/butten1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="添加"        />    <Button        android:id="@+id/butten2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="查询"        />    <Button        android:id="@+id/butten3"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="修改"        />    <Button        android:id="@+id/butten4"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="删除"        />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

谢谢观看,小编祝大家生活愉快!(多多关注小编,会有非常之多精彩分享哦!)

            <link rel="stylesheet" href="http://s.csdnimg.cn/static/production/markdown_views-d4dade9c33.css">                </div>
原创粉丝点击