
GreenDao的简单使用
发布日期:2022-02-26 14:49:31
浏览次数:22
分类:技术文章
本文共 2297 字,大约阅读时间需要 7 分钟。
首先导入依赖
将GreenDAO添加到项目中
GreenDAO可在Maven Central上使用。请确保您使用的是最新版本
将以下Gradle配置添加到Android项目中:
// In your root build.gradle file:buildscript { repositories { jcenter() mavenCentral() // add repository } dependencies { classpath 'com.android.tools.build:gradle:3.1.1' classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin }} // In your app projects build.gradle file:apply plugin: 'com.android.application'apply plugin: 'org.greenrobot.greendao' // apply plugin dependencies { implementation 'org.greenrobot:greendao:3.2.2' // add library}
greendao {
schemaVersion 1 //版本
daoPackage '生成文件包名' // 一般为app包名+生成文件的文件夹名
targetGenDir 'src/main/java.greendao' //生成文件路径
}
创建实体类,生成dao文件
@Entitypublic class User { @Id(autoincrement = true) private Long id; //这里必须实用Long封装类型 private String userName;}
之后一下或者Ctrl+F9(Make project),程序会自动编译生成dao文件,生成的文件一共有三个。
在src/main/java.greendao 上面设置的路径里 greendao包中..
之后自己创建工具类
public class SqliteUtils { private SQLiteDatabase db; private DaoMaster mDaoMaster; private DaoSession mSession; private SqliteUtils() { } private static SqliteUtils mSqliteUtils; public static SqliteUtils getSqliteUtils() { if (mSqliteUtils == null) { mSqliteUtils = new SqliteUtils(); } return mSqliteUtils; } //初始化 public void init(Context context) { //这里是数据库的名字"ming1" DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, "ming1"); db = helper.getWritableDatabase(); mDaoMaster = new DaoMaster(db); mSession = mDaoMaster.newSession(); } //增 public void insert(User user){ mSession.getUserDao().insert(user); } //查询全部 public ListqueryAll(){ return mSession.getUserDao().loadAll(); } //查询一条 public User query(Long key){ return mSession.getUserDao().load(key); } //删除全部 public void deleteAll(){ mSession.getUserDao().deleteAll(); } //删除单个 public void delete(String key){ mSession.getUserDao().deleteByKey(Long.parseLong(key)); } //更新 public void update(User user){ mSession.getUserDao().update(user); }}
之在项目中创建app包里面创建App类继承Application在
重写onCreate方法调用里面的init(Context context); 方法传入this
记得在
里面声明~~~
之后就可以使用了大概~
转载地址:https://blog.csdn.net/qq_31353347/article/details/84346837 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2023年05月30日 11时25分49秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
最新文章
示波器使用
2022-01-31
ISE仿真选择布局布线后带有延时信息的仿真模型
2022-01-31
在FPGA Editor中将模块做成Hard Marco
2022-01-31
FPGA中如何固定模块布局
2022-01-31
在版图中只显示一层或多层金属
2022-01-31
Cadence中遇到的问题
2022-01-31
matlab定义循环变量
2022-01-31
matlab画柱状堆叠图
2022-01-31
LabVIEW使用
2022-01-31
常见clock tree结构
2022-01-31
时序分析类笔试题
2022-01-31
编程练习2-筛选出及格和不及格的人从高到底排列
2022-01-31
编程练习3-将文件a处理为文件b
2022-01-31
编程练习1-输入姓,返回名
2022-01-31
Encounter——查看布线通道占用率
2022-01-31
Encounter——查看布线拥堵率 congestion
2022-01-31
Matlab使用注意事项及遇到的问题
2022-01-31
matlab输出字符串,前面补0
2022-01-31
matlab——矩阵相关
2022-01-31
外部中断原理及配置步骤
2022-01-31