Android room使用教程

  • Post author:
  • Post category:其他


demo:


https://github.com/anymyna/android-example-ant

步骤:

1、 初始化


dependencies {
    def room_version = "2.2.5"
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor  "androidx.room:room-compiler:$room_version"
}

2、 用法

2.1 application 中初始化


        db = Room.databaseBuilder(getApplicationContext(),
                AppDatabase.class, "database-name")
                .allowMainThreadQueries()
                .build();

2.2、创建实体类User


    @Entity
    public class User {
    @PrimaryKey
    public int uid;

    @ColumnInfo(name = "first_name")
    public String firstName;

    @ColumnInfo(name = "last_name")
    public String lastName;
}

2.3 创建数据访问接口

@Dao
public interface UserDao {
    @Query("SELECT * FROM user")
    List<User> getAll();

    @Query("SELECT * FROM user WHERE uid=:id")
    User getUser(int id);

    @Query("SELECT * FROM user WHERE first_name LIKE :first AND " +
            "last_name LIKE :last LIMIT 1")
    User findByName(String first, String last);

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertAll(User... users);

    @Delete
    void delete(User user);

    @Update
    int update(User... users);
}

2.4 创建继承RoomDatabase抽象类的抽象类


       @Database(entities = {User.class}, version = 1)
	public abstract class AppDatabase extends RoomDatabase {
    public abstract UserDao userDao();
}

2.5 进行增删改查


               // 插入
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                User user = new User();
                user.firstName = "ant";
                user.lastName = "ant";
                user.uid = 1;
                userDao.insertAll(user);
                Log.d(TAG,"insertAll user uid: " + user.uid);
            }
        });


        // 查询单个
        findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                User user = userDao.findByName("ant","ant");
//                Log.d(TAG,"search  uid: " + user.uid);
                User user = userDao.getUser(1);
                Log.d(TAG,"search  firstName: " + user.firstName);
            }
        });

        // 删除
        findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                User user = userDao.findByName("ant","ant");
                userDao.delete(user);
                Log.d(TAG,"delete user uid: " + user.uid);
            }
        });

        // 查询所有
        findViewById(R.id.button4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<User> list = userDao.getAll();
                Log.d(TAG,"list size: " + list.size());
            }
        });

        // 更新
        findViewById(R.id.button5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                User user = new User();
                user.firstName = "bee";
                user.lastName = "bee";
                user.uid = 1;
                userDao.update(user);
                Log.d(TAG,"update uid: " + user.uid);
                
            }
        });

参考:


使用 Room 将数据保存到本地数据库



版权声明:本文为anymyna原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。