转载请标明地址http://blog.csdn.net/qq_30034925/article/details/54729734
GreenDao官网说它是最好的数据库框架,我们也当真吧,反真它的更新进度给力,最近都有在更新。也使得我们android程序员使用起来越来越方便。至于为什么使用它这里不做多解析。进入正题。
首先我们需要在项目build.gradle中导入包`
dependencies { classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1' }
接着在model中的build.gradle中导入包
dependencies { compile 'org.greenrobot:greendao:3.2.0' compile 'org.greenrobot:greendao-generator:3.2.0'}
接着在模块build.gradle的顶部添加
apply plugin: ‘org.greenrobot.greendao’
以及
注:这个建议不写,如果不需要修改其代码的话,一般不需要下面的配置
greendao { schemaVersion 1 daoPackage 'com.jian.greendao.gen' targetGenDir 'src/main/java' }
同步后就可以使用greenDao。
先来看看greenDao强大的自动生成代码功能
创建实体类
@Entitypublic class User { @Id(autoincrement = true) private Long id; private String name; private String sex; private int age; private int salary; }
我们字需要编写这些,大家应该看到,在最顶部有一个@Entity,这个标志告诉greenDao,这个是我需要生成的表。接着点击 Build -> Make Project(快捷键ctrl+F9),将会自动为我们生成需要的类和代码
生成的User类
@Entitypublic class User { @Id(autoincrement = true) private Long id; private String name; private String sex; private int age; private int salary; @Generated(hash = 257789183) public User(Long id, String name, String sex, int age, int salary) { this.id = id; this.name = name; this.sex = sex; this.age = age; this.salary = salary; } @Generated(hash = 586692638) public User() { } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public String getSex() { return this.sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return this.age; } public void setAge(int age) { this.age = age; } public int getSalary() { return this.salary; } public void setSalary(int salary) { this.salary = salary; }}
- 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
生成的类
创建App类,继承Application
public class App extends Application { public static final boolean ENCRYPTED = true; private DaoSession daoSession; @Override public void onCreate() { super.onCreate(); DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, ENCRYPTED ? "users-db-encrypted" : "users-db"); Database db = helper.getWritableDb(); daoSession = new DaoMaster(db).newSession(); } public DaoSession getDaoSession() { return daoSession; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
tip:别忘记在manifeste把它添加进去
这个类起到防止重复创建数据库的操作。当然也可以使用单例模式来实现。
使用GreenDao进行增删改查
package com.example.jian.asked;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.RecyclerView;import com.example.greendao.gen.DaoSession;import com.example.greendao.gen.UserDao;import org.greenrobot.greendao.query.Query;import org.greenrobot.greendao.query.QueryBuilder;import java.util.List;public class MainActivity extends AppCompatActivity { private Query<User> userQuery; private RecyclerView recyclerView; private MyAdapter myAdapter ; UserDao userDao; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DaoSession daoSession =((App) getApplication()).getDaoSession(); userDao = daoSession.getUserDao(); userQuery = userDao.queryBuilder().orderAsc(UserDao.Properties.Id).build(); } private void insertUser(){ User user = new User(null,"jianguotang", "男",18,2000); userDao.insert(user); } private void deleteUser(){ userDao.deleteByKey(5l); } /** * 对位置 为position的的数据进行修改 * @param position */ private void updateUser(Long position){ User user = userDao.load(5l); user.setName("简国堂"); userDao.update(user); } private List<User> queryList(){ List<User> users = userQuery.list(); return users; } /** * 按照属性name和sex来查询user * @param name */ private List<User> queryByName(String name,String sex){ QueryBuilder<User> builder = userDao.queryBuilder(); Query<User> query = builder .where(UserDao.Properties.Name.eq(name),UserDao.Properties.Sex.eq(sex)) .build(); List<User> list = query.list(); return list; }}
- 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
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
GreenDao注解
@Entity 用于标识这是一个需要Greendao帮我们生成代码的bean@Id 标明主键,括号里可以指定是否自增@Property 用于设置属性在数据库中的列名(默认不写就是保持一致)@NotNull 非空@Transient 标识这个字段是自定义的不会创建到数据库表里@Unique 添加唯一约束
@ToOne 是将自己的一个属性与另一个表建立关联(外键)
@ToMany的属性referencedJoinProperty,类似于外键约束。
@JoinProperty 对于更复杂的关系,可以使用这个注解标明目标属性的源属性。