概述
直接贴
def room_version ="2.2.5"
compile"android.arch.persistence.room:runtime:1.1.1"
annotationProcessor"android.arch.persistence.room:compiler:1.1.1"
----------------
新建三个类一个包下
@Dao
public interface UserDao {
@Query("SELECT * FROM user")
ListgetAll();
@Query("SELECT * FROM user WHERE uid IN (:userIds)")
ListloadAllByIds(int[] userIds);
@Query("SELECT * FROM user WHERE first_name LIKE :first AND " +
"last_name LIKE :last LIMIT 1")
UserfindByName(String first, String last);
@Insert
void insertAll(User... users);
@Delete
void delete(User user);
}
----------------------------
@Entity(tableName ="user")
public class User {
@PrimaryKey
public int uid;
@ColumnInfo(name ="first_name")
public StringfirstName;
@ColumnInfo(name ="last_name")
public StringlastName;
public User(int uid) {
this.uid = uid;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public StringgetFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public StringgetLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
----------------
@Database(entities = {User.class}, version =1)
public abstract class AppDatabaseextends RoomDatabase {
public abstract UserDaouserDao();
}
不要忘了 application中
private static final ObjectsLock =new Object();
private static AppDatabaseINSTANCE;
public static AppDatabasegetInstance(Context context) {
synchronized (sLock) {
if (INSTANCE ==null) {
INSTANCE =
Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "user.db")
.build();
}
return INSTANCE;
}
}
声明一下 网上喜欢搞文章的能不能把你们的用法和原理分开写,那么多东西你自以为很牛,你以为总是你以为的,新手想上路都困难,请你们不要给android 新手开发增加难度了,大婶们
这就是 jetpack地址 https://github.com/la149457357/jetpack 欢迎点赞。。谢谢大家
最后
以上就是风中糖豆为你收集整理的android room 简书,Android Room使用的全部内容,希望文章能够帮你解决android room 简书,Android Room使用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复