I. Introduction of GreenDao
GreenDAO is an open source Android ORM (“Object/Relational Mapping”), through ORM (called “Object/Relational Mapping”), it saves us Time in the database development process!
Through GreenDao, we can operate the database more quickly. We can use simple object-oriented APIs to store, update, delete and query Java objects.
The advantages and disadvantages of GreenDao?
1. High performance
2. Powerful API that is easy to use, covering relationships and connections
3. Minimal memory consumption
4. Library size (<100KB) to keep the build time low and avoid the 65k method limitation
5. Database encryption: greenDAO supports SQLCipher to ensure user data security;
The core classes of GreenDao
There are three core classes of GreenDao: DaoMaster, DaoSession, XXXDao, these three classes will be created automatically, no need to create by yourself!
- DaoMaster:: DaoMaster saves database objects and manages DAO classes of specific modes. It has static methods to create tables or delete them. Its internal classes OpenHelper and DevOpenHelper are SQLiteOpenHelper implementations, which create schemas in SQLite databases.
- DaoSession: Manage all available DAO objects of a specific mode, you can use one of the getter methods to get the object. DaoSession also provides some general persistence methods, such as entity insertion, loading, updating, refreshing and deleting.
- XXXDao: Data Access Object (DAO) persists and queries entities. For each entity, greenDAO generates DAO. It has more persistence methods than DaoSession.
- Entities: Persistent objects. Usually, the entity object represents a database row using standard Java attributes (such as a POJO or JavaBean).
2. How to use GreenDao
1. Import Gradle plug-in and Dao code generation
To use GreenDao in an Android project, you need to add GreenDao Gradle plugin and add GreenDao library:
a). Import plugin
// Add in the build.gradle file of the Project:
buildscript {
repositories {
jcenter()
mavenCentral() // add repository
}
dependencies {
classpath ‘com.android.tools.build:gradle:3.1.2‘
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin
}
}
b). Configuration related dependencies
// Add in the build.gradle file of Moudle:app:
apply plugin: ‘com.android.application‘
apply plugin: 'org.greenrobot.greendao' // apply plugin
dependencies {
implementation 'org.greenrobot:greendao:3.2.2' // add library
}
c). Configure database related information
greendao {
schemaVersion 1 //database version number
daoPackage ‘com.renhui.testapp.functions.database.greenDao.db’
// Set DaoMaster, DaoSession, Dao package name
targetGenDir'src.main.java'//Set DaoMaster, DaoSession, Dao directories, Please note that the path is used here. Do not use/
generateTests false //Set to true to automatically generate unit tests.
targetGenDirTests'src/main/java' //The basics of the generated unit tests should be stored content. The default is src / androidTest / java.
}
The configuration is complete, use Build> Make Project in Android Studio to rewrite the build project, and the GreenDao integration is complete!
2. Create a storage object entity class
To use GreenDao to store data, you only need to declare the @Entity annotation in front of the storage data class and let GreenDao be It generates the necessary code:
@Entity
public class Student {
@Id(autoincrement = true)
Long id;
@Unique
int studentNo;//Student ID
int age; //Age
String telPhone;//Mobile phone number
String sex; //Gender
String name;//Name
String address;//Home address
String schoolName;//School name
String grade;//How many grades
……getter and setter and constructor method……
}
3. GreenDao initialization
We can maintain a global session in Application. We initialize the database in Applicaiton: =
/**
* Initialize GreenDao and perform initialization directly in Application
*/
private void initGreenDao() {
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "aserbao.db");
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
}
private DaoSession daoSession;
public DaoSession getDaoSession() {
return daoSession;
}
After the initialization is complete, rebuild the project and you will find that three class files are generated in the set targetGenDir directory. This is automatically generated by GreenDao! It means that the database is connected, we only need to add, delete, modify and check the database next.
4. Use GreenDao to add, delete, modify and check
1. Add
insert() insert Data
@Override
public void insertData(Thing s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
for (int i = 0; i <1000 ; i++) {
Student student = new Student();
student.setStudentNo(i);
int age = mRandom.nextInt(10) + 10;
student.setAge(age);
student.setTelPhone(RandomValue.getTel());
String chineseName = RandomValue.getChineseName();
student.setName(chineseName);
if (i% 2 == 0) {
student.setSex("男");
} else {
student.setSex("女");
}
student.setAddress(RandomValue.getRoad());
student.setGrade(String.valueOf(age % 10) + "age");
student.setSchoolName(RandomValue.getSchoolName());
daoSession.insert(student);
}
}
insertOrReplace() If the data exists, replace, and if the data does not exist, insert
@Override
public void insertData(Thing s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
for (int i = 0; i <1000 ; i++) {
Student student = new Student();
student.setStudentNo(i);
int age = mRandom.nextInt(10) + 10;
student.setAge(age);
student.setTelPhone(RandomValue.getTel());
String chineseName = RandomValue.getChineseName();
student.setName(chineseName);
if (i% 2 == 0) {
student.setSex("男");
} else {
student.setSex("女");
}
student.setAddress(RandomValue.getRoad());
student.setGrade(String.valueOf(age % 10) + "age");
student.setSchoolName(RandomValue.getSchoolName());
daoSession.insertOrReplace(student);//insert or replace span>
}
}
2. Delete
There are two ways to delete: delete() and deleteAll(); respectively, delete a single and delete all.
@Override
public void deleteData(Student s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.delete(s);
}
@Override
public void deleteAll() {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.deleteAll(Student.class);
}
3. Change
Modify through update:
@Override
public void updataData(Student s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.update(s);
}
4. Check
The query methods are:
- loadAll(): query all data.
- queryRaw(): Query according to conditions.
- queryBuilder(): to facilitate the creation of queries, which will be explained in detail later.
public List queryAll () {
Liststudents = daoSession.loadAll(Student.class) ;
return students;
}
@Override
public void queryData(String s) {
Liststudents = daoSession.queryRaw(Student.class, "where id = ?", s);
mDataBaseAdapter.addNewStudentData(students);
}
Reference material: https://www .jianshu.com/p/53083f782ea2
With GreenDao, we can operate more quickly In the database, we can use simple object-oriented APIs to store, update, delete and query Java objects.
The advantages and disadvantages of GreenDao?
1. High performance
2. Powerful API that is easy to use, covering relationships and connections
3. Minimal memory consumption
4. Library size (<100KB) to keep the build time low and avoid the 65k method limitation
5. Database encryption: greenDAO supports SQLCipher to ensure user data security;
The core classes of GreenDao
There are three core classes of GreenDao: DaoMaster, DaoSession, XXXDao, these three classes will be created automatically, no need to create by yourself!
- DaoMaster:: DaoMaster saves database objects and manages DAO classes of specific modes. It has static methods to create tables or delete them. Its internal classes OpenHelper and DevOpenHelper are SQLiteOpenHelper implementations, which create schemas in SQLite databases.
- DaoSession: Manage all available DAO objects of a specific mode, you can use one of the getter methods to get the object. DaoSession also provides some general persistence methods, such as entity insertion, loading, updating, refreshing and deleting.
- XXXDao: Data Access Object (DAO) persists and queries entities. For each entity, greenDAO generates DAO. It has more persistence methods than DaoSession.
- Entities: Persistent objects. Usually, the entity object represents a database row using standard Java attributes (such as a POJO or JavaBean).
2. How to use GreenDao
1. Import Gradle plug-in and Dao code generation
To use GreenDao in an Android project, you need to add GreenDao Gradle plugin and add GreenDao library:
a). Import plugin
// Add in the build.gradle file of the Project:
buildscript {
repositories {
jcenter()
mavenCentral() // add repository
}
dependencies {
classpath ‘com.android.tools.build:gradle:3.1.2‘
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin
}
}
b). Configuration related dependencies
// Add in the build.gradle file of Moudle:app:
apply plugin: ‘com.android.application‘
apply plugin: 'org.greenrobot.greendao' // apply plugin
dependencies {
implementation 'org.greenrobot:greendao:3.2.2' // add library
}
c). Configure database related information
greendao {
schemaVersion 1 //database version number
daoPackage ‘com.renhui.testapp.functions.database.greenDao.db’
// Set DaoMaster, DaoSession, Dao package name
targetGenDir'src.main.java'//Set DaoMaster, DaoSession, Dao directories, Please note that the path is used here. Do not use/
generateTests false //Set to true to automatically generate unit tests.
targetGenDirTests'src/main/java' //The basics of the generated unit tests should be stored content. The default is src / androidTest / java.
}
The configuration is complete, use Build> Make Project in Android Studio to rewrite the build project, and the GreenDao integration is complete!
2. Create a storage object entity class
To use GreenDao to store data, you only need to declare the @Entity annotation in front of the storage data class and let GreenDao be It generates the necessary code:
@Entity
public class Student {
@Id(autoincrement = true)
Long id;
@Unique
int studentNo;//Student ID
int age; //Age
String telPhone;//Mobile phone number
String sex; //Gender
String name;//Name
String address;//Home address
String schoolName;//School name
String grade;//How many grades
……getter and setter and constructor method……
}
3. GreenDao initialization
We can maintain a global session in Application. We initialize the database in Applicaiton: =
/**
* Initialize GreenDao and perform initialization directly in Application
*/
private void initGreenDao() {
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "aserbao.db");
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
}
private DaoSession daoSession;
public DaoSession getDaoSession() {
return daoSession;
}
After the initialization is complete, rebuild the project and you will find that three class files are generated in the set targetGenDir directory. This is automatically generated by GreenDao! It means that the database is connected, we only need to add, delete, modify and check the database next.
4. Use GreenDao to add, delete, modify and check
1. Add
insert() insert Data
@Override
public void insertData(Thing s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
for (int i = 0; i <1000 ; i++) {
Student student = new Student();
student.setStudentNo(i);
int age = mRandom.nextInt(10) + 10;
student.setAge(age);
student.setTelPhone(RandomValue.getTel());
String chineseName = RandomValue.getChineseName();
student.setName(chineseName);
if (i% 2 == 0) {
student.setSex("男");
} else {
student.setSex("女");
}
student.setAddress(RandomValue.getRoad());
student.setGrade(String.valueOf(age % 10) + "age");
student.setSchoolName(RandomValue.getSchoolName());
daoSession.insert(student);
}
}
insertOrReplace() If the data exists, replace, and if the data does not exist, insert
@Override
public void insertData(Thing s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
for (int i = 0; i <1000 ; i++) {
Student student = new Student();
student.setStudentNo(i);
int age = mRandom.nextInt(10) + 10;
student.setAge(age);
student.setTelPhone(RandomValue.getTel());
String chineseName = RandomValue.getChineseName();
student.setName(chineseName);
if (i% 2 == 0) {
student.setSex("男");
} else {
student.setSex("女");
}
student.setAddress(RandomValue.getRoad());
student.setGrade(String.valueOf(age % 10) + "age");
student.setSchoolName(RandomValue.getSchoolName());
daoSession.insertOrReplace(student);//insert or replace span>
}
}
2. Delete
There are two ways to delete: delete() and deleteAll(); respectively, delete a single and delete all.
@Override
public void deleteData(Student s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.delete(s);
}
@Override
public void deleteAll() {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.deleteAll(Student.class);
}
3. Change
Modify through update:
@Override
public void updataData(Student s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.update(s);
}
4. Check
The query methods are:
- loadAll(): query all data.
- queryRaw(): Query according to conditions.
- queryBuilder(): to facilitate the creation of queries, which will be explained in detail later.
public List queryAll () {
Liststudents = daoSession.loadAll(Student.class) ;
return students;
}
@Override
public void queryData(String s) {
Liststudents = daoSession.queryRaw(Student.class, "where id = ?", s);
mDataBaseAdapter.addNewStudentData(students);
}
Reference material: https://www .jianshu.com/p/53083f782ea2
Through GreenDao, we can operate the database more quickly, and we can use simple Object-oriented API to store, update, delete and query Java objects.
The advantages and disadvantages of GreenDao?
1. High performance
2. Powerful API that is easy to use, covering relationships and connections
3. Minimal memory consumption
4. Library size (<100KB) to keep the build time low and avoid the 65k method limitation
5. Database encryption: greenDAO supports SQLCipher to ensure user data security;
The core classes of GreenDao
There are three core classes of GreenDao: DaoMaster, DaoSession, XXXDao, these three classes will be created automatically, no need to create by yourself!
- DaoMaster:: DaoMaster saves database objects and manages DAO classes of specific modes. It has static methods to create tables or delete them. Its internal classes OpenHelper and DevOpenHelper are SQLiteOpenHelper implementations, which create schemas in SQLite databases.
- DaoSession: Manage all available DAO objects of a specific mode, you can use one of the getter methods to get the object. DaoSession also provides some general persistence methods, such as entity insertion, loading, updating, refreshing and deleting.
- XXXDao: Data Access Object (DAO) persists and queries entities. For each entity, greenDAO generates DAO. It has more persistence methods than DaoSession.
- Entities: Persistent objects. Usually, the entity object represents a database row using standard Java attributes (such as a POJO or JavaBean).
2. How to use GreenDao
1. Import Gradle plug-in and Dao code generation
To use GreenDao in an Android project, you need to add GreenDao Gradle plugin and add GreenDao library:
a). Import plugin
// Add in the build.gradle file of the Project:
buildscript {
repositories {
jcenter()
mavenCentral() // add repository
}
dependencies {
classpath ‘com.android.tools.build:gradle:3.1.2‘
classpath ‘org.greenrobot:greendao-gradle-plugin:3.2.2‘ // add plugin
}
}
b). 配置相关依赖
// 在 Moudle:app的 build.gradle 文件中添加:
apply plugin: ‘com.android.application‘
apply plugin: ‘org.greenrobot.greendao‘ // apply plugin
dependencies {
implementation ‘org.greenrobot:greendao:3.2.2‘ // add library
}
c). 配置数据库相关信息
greendao {
schemaVersion 1 //数据库版本号
daoPackage ‘com.renhui.testapp.functions.database.greenDao.db‘
// 设置DaoMaster、DaoSession、Dao 包名
targetGenDir ‘src.main.java‘//设置DaoMaster、DaoSession、Dao目录,请注意,这里路径用.不要用/
generateTests false //设置为true以自动生成单元测试。
targetGenDirTests ‘src/main/java‘ //应存储生成的单元测试的基本目录。默认为 src / androidTest / java。
}
配置完成,在Android Studio中使用Build> Make Project,重写build项目,GreenDao集成完成!
2. 创建存储对象实体类
使用GreenDao存储数据只需要在存储数据类前面声明@Entity注解就让GreenDao为其生成必要的代码:
@Entity
public class Student {
@Id(autoincrement = true)
Long id;
@Unique
int studentNo;//学号
int age; //年龄
String telPhone;//手机号
String sex; //性别
String name;//姓名
String address;//家庭住址
String schoolName;//学校名字
String grade;//几年级
……getter and setter and constructor method……
}
3. GreenDao初始化
我们可以在Application中维持一个全局的会话。我们在Applicaiton进行数据库的初始化操作:=
/**
* 初始化GreenDao,直接在Application中进行初始化操作
*/
private void initGreenDao() {
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "aserbao.db");
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
}
private DaoSession daoSession;
public DaoSession getDaoSession() {
return daoSession;
}
初始化完成之后重新rebuild一下项目会发现在设置的targetGenDir的目录生成三个类文件,这个是GreenDao自动生成的!说明数据库已经连接好了,咱们接下来只需要进行数据库的增删改查操作就行了。
4. 使用GreenDao实现增删改查
1. 增
insert() 插入数据
@Override
public void insertData(Thing s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
for (int i = 0; i < 1000; i++) {
Student student = new Student();
student.setStudentNo(i);
int age = mRandom.nextInt(10) + 10;
student.setAge(age);
student.setTelPhone(RandomValue.getTel());
String chineseName = RandomValue.getChineseName();
student.setName(chineseName);
if (i % 2 == 0) {
student.setSex("男");
} else {
student.setSex("女");
}
student.setAddress(RandomValue.getRoad());
student.setGrade(String.valueOf(age % 10) + "年纪");
student.setSchoolName(RandomValue.getSchoolName());
daoSession.insert(student);
}
}
insertOrReplace()数据存在则替换,数据不存在则插入
@Override
public void insertData(Thing s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
for (int i = 0; i < 1000; i++) {
Student student = new Student();
student.setStudentNo(i);
int age = mRandom.nextInt(10) + 10;
student.setAge(age);
student.setTelPhone(RandomValue.getTel());
String chineseName = RandomValue.getChineseName();
student.setName(chineseName);
if (i % 2 == 0) {
student.setSex("男");
} else {
student.setSex("女");
}
student.setAddress(RandomValue.getRoad());
student.setGrade(String.valueOf(age % 10) + "年纪");
student.setSchoolName(RandomValue.getSchoolName());
daoSession.insertOrReplace(student);//插入或替换
}
}
2. 删
删除有两种方式:delete()和deleteAll();分别表示删除单个和删除所有。
@Override
public void deleteData(Student s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.delete(s);
}
@Override
public void deleteAll() {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.deleteAll(Student.class);
}
3. 改
通过update来进行修改:
@Override
public void updataData(Student s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.update(s);
}
4. 查
查询的方法有:
- loadAll():查询所有数据。
- queryRaw():根据条件查询。
- queryBuilder() : 方便查询的创建,后面详细讲解。
public List queryAll() {
Liststudents = daoSession.loadAll(Student.class);
return students;
}
@Override
public void queryData(String s) {
Liststudents = daoSession.queryRaw(Student.class, " where id = ?", s);
mDataBaseAdapter.addNewStudentData(students);
}
参考资料: https://www.jianshu.com/p/53083f782ea2
GreenDao的核心类
GreenDao的核心类有三个:分别是DaoMaster, DaoSession, XXXDao,这三个类都会自动创建,无需自己编写创建!
- DaoMaster::DaoMaster保存数据库对象并管理特定模式的DAO类。它有静态方法来创建表或删除它们。它的内部类OpenHelper和DevOpenHelper是SQLiteOpenHelper实现,它们在SQLite数据库中创建模式。
- DaoSession:管理特定模式的所有可用DAO对象,您可以使用其中一个getter方法获取该对象。DaoSession还提供了一些通用的持久性方法,如实体的插入,加载,更新,刷新和删除。
- XXXDao:数据访问对象(DAO)持久存在并查询实体。对于每个实体,greenDAO生成DAO。它具有比DaoSession更多的持久性方法。
- Entities :可持久化对象。通常, 实体对象代表一个数据库行使用标准 Java 属性(如一个POJO 或 JavaBean )。
二、GreenDao使用方法
1. 导入Gradle插件和Dao代码生成
要在Android项目中使用GreenDao,您需要添加GreenDao Gradle插件并添加GreenDao库:
a). 导入插件
// 在 Project的build.gradle 文件中添加:
buildscript {
repositories {
jcenter()
mavenCentral() // add repository
}
dependencies {
classpath ‘com.android.tools.build:gradle:3.1.2‘
classpath ‘org.greenrobot:greendao-gradle-plugin:3.2.2‘ // add plugin
}
}
b). 配置相关依赖
// 在 Moudle:app的 build.gradle 文件中添加:
apply plugin: ‘com.android.application‘
apply plugin: ‘org.greenrobot.greendao‘ // apply plugin
dependencies {
implementation ‘org.greenrobot:greendao:3.2.2‘ // add library
}
c). 配置数据库相关信息
greendao {
schemaVersion 1 //数据库版本号
daoPackage ‘com.renhui.testapp.functions.database.greenDao.db‘
// 设置DaoMaster、DaoSession、Dao 包名
targetGenDir ‘src.main.java‘//设置DaoMaster、DaoSession、Dao目录,请注意,这里路径用.不要用/
generateTests false //设置为true以自动生成单元测试。
targetGenDirTests ‘src/main/java‘ //应存储生成的单元测试的基本目录。默认为 src / androidTest / java。
}
配置完成,在Android Studio中使用Build> Make Project,重写build项目,GreenDao集成完成!
2. 创建存储对象实体类
使用GreenDao存储数据只需要在存储数据类前面声明@Entity注解就让GreenDao为其生成必要的代码:
@Entity
public class Student {
@Id(autoincrement = true)
Long id;
@Unique
int studentNo;//学号
int age; //年龄
String telPhone;//手机号
String sex; //性别
String name;//姓名
String address;//家庭住址
String schoolName;//学校名字
String grade;//几年级
……getter and setter and constructor method……
}
3. GreenDao初始化
我们可以在Application中维持一个全局的会话。我们在Applicaiton进行数据库的初始化操作:=
/**
* 初始化GreenDao,直接在Application中进行初始化操作
*/
private void initGreenDao() {
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "aserbao.db");
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
}
private DaoSession daoSession;
public DaoSession getDaoSession() {
return daoSession;
}
初始化完成之后重新rebuild一下项目会发现在设置的targetGenDir的目录生成三个类文件,这个是GreenDao自动生成的!说明数据库已经连接好了,咱们接下来只需要进行数据库的增删改查操作就行了。
4. 使用GreenDao实现增删改查
1. 增
insert() 插入数据
@Override
public void insertData(Thing s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
for (int i = 0; i < 1000; i++) {
Student student = new Student();
student.setStudentNo(i);
int age = mRandom.nextInt(10) + 10;
student.setAge(age);
student.setTelPhone(RandomValue.getTel());
String chineseName = RandomValue.getChineseName();
student.setName(chineseName);
if (i % 2 == 0) {
student.setSex("男");
} else {
student.setSex("女");
}
student.setAddress(RandomValue.getRoad());
student.setGrade(String.valueOf(age % 10) + "年纪");
student.setSchoolName(RandomValue.getSchoolName());
daoSession.insert(student);
}
}
insertOrReplace()数据存在则替换,数据不存在则插入
@Override
public void insertData(Thing s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
for (int i = 0; i < 1000; i++) {
Student student = new Student();
student.setStudentNo(i);
int age = mRandom.nextInt(10) + 10;
student.setAge(age);
student.setTelPhone(RandomValue.getTel());
String chineseName = RandomValue.getChineseName();
student.setName(chineseName);
if (i % 2 == 0) {
student.setSex("男");
} else {
student.setSex("女");
}
student.setAddress(RandomValue.getRoad());
student.setGrade(String.valueOf(age % 10) + "年纪");
student.setSchoolName(RandomValue.getSchoolName());
daoSession.insertOrReplace(student);//插入或替换
}
}
2. 删
删除有两种方式:delete()和deleteAll();分别表示删除单个和删除所有。
@Override
public void deleteData(Student s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.delete(s);
}
@Override
public void deleteAll() {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.deleteAll(Student.class);
}
3. 改
通过update来进行修改:
@Override
public void updataData(Student s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.update(s);
}
4. 查
查询的方法有:
- loadAll():查询所有数据。
- queryRaw():根据条件查询。
- queryBuilder() : 方便查询的创建,后面详细讲解。
public List queryAll() {
Liststudents = daoSession.loadAll(Student.class);
return students;
}
@Override
public void queryData(String s) {
Liststudents = daoSession.queryRaw(Student.class, " where id = ?", s);
mDataBaseAdapter.addNewStudentData(students);
}
参考资料: https://www.jianshu.com/p/53083f782ea2
GreenDao的核心类
GreenDao的核心类有三个:分别是DaoMaster, DaoSession, XXXDao,这三个类都会自动创建,无需自己编写创建!
- DaoMaster::DaoMaster保存数据库对象并管理特定模式的DAO类。它有静态方法来创建表或删除它们。它的内部类OpenHelper和DevOpenHelper是SQLiteOpenHelper实现,它们在SQLite数据库中创建模式。
- DaoSession:管理特定模式的所有可用DAO对象,您可以使用其中一个getter方法获取该对象。DaoSession还提供了一些通用的持久性方法,如实体的插入,加载,更新,刷新和删除。
- XXXDao:数据访问对象(DAO)持久存在并查询实体。对于每个实体,greenDAO生成DAO。它具有比DaoSession更多的持久性方法。
- Entities :可持久化对象。通常, 实体对象代表一个数据库行使用标准 Java 属性(如一个POJO 或 JavaBean )。
二、GreenDao使用方法
1. 导入Gradle插件和Dao代码生成
要在Android项目中使用GreenDao,您需要添加GreenDao Gradle插件并添加GreenDao库:
a). 导入插件
// 在 Project的build.gradle 文件中添加:
buildscript {
repositories {
jcenter()
mavenCentral() // add repository
}
dependencies {
classpath ‘com.android.tools.build:gradle:3.1.2‘
classpath ‘org.greenrobot:greendao-gradle-plugin:3.2.2‘ // add plugin
}
}
b). 配置相关依赖
// 在 Moudle:app的 build.gradle 文件中添加:
apply plugin: ‘com.android.application‘
apply plugin: ‘org.greenrobot.greendao‘ // apply plugin
dependencies {
implementation ‘org.greenrobot:greendao:3.2.2‘ // add library
}
c). 配置数据库相关信息
greendao {
schemaVersion 1 //数据库版本号
daoPackage ‘com.renhui.testapp.functions.database.greenDao.db‘
// 设置DaoMaster、DaoSession、Dao 包名
targetGenDir ‘src.main.java‘//设置DaoMaster、DaoSession、Dao目录,请注意,这里路径用.不要用/
generateTests false //设置为true以自动生成单元测试。
targetGenDirTests ‘src/main/java‘ //应存储生成的单元测试的基本目录。默认为 src / androidTest / java。
}
配置完成,在Android Studio中使用Build> Make Project,重写build项目,GreenDao集成完成!
2. 创建存储对象实体类
使用GreenDao存储数据只需要在存储数据类前面声明@Entity注解就让GreenDao为其生成必要的代码:
@Entity
public class Student {
@Id(autoincrement = true)
Long id;
@Unique
int studentNo;//学号
int age; //年龄
String telPhone;//手机号
String sex; //性别
String name;//姓名
String address;//家庭住址
String schoolName;//学校名字
String grade;//几年级
……getter and setter and constructor method……
}
3. GreenDao初始化
我们可以在Application中维持一个全局的会话。我们在Applicaiton进行数据库的初始化操作:=
/**
* 初始化GreenDao,直接在Application中进行初始化操作
*/
private void initGreenDao() {
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "aserbao.db");
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
}
private DaoSession daoSession;
public DaoSession getDaoSession() {
return daoSession;
}
初始化完成之后重新rebuild一下项目会发现在设置的targetGenDir的目录生成三个类文件,这个是GreenDao自动生成的!说明数据库已经连接好了,咱们接下来只需要进行数据库的增删改查操作就行了。
4. 使用GreenDao实现增删改查
1. 增
insert() 插入数据
@Override
public void insertData(Thing s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
for (int i = 0; i < 1000; i++) {
Student student = new Student();
student.setStudentNo(i);
int age = mRandom.nextInt(10) + 10;
student.setAge(age);
student.setTelPhone(RandomValue.getTel());
String chineseName = RandomValue.getChineseName();
student.setName(chineseName);
if (i % 2 == 0) {
student.setSex("男");
} else {
student.setSex("女");
}
student.setAddress(RandomValue.getRoad());
student.setGrade(String.valueOf(age % 10) + "年纪");
student.setSchoolName(RandomValue.getSchoolName());
daoSession.insert(student);
}
}
insertOrReplace()数据存在则替换,数据不存在则插入
@Override
public void insertData(Thing s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
for (int i = 0; i < 1000; i++) {
Student student = new Student();
student.setStudentNo(i);
int age = mRandom.nextInt(10) + 10;
student.setAge(age);
student.setTelPhone(RandomValue.getTel());
String chineseName = RandomValue.getChineseName();
student.setName(chineseName);
if (i % 2 == 0) {
student.setSex("男");
} else {
student.setSex("女");
}
student.setAddress(RandomValue.getRoad());
student.setGrade(String.valueOf(age % 10) + "年纪");
student.setSchoolName(RandomValue.getSchoolName());
daoSession.insertOrReplace(student);//插入或替换
}
}
2. 删
删除有两种方式:delete()和deleteAll();分别表示删除单个和删除所有。
@Override
public void deleteData(Student s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.delete(s);
}
@Override
public void deleteAll() {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.deleteAll(Student.class);
}
3. 改
通过update来进行修改:
@Override
public void updataData(Student s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.update(s);
}
4. 查
查询的方法有:
- loadAll():查询所有数据。
- queryRaw():根据条件查询。
- queryBuilder() : 方便查询的创建,后面详细讲解。
public List queryAll() {
Liststudents = daoSession.loadAll(Student.class);
return students;
}
@Override
public void queryData(String s) {
Liststudents = daoSession.queryRaw(Student.class, " where id = ?", s);
mDataBaseAdapter.addNewStudentData(students);
}
参考资料: https://www.jianshu.com/p/53083f782ea2
a). 导入插件
// 在 Project的build.gradle 文件中添加:
buildscript {
repositories {
jcenter()
mavenCentral() // add repository
}
dependencies {
classpath ‘com.android.tools.build:gradle:3.1.2‘
classpath ‘org.greenrobot:greendao-gradle-plugin:3.2.2‘ // add plugin
}
}
b). 配置相关依赖
// 在 Moudle:app的 build.gradle 文件中添加:
apply plugin: ‘com.android.application‘
apply plugin: ‘org.greenrobot.greendao‘ // apply plugin
dependencies {
implementation ‘org.greenrobot:greendao:3.2.2‘ // add library
}
c). 配置数据库相关信息
greendao {
schemaVersion 1 //数据库版本号
daoPackage ‘com.renhui.testapp.functions.database.greenDao.db‘
// 设置DaoMaster、DaoSession、Dao 包名
targetGenDir ‘src.main.java‘//设置DaoMaster、DaoSession、Dao目录,请注意,这里路径用.不要用/
generateTests false //设置为true以自动生成单元测试。
targetGenDirTests ‘src/main/java‘ //应存储生成的单元测试的基本目录。默认为 src / androidTest / java。
}
配置完成,在Android Studio中使用Build> Make Project,重写build项目,GreenDao集成完成!
2. 创建存储对象实体类
使用GreenDao存储数据只需要在存储数据类前面声明@Entity注解就让GreenDao为其生成必要的代码:
@Entity
public class Student {
@Id(autoincrement = true)
Long id;
@Unique
int studentNo;//学号
int age; //年龄
String telPhone;//手机号
String sex; //性别
String name;//姓名
String address;//家庭住址
String schoolName;//学校名字
String grade;//几年级
……getter and setter and constructor method……
}
3. GreenDao初始化
我们可以在Application中维持一个全局的会话。我们在Applicaiton进行数据库的初始化操作:=
/**
* 初始化GreenDao,直接在Application中进行初始化操作
*/
private void initGreenDao() {
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "aserbao.db");
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
}
private DaoSession daoSession;
public DaoSession getDaoSession() {
return daoSession;
}
初始化完成之后重新rebuild一下项目会发现在设置的targetGenDir的目录生成三个类文件,这个是GreenDao自动生成的!说明数据库已经连接好了,咱们接下来只需要进行数据库的增删改查操作就行了。
4. 使用GreenDao实现增删改查
1. 增
insert() 插入数据
@Override
public void insertData(Thing s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
for (int i = 0; i < 1000; i++) {
Student student = new Student();
student.setStudentNo(i);
int age = mRandom.nextInt(10) + 10;
student.setAge(age);
student.setTelPhone(RandomValue.getTel());
String chineseName = RandomValue.getChineseName();
student.setName(chineseName);
if (i % 2 == 0) {
student.setSex("男");
} else {
student.setSex("女");
}
student.setAddress(RandomValue.getRoad());
student.setGrade(String.valueOf(age % 10) + "年纪");
student.setSchoolName(RandomValue.getSchoolName());
daoSession.insert(student);
}
}
insertOrReplace()数据存在则替换,数据不存在则插入
@Override
public void insertData(Thing s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
for (int i = 0; i < 1000; i++) {
Student student = new Student();
student.setStudentNo(i);
int age = mRandom.nextInt(10) + 10;
student.setAge(age);
student.setTelPhone(RandomValue.getTel());
String chineseName = RandomValue.getChineseName();
student.setName(chineseName);
if (i % 2 == 0) {
student.setSex("男");
} else {
student.setSex("女");
}
student.setAddress(RandomValue.getRoad());
student.setGrade(String.valueOf(age % 10) + "年纪");
student.setSchoolName(RandomValue.getSchoolName());
daoSession.insertOrReplace(student);//插入或替换
}
}
2. 删
删除有两种方式:delete()和deleteAll();分别表示删除单个和删除所有。
@Override
public void deleteData(Student s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.delete(s);
}
@Override
public void deleteAll() {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.deleteAll(Student.class);
}
3. 改
通过update来进行修改:
@Override
public void updataData(Student s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.update(s);
}
4. 查
查询的方法有:
- loadAll():查询所有数据。
- queryRaw():根据条件查询。
- queryBuilder() : 方便查询的创建,后面详细讲解。
public List queryAll() {
Liststudents = daoSession.loadAll(Student.class);
return students;
}
@Override
public void queryData(String s) {
Liststudents = daoSession.queryRaw(Student.class, " where id = ?", s);
mDataBaseAdapter.addNewStudentData(students);
}
参考资料: https://www.jianshu.com/p/53083f782ea2
a). 导入插件
// 在 Project的build.gradle 文件中添加:
buildscript {
repositories {
jcenter()
mavenCentral() // add repository
}
dependencies {
classpath ‘com.android.tools.build:gradle:3.1.2‘
classpath ‘org.greenrobot:greendao-gradle-plugin:3.2.2‘ // add plugin
}
}
b). 配置相关依赖
// 在 Moudle:app的 build.gradle 文件中添加:
apply plugin: ‘com.android.application‘
apply plugin: ‘org.greenrobot.greendao‘ // apply plugin
dependencies {
implementation ‘org.greenrobot:greendao:3.2.2‘ // add library
}
c). 配置数据库相关信息
greendao {
schemaVersion 1 //数据库版本号
daoPackage ‘com.renhui.testapp.functions.database.greenDao.db‘
// 设置DaoMaster、DaoSession、Dao 包名
targetGenDir ‘src.main.java‘//设置DaoMaster、DaoSession、Dao目录,请注意,这里路径用.不要用/
generateTests false //设置为true以自动生成单元测试。
targetGenDirTests ‘src/main/java‘ //应存储生成的单元测试的基本目录。默认为 src / androidTest / java。
}
配置完成,在Android Studio中使用Build> Make Project,重写build项目,GreenDao集成完成!
// 在 Project的build.gradle 文件中添加:
buildscript {
repositories {
jcenter()
mavenCentral() // add repository
}
dependencies {
classpath ‘com.android.tools.build:gradle:3.1.2‘
classpath ‘org.greenrobot:greendao-gradle-plugin:3.2.2‘ // add plugin
}
}
// 在 Moudle:app的 build.gradle 文件中添加:
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 ‘com.renhui.testapp.functions.database.greenDao.db‘
// 设置DaoMaster、DaoSession、Dao 包名
targetGenDir ‘src.main.java‘//设置DaoMaster、DaoSession、Dao目录,请注意,这里路径用.不要用/
generateTests false //设置为true以自动生成单元测试。
targetGenDirTests ‘src/main/java‘ //应存储生成的单元测试的基本目录。默认为 src / androidTest / java。
}
2. 创建存储对象实体类
使用GreenDao存储数据只需要在存储数据类前面声明@Entity注解就让GreenDao为其生成必要的代码:
@Entity
public class Student {
@Id(autoincrement = true)
Long id;
@Unique
int studentNo;//学号
int age; //年龄
String telPhone;//手机号
String sex; //性别
String name;//姓名
String address;//家庭住址
String schoolName;//学校名字
String grade;//几年级
……getter and setter and constructor method……
}
3. GreenDao初始化
我们可以在Application中维持一个全局的会话。我们在Applicaiton进行数据库的初始化操作:=
/**
* 初始化GreenDao,直接在Application中进行初始化操作
*/
private void initGreenDao() {
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "aserbao.db");
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
}
private DaoSession daoSession;
public DaoSession getDaoSession() {
return daoSession;
}
初始化完成之后重新rebuild一下项目会发现在设置的targetGenDir的目录生成三个类文件,这个是GreenDao自动生成的!说明数据库已经连接好了,咱们接下来只需要进行数据库的增删改查操作就行了。
4. 使用GreenDao实现增删改查
1. 增
insert() 插入数据
@Override
public void insertData(Thing s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
for (int i = 0; i < 1000; i++) {
Student student = new Student();
student.setStudentNo(i);
int age = mRandom.nextInt(10) + 10;
student.setAge(age);
student.setTelPhone(RandomValue.getTel());
String chineseName = RandomValue.getChineseName();
student.setName(chineseName);
if (i % 2 == 0) {
student.setSex("男");
} else {
student.setSex("女");
}
student.setAddress(RandomValue.getRoad());
student.setGrade(String.valueOf(age % 10) + "年纪");
student.setSchoolName(RandomValue.getSchoolName());
daoSession.insert(student);
}
}
insertOrReplace()数据存在则替换,数据不存在则插入
@Override
public void insertData(Thing s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
for (int i = 0; i < 1000; i++) {
Student student = new Student();
student.setStudentNo(i);
int age = mRandom.nextInt(10) + 10;
student.setAge(age);
student.setTelPhone(RandomValue.getTel());
String chineseName = RandomValue.getChineseName();
student.setName(chineseName);
if (i % 2 == 0) {
student.setSex("男");
} else {
student.setSex("女");
}
student.setAddress(RandomValue.getRoad());
student.setGrade(String.valueOf(age % 10) + "年纪");
student.setSchoolName(RandomValue.getSchoolName());
daoSession.insertOrReplace(student);//插入或替换
}
}
2. 删
删除有两种方式:delete()和deleteAll();分别表示删除单个和删除所有。
@Override
public void deleteData(Student s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.delete(s);
}
@Override
public void deleteAll() {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.deleteAll(Student.class);
}
3. 改
通过update来进行修改:
@Override
public void updataData(Student s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.update(s);
}
4. 查
查询的方法有:
- loadAll():查询所有数据。
- queryRaw():根据条件查询。
- queryBuilder() : 方便查询的创建,后面详细讲解。
public List queryAll() {
Liststudents = daoSession.loadAll(Student.class);
return students;
}
@Override
public void queryData(String s) {
Liststudents = daoSession.queryRaw(Student.class, " where id = ?", s);
mDataBaseAdapter.addNewStudentData(students);
}
2. 创建存储对象实体类
使用GreenDao存储数据只需要在存储数据类前面声明@Entity注解就让GreenDao为其生成必要的代码:
@Entity
public class Student {
@Id(autoincrement = true)
Long id;
@Unique
int studentNo;//学号
int age; //年龄
String telPhone;//手机号
String sex; //性别
String name;//姓名
String address;//家庭住址
String schoolName;//学校名字
String grade;//几年级
……getter and setter and constructor method……
}
3. GreenDao初始化
我们可以在Application中维持一个全局的会话。我们在Applicaiton进行数据库的初始化操作:=
/**
* 初始化GreenDao,直接在Application中进行初始化操作
*/
private void initGreenDao() {
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "aserbao.db");
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
}
private DaoSession daoSession;
public DaoSession getDaoSession() {
return daoSession;
}
初始化完成之后重新rebuild一下项目会发现在设置的targetGenDir的目录生成三个类文件,这个是GreenDao自动生成的!说明数据库已经连接好了,咱们接下来只需要进行数据库的增删改查操作就行了。
@Entity
public class Student {
@Id(autoincrement = true)
Long id;
@Unique
int studentNo;//学号
int age; //年龄
String telPhone;//手机号
String sex; //性别
String name;//姓名
String address;//家庭住址
String schoolName;//学校名字
String grade;//几年级
……getter and setter and constructor method……
}
/**
* 初始化GreenDao,直接在Application中进行初始化操作
*/
private void initGreenDao() {
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "aserbao.db");
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
}
private DaoSession daoSession;
public DaoSession getDaoSession() {
return daoSession;
}
4. 使用GreenDao实现增删改查
1. 增
insert() 插入数据
@Override
public void insertData(Thing s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
for (int i = 0; i < 1000; i++) {
Student student = new Student();
student.setStudentNo(i);
int age = mRandom.nextInt(10) + 10;
student.setAge(age);
student.setTelPhone(RandomValue.getTel());
String chineseName = RandomValue.getChineseName();
student.setName(chineseName);
if (i % 2 == 0) {
student.setSex("男");
} else {
student.setSex("女");
}
student.setAddress(RandomValue.getRoad());
student.setGrade(String.valueOf(age % 10) + "年纪");
student.setSchoolName(RandomValue.getSchoolName());
daoSession.insert(student);
}
}
insertOrReplace()数据存在则替换,数据不存在则插入
@Override
public void insertData(Thing s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
for (int i = 0; i < 1000; i++) {
Student student = new Student();
student.setStudentNo(i);
int age = mRandom.nextInt(10) + 10;
student.setAge(age);
student.setTelPhone(RandomValue.getTel());
String chineseName = RandomValue.getChineseName();
student.setName(chineseName);
if (i % 2 == 0) {
student.setSex("男");
} else {
student.setSex("女");
}
student.setAddress(RandomValue.getRoad());
student.setGrade(String.valueOf(age % 10) + "年纪");
student.setSchoolName(RandomValue.getSchoolName());
daoSession.insertOrReplace(student);//插入或替换
}
}
2. 删
删除有两种方式:delete()和deleteAll();分别表示删除单个和删除所有。
@Override
public void deleteData(Student s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.delete(s);
}
@Override
public void deleteAll() {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.deleteAll(Student.class);
}
3. 改
通过update来进行修改:
@Override
public void updataData(Student s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.update(s);
}
4. 查
查询的方法有:
- loadAll():查询所有数据。
- queryRaw():根据条件查询。
- queryBuilder() : 方便查询的创建,后面详细讲解。
public List queryAll() {
Liststudents = daoSession.loadAll(Student.class);
return students;
}
@Override
public void queryData(String s) {
Liststudents = daoSession.queryRaw(Student.class, " where id = ?", s);
mDataBaseAdapter.addNewStudentData(students);
}
@Override
public void insertData(Thing s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
for (int i = 0; i < 1000; i++) {
Student student = new Student();
student.setStudentNo(i);
int age = mRandom.nextInt(10) + 10;
student.setAge(age);
student.setTelPhone(RandomValue.getTel());
String chineseName = RandomValue.getChineseName();
student.setName(chineseName);
if (i % 2 == 0) {
student.setSex("男");
} else {
student.setSex("女");
}
student.setAddress(RandomValue.getRoad());
student.setGrade(String.valueOf(age % 10) + "年纪");
student.setSchoolName(RandomValue.getSchoolName());
daoSession.insert(student);
}
}
@Override
public void insertData(Thing s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
for (int i = 0; i < 1000; i++) {
Student student = new Student();
student.setStudentNo(i);
int age = mRandom.nextInt(10) + 10;
student.setAge(age);
student.setTelPhone(RandomValue.getTel());
String chineseName = RandomValue.getChineseName();
student.setName(chineseName);
if (i % 2 == 0) {
student.setSex("男");
} else {
student.setSex("女");
}
student.setAddress(RandomValue.getRoad());
student.setGrade(String.valueOf(age % 10) + "年纪");
student.setSchoolName(RandomValue.getSchoolName());
daoSession.insertOrReplace(student);//插入或替换
}
}
@Override
public void deleteData(Student s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.delete(s);
}
@Override
public void deleteAll() {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.deleteAll(Student.class);
}
@Override
public void updataData(Student s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.update(s);
}
public List queryAll() {
Liststudents = daoSession.loadAll(Student.class);
return students;
}
@Override
public void queryData(String s) {
Liststudents = daoSession.queryRaw(Student.class, " where id = ?", s);
mDataBaseAdapter.addNewStudentData(students);
}