1. 在project的gradle中添加依赖,如下图:

代码: classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin

使用greenDao操作SQLite数据库

  1. 在app的gradle中添加依赖,如下图

使用greenDao操作SQLite数据库

代码:

第一部分:

greendao {

schemaVersion 1 //数据库版本号

daoPackage 'cn.humiao.myserialport.greenDao'

// 设置DaoMaster、DaoSession、Dao 包名

targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录,请注意,这里路径用.不要用/

// generateTests false //设置为true以自动生成单元测试。

// targetGenDirTests 'src/main/java' //应存储生成的单元测试的基本目录。默认为 src / androidTest / java。

}

第二部分:

compile 'org.greenrobot:greendao:3.2.2' // add library

  1. Rebuild项目,在之前设置的路径下生成greenDao包

使用greenDao操作SQLite数据库

  1. 创建MyApplication(随便哪边,我是在cn.humiao.myserialport下创建的)

package cn.humiao.myserialport;

 

import android.app.Application;

import android.database.sqlite.SQLiteDatabase;

 

import cn.humiao.myserialport.greenDao.DaoMaster;

import cn.humiao.myserialport.greenDao.DaoSession;

 

/**

* Author:duanmu

* Date:2019/9/4 0004

* Description:

*/

public class MyApplication extends Application {

public static final String DB_NAME = "app.db";

private static DaoSession mDaoSession;

 

@Override

public void onCreate() {

super.onCreate();

initGreenDao();

}

/**

* 初始化GreenDao,直接在Application中进行初始化操作

*/

private void initGreenDao() {

DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, DB_NAME);

SQLiteDatabase db = helper.getWritableDatabase();

DaoMaster daoMaster = new DaoMaster(db);

mDaoSession = daoMaster.newSession();

}

 

public static DaoSession getmDaoSession() {

return mDaoSession;

}

 

}

 

  1. 注册MyApplication,如下图:

使用greenDao操作SQLite数据库

  1. 到这边就告一段落了,后面就去创建实体类、创建完实体类不要忘记去rebuild项目在包greenDao中生成对应的文件;然后开始你的增删改查之旅了,下面展示实体类的一个示例:

package cn.humiao.myserialport.Entity;

 

import org.greenrobot.greendao.annotation.Entity;

import org.greenrobot.greendao.annotation.Id;

import org.greenrobot.greendao.annotation.NotNull;

import org.greenrobot.greendao.annotation.Generated;

 

/**

* Author:duanmu

* Date:2019/9/5 0005

* Description:用户信息实体类

* @Id(autoincrement = true)通过这个注解标记的字段必须是Long类型的,这个字段在数据库中表示它就是主键,并且它默认就是自增的

* @NotNull 设置数据库表当前列不能为空

* @Property:设置一个非默认关系映射所对应的列名,默认是使用字段名,例如:@Property(nameInDb = "name")

* @Transient:表明这个字段不会被写入数据库,只是作为一个普通的java类字段,用来临时存储数据的,不会被持久化

*/

@Entity

public class UserInfo {

@Id(autoincrement = true)

private Long id;//唯一标识

@NotNull

private String WorkNumber;//工号

@NotNull

private String UserName;// 姓名

@NotNull

private String Password;// 密码

@NotNull

private String Phone;// 电话

@NotNull

private String Sex;// 性别

@NotNull

private String Permission;// 权限

@Generated(hash = 215100266)

public UserInfo(Long id, @NotNull String WorkNumber, @NotNull String UserName,

@NotNull String Password, @NotNull String Phone, @NotNull String Sex,

@NotNull String Permission) {

this.id = id;

this.WorkNumber = WorkNumber;

this.UserName = UserName;

this.Password = Password;

this.Phone = Phone;

this.Sex = Sex;

this.Permission = Permission;

}

@Generated(hash = 1279772520)

public UserInfo() {

}

public Long getId() {

return this.id;

}

public void setId(Long id) {

this.id = id;

}

public String getWorkNumber() {

return this.WorkNumber;

}

public void setWorkNumber(String WorkNumber) {

this.WorkNumber = WorkNumber;

}

public String getUserName() {

return this.UserName;

}

public void setUserName(String UserName) {

this.UserName = UserName;

}

public String getPassword() {

return this.Password;

}

public void setPassword(String Password) {

this.Password = Password;

}

public String getPhone() {

return this.Phone;

}

public void setPhone(String Phone) {

this.Phone = Phone;

}

public String getSex() {

return this.Sex;

}

public void setSex(String Sex) {

this.Sex = Sex;

}

public String getPermission() {

return this.Permission;

}

public void setPermission(String Permission) {

this.Permission = Permission;

}

 

}

 

 

相关文章:

  • 2018-04-26
  • 2021-07-23
  • 2022-12-23
  • 2021-09-19
  • 2022-12-23
  • 2021-05-29
  • 2022-12-23
  • 2021-07-11
猜你喜欢
  • 2021-10-14
  • 2022-12-23
  • 2022-12-23
  • 2021-06-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-03
相关资源
相似解决方案