- 第一步
//学习地址
https://blog.csdn.net/qq_38520096/article/details/78833801
//网站可看
https://github.com/greenrobot/greenDAO
2 . 第二步CV大法
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin
mavenCentral() // add repository
implementation 'org.greenrobot:greendao:3.2.2' // add library
apply plugin: 'org.greenrobot.greendao' // apply plugin
//工具类
//单例数据库
public class SqlliteUtils {
private DaoMaster mdaoMaster;
private SQLiteDatabase db;
private DaoSession daoSession;
public SqlliteUtils() {
}
// //声明对象
// private static SqlliteUtils mSqlliteUtils;
//
// //获取对象
// public static SqlliteUtils getSqlliteUtils() {
// if (mSqlliteUtils == null) {
// mSqlliteUtils=new SqlliteUtils();
//
// }
// return mSqlliteUtils;
// }
//初始化数据库
public void init(Context context){
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, "lin");
db = helper.getWritableDatabase();
mdaoMaster = new DaoMaster(db);
daoSession = mdaoMaster.newSession();
}
//zeng
public void insert(User user){
daoSession.getUserDao().insert(user);
}
//查询全部
public List<User> queryAll(){
return daoSession.getUserDao().loadAll();
}
//查询一条
public User query(Long key){
return daoSession.getUserDao().load(key);
}
//删除全部
public void deleteAll(){
daoSession.getUserDao().deleteAll();
}
//删除单个
public void delete(String key){
daoSession.getUserDao().deleteByKey(Long.parseLong(key));
}
//更新
public void update(User user){
daoSession.getUserDao().update(user);
}
}