架构图
Dao 层 操作 数据库
dataBase层操作 是一个抽象类或者是一个接口 提供 dao实例 和 dataBase 实例 需要基础 RoomDataBase 执行一下 数据库的一下操作
viewModel 层 提供dataBase
使用viewmodel层提供的database 去修改操作视图
实体类
package com.example.jetpack.Pojo;
/*
createName : 程序员keven
createTime : 2022/3/7
*/
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;
@Entity(tableName = "user")
public class User {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(typeAffinity = ColumnInfo.INTEGER , name = "id")
public int id ;
@ColumnInfo(typeAffinity = ColumnInfo.TEXT , name = "name")
public String name ;
@ColumnInfo(typeAffinity = ColumnInfo.INTEGER , name = "age")
public int age ;
public User(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@Ignore
public User(String name, int age) {
this.name = name;
this.age = age;
}
}
Dao层
package com.example.jetpack.Dao;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import com.example.jetpack.Pojo.User;
import java.util.List;
/**
* 操作数据库的 Dao层
*/
@Dao
public interface UserDao {
@Insert
void insert(User... user);
@Delete
public void delete(User user);
@Query("select * from user ")
public List<User> queryAll();
}
dataBase层
package com.example.jetpack.DataBase;
import android.content.Context;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;
import com.example.jetpack.Dao.UserDao;
import com.example.jetpack.Pojo.User;
/*
createName : 程序员keven
createTime : 2022/3/7
*/
@Database(entities = {User.class} ,version = 1 , exportSchema = false)
public abstract class DataBase extends RoomDatabase {
private static DataBase database = null;
public abstract UserDao getUserDao();
static final Migration MIGRATION_2_3 = new Migration(2, 3) {
@Override
public void migrate(SupportSQLiteDatabase database) {
}
};
public static DataBase get(Context context) {
if (database == null){
database = Room.databaseBuilder(context, DataBase.class, "user")
.allowMainThreadQueries()
.build();
}
return database;
}
}
viewModel层
package com.example.jetpack.ViewModel;
import android.content.Context;
import androidx.lifecycle.ViewModel;
import com.example.jetpack.DataBase.DataBase;
/*
createName : 程序员keven
createTime : 2022/3/7
*/
public class UserViewModel extends ViewModel {
public DataBase dataBase = null ;
public DataBase getDataBase(Context context) {
if (dataBase == null){
dataBase = DataBase.get(context);
}
return dataBase;
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加"
/>
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="删除"
/>
</RelativeLayout>
</LinearLayout>
Main
package com.example.jetpack;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.example.jetpack.Dao.UserDao;
import com.example.jetpack.DataBase.DataBase;
import com.example.jetpack.Pojo.User;
import com.example.jetpack.ViewModel.UserViewModel;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private DataBase dataBase;
private UserDao userDao;
private TextView delete;
private TextView insert;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UserViewModel userViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication())).get(UserViewModel.class);
dataBase = userViewModel.getDataBase(getBaseContext());
userDao = dataBase.getUserDao();
delete = findViewById(R.id.delete);
insert = findViewById(R.id.insert);
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
User user = new User( "keven" , 20);
Log.e("TAG," , "onClick: insert " );
userDao.insert(user);
List<User> users = userDao.queryAll();
for (User u: users ) {
Log.e("TAG", "onClick: "+u.name +""+ u.age +""+u.id );
}
}
});
}
}
|