GreenDao的使用

xiaoxiao2021-02-28  54

导依赖

compile'org.greenrobot:greendao:3.0.1' compile'org.greenrobot:greendao-generator:3.0.0'
二,在build.gradle中进行配置:
apply plugin: 'org.greenrobot.greendao' buildscript { repositories { mavenCentral() } dependencies { classpath 'org.greenrobot:greendao-gradle-plugin:3.0.0' } } <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     tools:context="com.example.gjl.day06_greendao.MainActivity">     <EditText         android:id="@+id/name"         android:layout_width="match_parent"         android:layout_height="wrap_content" />     <EditText         android:id="@+id/pass"         android:layout_width="match_parent"         android:layout_height="wrap_content" />     <Button         android:id="@+id/add"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="添加" />     <Button         android:id="@+id/dele"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="删除" />     <Button         android:id="@+id/update"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="修改" />     <Button         android:id="@+id/query"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="查询" /> </LinearLayout> /**  * GreeDao  *      原理:通过注解,反射实现数据的操作的  *  *      将  *          bean-----database  *  *          @Entity  实体类  *          @Id      GreenDao的实体类里面必须要有id,而且是Long  *                  如果想让id是自增的怎么办?GreenDao默认就是自增的。但是建议大家显示的现出来  *          @Property(nameInDb = "username") 规定,数据里面的相应字段的名字  *  */ @Entity实体类 public class Person {     @Id(autoincrement = true)     private Long id;     private String name;     private String password;     public String getPassword() {         return this.password;     }     public void setPassword(String password) {         this.password = password;     }     public String getName() {         return this.name;     }     public void setName(String name) {         this.name = name;     }     public Long getId() {         return this.id;     }     public void setId(Long id) {         this.id = id;     }     @Generated(hash = 82864578)     public Person(Long id, String name, String password) {         this.id = id;         this.name = name;         this.password = password;     }     @Generated(hash = 1024547259)     public Person() {     }     @Override     public String toString() {         return "Person{" +                 "id=" + id +                 ", name='" + name + '\'' +                 ", password='" + password + '\'' +                 '}';     }

}

public class MyApp extends Application {    private DaoSession daoSession;    private static MyApp myApp;    @Override    public void onCreate() {        super.onCreate();        myApp = this;//        DevOpenHelper数据库的帮助类//        1.上下文,2.数据库的名字,3.指针,null 代表使用默认指针        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "demo.db", null);        SQLiteDatabase db = helper.getReadableDatabase();//得到数据库        DaoMaster daoMaster = new DaoMaster(db);        daoSession = daoMaster.newSession();    }    //获取实例    public static MyApp getInstance() {        return myApp;    }    //定义方法,返回DaoSession    public DaoSession getDaoSession() {        return daoSession;    }

}

/** * 1.配置 * 2.初始化 * 3.开始使用 */public class MainActivity extends AppCompatActivity {    private static final String TAG = "MainActivity======";    @BindView(R.id.name)    EditText name;    @BindView(R.id.pass)    EditText pass;    @BindView(R.id.add)    Button add;    @BindView(R.id.dele)    Button dele;    @BindView(R.id.update)    Button update;    @BindView(R.id.query)    Button query;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ButterKnife.bind(this);    }    @OnClick({R.id.add, R.id.dele, R.id.update, R.id.query})    public void onViewClicked(View view) {        switch (view.getId()) {            case R.id.add:                //添加                String name1 = name.getText().toString();                String pass1 = pass.getText().toString();                //                Person person = new Person();                person.setName(name1);                person.setPassword(pass1);                MyApp myApp = MyApp.getInstance();                DaoSession daoSession = myApp.getDaoSession();                //PersonDao具体的操作类                PersonDao personDao = daoSession.getPersonDao();                personDao.insert(person);//添加                break;            case R.id.dele:                //删除所有张三                MyApp myApp2 = MyApp.getInstance();                DaoSession daoSession2 = myApp2.getDaoSession();                PersonDao personDao2 = daoSession2.getPersonDao();                //                List<Person> list1 = personDao2.queryBuilder().where(PersonDao.Properties.Name.like("张三")).build().list();                for (int i=0;i<list1.size();i++){                    personDao2.delete(list1.get(i));                }                break;            case R.id.update:                //修改                MyApp myApp3 = MyApp.getInstance();                DaoSession daoSession3 = myApp3.getDaoSession();                PersonDao personDao3 = daoSession3.getPersonDao();                List<Person> list2 = personDao3.queryBuilder().build().list();                for (int i = 0; i < list2.size(); i++) {                    if (list2.get(i).getName().equals("李四")){                        list2.get(i).setPassword("1234567890");                        personDao3.update(list2.get(i));                    }                }               break;            case R.id.query:                //查询                MyApp myApp1=MyApp.getInstance();                DaoSession daoSession1 = myApp1.getDaoSession();                PersonDao personDao1 = daoSession1.getPersonDao();                //全查                List<Person> list = personDao1.queryBuilder().build().list();//                Log.d(TAG, "onViewClicked: "+list.toString());                //按条件查询//                eq相等//                List<Person> list = personDao1.queryBuilder().where(PersonDao.Properties.Age.gt(12)).build().list();//                Log.d(TAG, "onViewClicked: "+list);//                模糊查询//                List<Person> list = personDao1.queryBuilder().where(PersonDao.Properties.Name.like("%三%")).build().list();                Log.d(TAG, "onViewClicked: "+list);                break;        }    }}

转载请注明原文地址: https://www.6miu.com/read-2626794.html

最新回复(0)