GreenDao

GreenDao 优点:
 
1.性能高,号称Android最快的关系型数据库
2.内存占用小
3.库文件比较小,小于100K,编译时间低,而且可以避免65K方法限制
4.支持数据库加密 greendao支持SQLCipher进行数据库加密 有关SQLCipher可以参考这篇博客 Android数据存储之Sqlite采用SQLCipher数据库加密实战
5.简洁易用的API
 
第一步 先在 项目的Project 的 buil.gradle 里 在buildscript- repositories添加配置mavenCentral(),
 
在dependencies 里添加
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
 
 
buildscript { repositories { jcenter() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:2.3.3' classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' } }
第二步 在自己想要用的
GreenDao Module 里的 dependencies 里添加
compile 'org.greenrobot:greendao:3.2.2'
 
 
头部 添加
apply plugin: 'org.greenrobot.greendao'
 
在Android中添加
greendao{
schemaVersion 1
daoPackage'com.afa.tourism.greendao.gen'
targetGenDir'src/main/java'
}
 
现在已经配置好了可以使用了
 
创建新的实体类
不需要写set get 方法。 他自动帮我们生成.
 
 
@Entity //指定为数据库类的对象  
public class User {    
    @Id(autoincrement=true)   //此ID必须有且为long类型 autoincrement=true为自增长 
    private Long id;    
@Property(nameInDb = "username") //映射所对应的列名为 username
    private String name;  
    @Transient  //这个注解意思为这个属性不会保存到数据库为属性 在数据库中不对这个属性进行操作  
    private int tempUsageCount; // not persisted  
  } 
 
自己生成的三个核心类
 
 
基本上这样的配置就算完成了,简单的写一下增删改查的类
public class DBManager {
private final static String dbName = "test_db";
private static DBManager mInstance;
private DaoMaster.DevOpenHelper openHelper;
private Context context;
 
private DBManager(Context context) {
this.context = context;
openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
}
public static DBManager getInstance(Context context) {
if (mInstance == null) {
synchronized (DBManager.class) {
if (mInstance == null) {
mInstance = new DBManager(context);
}
}
}
return mInstance;
}
/**
* 获取可读数据库
*/
private SQLiteDatabase getReadableDatabase() {
if (openHelper == null) {
openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
}
SQLiteDatabase db = openHelper.getReadableDatabase();
return db;
}
/**
* 获取可写数据库
*/
private SQLiteDatabase getWritableDatabase() {
if (openHelper == null) {
openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
}
SQLiteDatabase db = openHelper.getWritableDatabase();
return db;
}
/**
* 插入一条记录
*
* @param user
*/
public void insertUser(User user) {
DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
DaoSession daoSession = daoMaster.newSession();
UserDao userDao = daoSession.getUserDao();
userDao.insert(user);
}
 
/**
* 插入用户集合
*
* @param users
*/
public void insertUserList(List users) {
if (users == null || users.isEmpty()) {
return;
}
DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
DaoSession daoSession = daoMaster.newSession();
UserDao userDao = daoSession.getUserDao();
userDao.insertInTx(users);
}
/**
* 删除一条记录
*
* @param user
*/
public void deleteUser(User user) {
DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
DaoSession daoSession = daoMaster.newSession();
UserDao userDao = daoSession.getUserDao();
userDao.delete(user);
}
/**
* 更新一条记录
*
* @param user
*/
public void updateUser(User user) {
DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
DaoSession daoSession = daoMaster.newSession();
UserDao userDao = daoSession.getUserDao();
userDao.update(user);
}
/**
* 查询用户列表
*/
public List queryUserList() {
DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
DaoSession daoSession = daoMaster.newSession();
UserDao userDao = daoSession.getUserDao();
QueryBuilder qb = userDao.queryBuilder();
List list = qb.list();
return list;
}
}
 
测试程序
DBManager dbManager = DBManager.getInstance(this); for (int i = 0; i < 5; i++) { User user = new User(); user.setId(i); user.setAge(i * 3); user.setName("第" + i + "人"); dbManager.insertUser(user); } List userList = dbManager.queryUserList(); for (User user : userList) { Log.e("TAG", "queryUserList--before-->" + user.getId() + "--" + user.getName() +"--"+user.getAge()); if (user.getId() == 0) { dbManager.deleteUser(user); } if (user.getId() == 3) { user.setAge(10); dbManager.updateUser(user); } } userList = dbManager.queryUserList(); for (User user : userList) { Log.e("TAG", "queryUserList--after--->" + user.getId() + "---" + user.getName()+"--"+user.getAge()); }
 
运行结果
 

转载于:https://www.cnblogs.com/ysxy/p/8807283.html

你可能感兴趣的:(GreenDao)