一、準備工作 1、項目build.gradle文件下的dependencies中引入插件: 2、在module的build.gradle文件下頂部依賴插件: 3、在module的build.gradle文件下的dependencies中引入依賴: 二、GreenDao的註解 1、實體@Entity註 ...
一、準備工作
1、項目build.gradle文件下的dependencies中引入插件:
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
2、在module的build.gradle文件下頂部依賴插件:
apply plugin: 'org.greenrobot.greendao'
3、在module的build.gradle文件下的dependencies中引入依賴:
compile 'org.greenrobot:greendao:3.2.0'
二、GreenDao的註解
1、實體@Entity註解:
schema:告知GreenDao當前實體屬於哪個schema
active:標記一個實體處於活動狀態,活動實體有更新、刪除和刷新方法
nameInDb:在數據中使用的別名,預設使用的是實體的類名
indexes:定義索引,可以跨越多個列
createInDb:標記創建資料庫表
2、基礎屬性註解
@Id :主鍵 Long型,可以通過@Id(autoincrement = true)設置自增長
@Property:設置一個非預設關係映射所對應的列名,預設是的使用欄位名 舉例:@Property (nameInDb="name")
@NotNul:設置資料庫表當前列不能為空
@Transient :添加次標記之後不會生成資料庫表的列
3、索引註解
@Index:使用@Index作為一個屬性來創建一個索引,通過name設置索引別名,也可以通過unique給索引添加約束
@Unique:向資料庫列添加了一個唯一的約束
4、關係註解
@ToOne:定義與另一個實體(一個實體對象)的關係
@ToMany:定義與多個實體對象的關係
三、簡單使用----使用管理類DBManager,操作名稱為test_db的資料庫,完成資料庫中用戶實體類User的增刪改查等操作
1、創建實體用戶類User,使用GreenDao進行註解,ReBuild項目。
/** * <p>Description: */ @Entity public class User { @Id(autoincrement = true) private Long id ; private String name ; private int age ; public User(String name,int age){ this.name = name ; this.age = age ; } @Generated(hash = 1309193360) public User(Long id, String name, int age) { this.id = id; this.name = name; this.age = age; } @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 int getAge() { return this.age; } public void setAge(int age) { this.age = age; } }View Code
2、創建管理類DBManager,完成對User的增刪改查方法
public class DBManager { private static DBManager instance ; private final static String dbName = "test_db"; private Context mContext ; private DBManager(Context context){ this.mContext = context ; openHelper = new DaoMaster.DevOpenHelper(context,dbName); } private DaoMaster.DevOpenHelper openHelper = null ; public static DBManager getInstance(Context context){ if(instance == null){ synchronized (DBManager.class){ if(instance == null){ instance = new DBManager(context); } } } return instance ; } private SQLiteDatabase getWriteableDatabase(){ if(openHelper == null){ openHelper = new DaoMaster.DevOpenHelper(mContext,dbName); } return openHelper.getWritableDatabase(); } private SQLiteDatabase getRedadableDatabase(){ if(openHelper == null){ openHelper = new DaoMaster.DevOpenHelper(mContext,dbName); } return openHelper.getReadableDatabase() ; } public void saveUser(User user){ DaoMaster daoMaster = new DaoMaster(getWriteableDatabase()); DaoSession daoSession = daoMaster.newSession(); UserDao userDao = daoSession.getUserDao(); //存入user對象 userDao.save(user); } public void saveUsers(List<User> users){ DaoMaster daoMaster = new DaoMaster(getWriteableDatabase()); DaoSession daoSession = daoMaster.newSession(); UserDao userDao = daoSession.getUserDao(); userDao.saveInTx(users); } public void delteUser(User user){ DaoMaster daoMaster = new DaoMaster(getWriteableDatabase()); DaoSession daoSession = daoMaster.newSession(); UserDao userDao = daoSession.getUserDao(); userDao.delete(user); } public void updateUser(User user){ DaoMaster daoMaster = new DaoMaster(getWriteableDatabase()); DaoSession daoSession = daoMaster.newSession(); UserDao userDao = daoSession.getUserDao(); userDao.update(user); } public List<User> queryUser(){ DaoMaster daoMaster = new DaoMaster(getRedadableDatabase()); DaoSession daoSession = daoMaster.newSession(); UserDao userDao = daoSession.getUserDao(); QueryBuilder<User> qb = userDao.queryBuilder(); List<User> list = qb.list(); return list ; } public List<User> queryUser(int age){ DaoMaster daoMaster = new DaoMaster(getRedadableDatabase()); DaoSession daoSession = daoMaster.newSession(); UserDao userDao = daoSession.getUserDao(); QueryBuilder<User> qb = userDao.queryBuilder(); //添加查詢條件 qb.where(UserDao.Properties.Age.eq(age)); List<User> list = qb.list(); return list ; } }View Code