美文网首页
jetpack系列之room

jetpack系列之room

作者: gogoingmonkey | 来源:发表于2021-08-13 16:02 被阅读0次

    前言

    个人愚见,Room数据库一般适合整套项目都用的项目。如果老项目没用,里面的一些注解使用API使用时间久了容易忘记,使用到在看!

    1.实现细节

    1.首先从编译后的包可以看到,里面使用了contentprovider来初始化:

    public class ProcessLifecycleOwnerInitializer extends ContentProvider {
        @Override
        public boolean onCreate() {
            LifecycleDispatcher.init(getContext());
            ProcessLifecycleOwner.init(getContext());
            return true;
        }
    
    

    2.大量使用注解定义字段相关的逻辑,编译后,在APT目录下,文件还是一些常规的数据库操作逻辑,生成如下文件


    image.png

    2.简单实用

    1.创建database
    @Database(entities = {Student.class,Address.class},version = 1,exportSchema = false)
    public abstract class AppDatabase extends RoomDatabase {
        public abstract StudentDao userDao();
    }
    
    2.创建对象类

    注解里面定义

    @Entity(foreignKeys = @ForeignKey(entity = Address.class,parentColumns = "addressId",childColumns = "addressId"))
    public class Student {
        @PrimaryKey(autoGenerate = true)
        private int uid;
    
        public int getUid() {
            return uid;
        }
    
        public void setUid(int uid) {
            this.uid = uid;
        }
    
        @ColumnInfo(name = "name")
        private String name;
        @ColumnInfo(name="pwd")
        private String password;
        @ColumnInfo(name="addressId")
        private int addressId;
    
        @Override
        public String toString() {
            return "Student{" +
                    "id=" + uid +
                    ", name='" + name + '\'' +
                    ", password='" + password + '\'' +
                    ", addressId=" + addressId +
                    '}';
        }
    
    
    
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public int getAddressId() {
            return addressId;
        }
    
        public void setAddressId(int addressId) {
            this.addressId = addressId;
        }
    
        public Student( String name, String password, int addressId) {
            this.name = name;
            this.password = password;
            this.addressId = addressId;
        }
    }
    
    3.创建Dao
    @Dao
    public interface StudentDao {
        @Insert
        void insert(Student... students);
    
        @Delete
        void delete(Student student);
    
        @Update
        void update(Student student);
    
        @Query("select * from Student")
        List<Student> getAll();
    
        //查询一条记录
        @Query("select * from Student where name like :name")
        Student findByName(String name);
    
        @Query("select * from Student where uid in(:userIds)")
        List<Student> getAllId(int[] userIds);
    
        @Query("select name,pwd from Student")
        public List<StudentTuple> getRecord();
    
    //    @Query("select x,x,x from where student.x==address.x")
    
    }
    
    
    4. 测试代码
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //数据库的操作应该是在子线程
            DbTest t=new DbTest();
            t.start();
        }
    
        public class DbTest extends Thread{
            @Override
            public void run() {
                //数据库的操作都在这里进行
                AppDatabase jettDB= Room.databaseBuilder(getApplicationContext()
                                ,AppDatabase.class
                                ,"XXXDB").build();
                StudentDao dao=XXXDB.userDao();
                dao.insert(new Student("jett","123",1));
                dao.insert(new Student("jett1","123",2));
                dao.insert(new Student("jett2","123",3));
                dao.insert(new Student("jett3","123",4));
    
                List<Student> list=dao.getAll();
                Log.i("jett",list.toString());
    
                Student jett2=dao.findByName("jett3");
                Log.i("jett",jett2.toString());
                List<Student> allId=dao.getAllId(new int[]{2,3,4});
                Log.i("jett",allId.toString());
    
                List<StudentTuple> record=dao.getRecord();
                Log.i("jett",record.toString());
            }
        }
    
    }
    
    2.创建database
    
    
    2.创建database
    @Entity(foreignKeys = @ForeignKey(entity = Address.class,parentColumns = "addressId",childColumns = "addressId"))
    public class Student {
        @PrimaryKey(autoGenerate = true)
        private int uid;
    
        public int getUid() {
            return uid;
        }
    
        public void setUid(int uid) {
            this.uid = uid;
        }
    
        @ColumnInfo(name = "name")
        private String name;
        @ColumnInfo(name="pwd")
        private String password;
        @ColumnInfo(name="addressId")
        private int addressId;
    
        @Override
        public String toString() {
            return "Student{" +
                    "id=" + uid +
                    ", name='" + name + '\'' +
                    ", password='" + password + '\'' +
                    ", addressId=" + addressId +
                    '}';
        }
    
    
    
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public int getAddressId() {
            return addressId;
        }
    
        public void setAddressId(int addressId) {
            this.addressId = addressId;
        }
    
        public Student( String name, String password, int addressId) {
            this.name = name;
            this.password = password;
            this.addressId = addressId;
        }
    }
    

    相关文章

      网友评论

          本文标题:jetpack系列之room

          本文链接:https://www.haomeiwen.com/subject/rwzdbltx.html