目录
Room 的使用三角色介绍:
使用步骤:
使用准备:
使用:?
Room 的使用三角色介绍:
- @Entity(数据库表? 与? 类相似);
- @Dao (操作引擎,用于操作数据库)
- @Database(创建数据库)
使用步骤:
使用准备:
- 导入依赖:(在 bulid.gradle 文件)中导入,(导入的本处版本只是举例,可在官网出查找其他版本)
dependencies {
def room_version = "2.4.2"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
} - 结合Rxjava使用额外添加相关依赖:
dependencies {
...
// optional - RxJava2 support for Room
implementation "androidx.room:room-rxjava2:$room_version"
// optional - RxJava3 support for Room
implementation "androidx.room:room-rxjava3:$room_version"
// optional - Guava support for Room, including Optional and ListenableFuture
implementation "androidx.room:room-guava:$room_version"
// optional - Test helpers
testImplementation "androidx.room:room-testing:$room_version"
// optional - Paging 3 Integration
implementation "androidx.room:room-paging:2.5.0-alpha01"
}
使用:?
- @Entity? 创建 数据库表(与 类对象相似):? ??
package com.example.androidapp.SQlist;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
@Entity(tableName = "Agency")
public class Agency implements Serializable {
@PrimaryKey(autoGenerate = true)
private int id;
private String agency_name;
private String agency_nowTime;
private String agency_futureTime;
private String agency_okTime;
private String agency_type;
private String agency_nowstate;
private String agency_context;
public Agency(String agency_name, String agency_nowTime, String agency_futureTime,
String agency_type, String agency_nowstate, String agency_context) {
this.agency_name = agency_name;
this.agency_nowTime = agency_nowTime;
this.agency_futureTime = agency_futureTime;
this.agency_type = agency_type;
this.agency_nowstate = agency_nowstate;
this.agency_context = agency_context;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAgency_name() {
return agency_name;
}
public void setAgency_name(String agency_name) {
this.agency_name = agency_name;
}
public String getAgency_nowTime() {
return agency_nowTime;
}
public void setAgency_nowTime(String agency_nowTime) {
this.agency_nowTime = agency_nowTime;
}
public String getAgency_futureTime() {
return agency_futureTime;
}
public void setAgency_futureTime(String agency_futureTime) {
this.agency_futureTime = agency_futureTime;
}
public String getAgency_type() {
return agency_type;
}
public void setAgency_type(String agency_type) {
this.agency_type = agency_type;
}
public String getAgency_nowstate() {
return agency_nowstate;
}
public void setAgency_nowstate(String agency_nowstate) {
this.agency_nowstate = agency_nowstate;
}
public String getAgency_context() {
return agency_context;
}
public void setAgency_context(String agency_context) {
this.agency_context = agency_context;
}
public String getAgency_okTime() {
return agency_okTime;
}
public void setAgency_okTime(String agency_okTime) {
this.agency_okTime = agency_okTime;
}
@Override
public String toString() {
return "Agency{" +
"id=" + id +
", agency_name='" + agency_name + '\'' +
", agency_nowTime='" + agency_nowTime + '\'' +
", agency_futureTime='" + agency_futureTime + '\'' +
", agency_type='" + agency_type + '\'' +
", agency_nowstate='" + agency_nowstate + '\'' +
", agency_context='" + agency_context + '\'' +
'}';
}
} - @Dao 操作数据库
package com.example.androidapp.SQlist;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;
import java.util.List;
@Dao
public interface AgencyDao {
@Insert
void insertAgencies(Agency... agencies);
@Update
void updateAgencies(Agency... agencies);
@Delete
void deleteAgencies(Agency... agencies);
@Query("SELECT * FROM Agency ORDER BY ID DESC")
List<Agency> getAllAgency();
} - @Database 创建 数据库 并与 数据表 关联:暴露Dao给用户操作数据库
package com.example.androidapp.SQlist;
import android.content.Context;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
//数据库 关联 之前的表 数据库信息
//,User.class
@Database(entities = {Agency.class},version = 1,exportSchema = false)
public abstract class AgencyDatabase extends RoomDatabase {
// 暴露 dao 给用户操作 数据库
public abstract AgencyDao getAgencyDao();
// 单例模式 返回 db
private static AgencyDatabase INSTANCE;
static synchronized AgencyDatabase getInstance(Context context){
if (INSTANCE == null){
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),AgencyDatabase.class,"Agency_database")
.build();
}
return INSTANCE;
}
}
- 创建 一个 DBclass 用于实现 在异步操作 中进行数据操作(这里我用了AsyncTask进行异步操作)
package com.example.androidapp.SQlist;
import android.content.Context;
import android.os.AsyncTask;
import java.util.ArrayList;
import java.util.List;
public class DBEngine {
// 接收 传入的 AgencyDao 对象
private AgencyDao agencyDao;
// 接收 传入的 UserDao 对象
private UserDao userDao;
// 接收 获取的所有数据 实现单例
private static List<Agency> allAgencies = new ArrayList<>();
private static List<User> allUsers = new ArrayList<>();
// 实现 构造方法
public DBEngine(Context context) {
AgencyDatabase agencyDatabase = AgencyDatabase.getInstance(context);
agencyDao = agencyDatabase.getAgencyDao();
UserDatabase userDatabase = UserDatabase.getInstance(context);
userDao = userDatabase.getUserDao();
}
;
//*********************************************************************************************
// 插入 操作
public void insertAgencies(Agency... agencies) {
// 开启 InsertAsyncTask 线程
new InsertAsyncTask(agencyDao).execute(agencies);
}
// 更新 操作
public void updateAgencies(Agency... agencies) {
new UpdateAsyncTask(agencyDao).execute(agencies);
}
// 删除 操作
public void deleteAgencies(Agency... agencies) {
new DeleteAsyncTask(agencyDao).execute(agencies);
}
// 获取 操作
public void getAllAgencies(Agency... agencies) {
new GetAllAgencies(agencyDao).execute();
}
//*********************************************************************************************
//Agency
// 实现 插入功能
private static class InsertAsyncTask extends AsyncTask<Agency, Void, Void> {
// 获取 AgencyDao 对象
private AgencyDao dao;
// 实现 构造方法
public InsertAsyncTask(AgencyDao agencyDao) {
dao = agencyDao;
}
// 实现 AgencyDao 操作
@Override
protected Void doInBackground(Agency... agencies) {
dao.insertAgencies(agencies);
return null;
}
}
// 实现 更新功能
private static class UpdateAsyncTask extends AsyncTask<Agency, Void, Void> {
private AgencyDao dao;
public UpdateAsyncTask(AgencyDao agencyDao) {
dao = agencyDao;
}
@Override
protected Void doInBackground(Agency... agencies) {
dao.updateAgencies(agencies);
return null;
}
}
// 实现 删除功能
private static class DeleteAsyncTask extends AsyncTask<Agency, Void, Void> {
private AgencyDao dao;
public DeleteAsyncTask(AgencyDao agencyDao) {
dao = agencyDao;
}
@Override
protected Void doInBackground(Agency... agencies) {
dao.deleteAgencies(agencies);
return null;
}
}
// 实现 获取所有数据 功能
private static class GetAllAgencies extends AsyncTask<Void, Void, List<Agency>> {
private AgencyDao dao;
public GetAllAgencies(AgencyDao agencyDao) {
dao = agencyDao;
}
// 获取数据
@Override
protected List<Agency> doInBackground(Void... voids) {
allAgencies = dao.getAllAgency();
return allAgencies;
}
// 返回 数据
@Override
protected void onPostExecute(List<Agency> agencies) {
super.onPostExecute(agencies);
allAgencies = agencies;
}
} - 在主类 中 实例化 一个DB?对象,都数据进行操作
// 获取 数据库引擎
private DBEngine dbEngine;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbEngine = new DBEngine(this);
dbEngine.getAllAgencies();
// 实例化 一个 Agency 对象
Agency new_agency = new Agency(name, nowTimeStr, futureTimeStr, typeStr, stateStr, contextStr);
// 插入 操作
dbEngine.insertAgencies(new_agency );
// 更新 操作
dbEngine.updateAgencies(new_agency );
// 删除 操作
dbEngine.deleteAgencies(new_agency );
// 获取 操作
dbEngine.getAllAgencies(new_agency );
}
? ? ? ? 项目案例:
? ? ? ? 苏苏待办:
? ? ? ? github:????????GitHub - susihao/Agency: 苏苏待办
|