{"id":303244,"date":"2024-05-20T18:55:15","date_gmt":"2024-05-20T10:55:15","guid":{"rendered":""},"modified":"2024-05-20T18:55:48","modified_gmt":"2024-05-20T10:55:48","slug":"android-%e7%b3%bb%e7%bb%9f%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-room-%e6%9e%b6%e6%9e%84%e7%bb%84%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/303244.html","title":{"rendered":"Android \u7cfb\u7edf\u4e2d\u5982\u4f55\u4f7f\u7528 Room \u67b6\u6784\u7ec4\u4ef6"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26215459\/4d07075e-6a53-4448-aac4-accd50cb6dad.webp\" alt=\"Android \u7cfb\u7edf\u4e2d\u5982\u4f55\u4f7f\u7528 Room \u67b6\u6784\u7ec4\u4ef6\" \/><\/p>\n<p><p>\u5728Android\u7cfb\u7edf\u4e2d\uff0c\u4f7f\u7528Room\u67b6\u6784\u7ec4\u4ef6\u53ef\u4ee5\u6781\u5927\u5730\u7b80\u5316\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u6d41\u7a0b\uff0c<strong>\u63d0\u9ad8\u5e94\u7528\u6027\u80fd<\/strong>\u3001<strong>\u7b80\u5316\u6570\u636e\u5e93\u64cd\u4f5c<\/strong>\u3001<strong>\u786e\u4fdd\u6570\u636e\u5b89\u5168\u6027<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u63d0\u9ad8\u5e94\u7528\u6027\u80fd<\/strong>\u7684\u529f\u6548\u5c24\u4e3a\u7a81\u51fa\u3002Room\u901a\u8fc7\u51cf\u5c11\u5e94\u7528\u7a0b\u5e8f\u8fd0\u884c\u65f6\u7684\u5197\u4f59\u6570\u636e\u5e93\u8bbf\u95ee\uff0c\u6709\u6548\u964d\u4f4e\u4e86\u5e94\u7528\u7684\u5185\u5b58\u6d88\u8017\u548cCPU\u4f7f\u7528\u7387\u3002\u8fd9\u662f\u56e0\u4e3aRoom\u53ef\u4ee5\u7f16\u8bd1\u65f6\u5bf9\u6570\u636e\u5e93\u64cd\u4f5c\u7684SQL\u8bed\u53e5\u8fdb\u884c\u68c0\u67e5\uff0c\u786e\u4fdd\u8fd0\u884c\u65f6\u7684\u6548\u7387\u548c\u5b89\u5168\u6027\uff0c\u4ece\u800c\u6709\u6548\u63d0\u9ad8\u4e86\u5e94\u7528\u7684\u6574\u4f53\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001ROOM\u7684\u57fa\u672c\u4ecb\u7ecd<\/h3>\n<\/p>\n<p><p>Room\u662fAndroid Architecture Components\u7684\u4e00\u90e8\u5206\uff0c\u81f4\u529b\u4e8e\u628aSQLite\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u5c01\u88c5\u6210\u4e00\u4e2a\u66f4\u52a0\u7b80\u6d01\u6613\u7528\u7684\u5e93\u3002\u5728\u4f7f\u7528Room\u4e4b\u524d\uff0c\u5f00\u53d1\u8005\u9700\u8981\u8fdb\u884c\u5927\u91cf\u7684\u6837\u677f\u4ee3\u7801\u7f16\u5199\uff0c\u7528\u4e8e\u521b\u5efaSQLite\u6570\u636e\u5e93\u3001\u8868\u7684\u521b\u5efa\u3001\u6570\u636e\u7684\u67e5\u8be2\u7b49\u3002Room\u7684\u51fa\u73b0\uff0c\u6709\u6548\u5730\u51cf\u5c11\u8fd9\u90e8\u5206\u5de5\u4f5c\u91cf\uff0c\u901a\u8fc7\u6ce8\u89e3\u548c\u7f16\u8bd1\u65f6\u7684SQL\u8bed\u53e5\u68c0\u67e5\uff0c\u4f7f\u5f97\u6570\u636e\u5e93\u64cd\u4f5c\u66f4\u5b89\u5168\u3001\u66f4\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p>Room\u4e3b\u8981\u7531\u4e09\u4e2a\u4e3b\u8981\u7ec4\u4ef6\u6784\u6210\uff1a<strong>Database<\/strong>\u3001<strong>Entity<\/strong>\u3001<strong>DAO<\/strong>\uff08Data Access Object\uff09\u3002<strong>Database<\/strong>\u58f0\u660e\u4e00\u4e2a\u6570\u636e\u5e93\u6301\u6709\u8005\u5e76\u4e14\u4f5c\u4e3a\u5e94\u7528\u6301\u4e45\u5316\u6570\u636e\u7684\u5e95\u5c42\u8fde\u63a5\u7684\u4e3b\u8981\u5165\u53e3\u70b9\u3002<strong>Entity<\/strong>\u4ee3\u8868\u6570\u636e\u5e93\u4e2d\u7684\u4e00\u4e2a\u8868\u3002<strong>DAO<\/strong>\u5305\u542b\u7528\u4e8e\u8bbf\u95ee\u6570\u636e\u5e93\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001ROOM\u7684\u96c6\u6210\u6b65\u9aa4<\/h3>\n<\/p>\n<p><p>\u96c6\u6210Room\u7ec4\u4ef6\u5165\u9879\u76ee\u9996\u5148\u662f\u5728\u5e94\u7528\u7684<code>build.gradle<\/code>\u6587\u4ef6\u4e2d\u52a0\u5165\u4f9d\u8d56\u3002\u8981\u4f7f\u7528Room\uff0c\u786e\u4fdd\u5728\u9879\u76ee\u7684Gradle\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e86Google Maven\u4ed3\u5e93\uff0c\u7136\u540e\u5728\u5e94\u7528\u7684Gradle\u6587\u4ef6\u6dfb\u52a0Room\u8fd0\u884c\u65f6\u548c\u7f16\u8bd1\u65f6\u4f9d\u8d56\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6dfb\u52a0\u4f9d\u8d56<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-gradle\">dependencies {<\/p>\n<p>    def room_version = &quot;2.3.0&quot;<\/p>\n<p>    implementation &quot;androidx.room:room-runtime:$room_version&quot;<\/p>\n<p>    annotationProcessor &quot;androidx.room:room-compiler:$room_version&quot;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u914d\u7f6e\u6570\u636e\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u7ee7\u627f\u81eaRoomDatabase\u7684\u62bd\u8c61\u7c7b\u3002\u5728\u8be5\u7c7b\u4e0a\u58f0\u660e@Entity\u6240\u8868\u793a\u7684\u8868\uff0c\u4ee5\u53ca@Dao\u6240\u8868\u793a\u7684\u6570\u636e\u8bbf\u95ee\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">@Database(entities = {User.class}, version = 1)<\/p>\n<p>public abstract class AppDatabase extends RoomDatabase {<\/p>\n<p>    public abstract UserDao userDao();<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u5b9a\u4e49\u5b9e\u4f53\uff08ENTITY\uff09<\/h3>\n<\/p>\n<p><p>\u8981\u5728Room\u4e2d\u4f7f\u7528\u67d0\u4e2a\u7c7b\u4f5c\u4e3a\u6570\u636e\u5e93\u8868\uff0c\u9700\u8981\u4f7f\u7528@Entity\u6ce8\u89e3\u6807\u6ce8\u8fd9\u4e2a\u7c7b\u3002Room\u5c06\u6bcf\u4e2a@Entity\u7c7b\u7684\u5b9e\u4f8b\u6620\u5c04\u5230\u6570\u636e\u5e93\u4e2d\u7684\u4e00\u884c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u5b9e\u4f53<\/strong><\/p>\n<\/p>\n<p><p>\u5b9e\u4f53\u7c7b\u4ee3\u8868\u6570\u636e\u5e93\u4e2d\u7684\u4e00\u5f20\u8868\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49POJO\uff0c\u5e76\u7528@Entity\u6ce8\u89e3\u6807\u8bb0\uff0cRoom\u4f1a\u4e3a\u8fd9\u4e9b\u5b57\u6bb5\u521b\u5efa\u8868\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">@Entity<\/p>\n<p>public class User {<\/p>\n<p>    @PrimaryKey<\/p>\n<p>    public int uid;<\/p>\n<p>    @ColumnInfo(name = &quot;first_name&quot;)<\/p>\n<p>    public String firstName;<\/p>\n<p>    @ColumnInfo(name = &quot;last_name&quot;)<\/p>\n<p>    public String lastName;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u5b9a\u4e49\u6570\u636e\u8bbf\u95ee\u5bf9\u8c61\uff08DAO\uff09<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u8bbf\u95ee\u5bf9\u8c61\uff08DAO\uff09\u662fRoom\u7684\u7ec4\u4ef6\u4e4b\u4e00\uff0c\u7528\u4e8e\u5b9a\u4e49\u6570\u636e\u5e93\u7684\u8bbf\u95ee\u65b9\u6cd5\u3002\u901a\u8fc7DAO\uff0c\u53ef\u4ee5\u5b9a\u4e49\u8bbf\u95ee\u6570\u636e\u5e93\u7684\u589e\u5220\u6539\u67e5\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efaDAO\u63a5\u53e3<\/strong><\/p>\n<\/p>\n<p><p>DAO\u5b9a\u4e49\u4e3a\u63a5\u53e3\u6216\u62bd\u8c61\u7c7b\u3002\u901a\u8fc7\u5728\u63a5\u53e3\u4e2d\u5b9a\u4e49\u65b9\u6cd5\uff0c\u5e76\u4f7f\u7528Room\u63d0\u4f9b\u7684\u6ce8\u89e3\uff08\u5982@Insert\u3001@Delete\uff09\u4fee\u9970\uff0cRoom\u4f1a\u81ea\u52a8\u751f\u6210\u8fd9\u4e9b\u65b9\u6cd5\u7684\u5177\u4f53\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">@Dao<\/p>\n<p>public interface UserDao {<\/p>\n<p>    @Insert<\/p>\n<p>    void insertAll(User... users);<\/p>\n<p>    @Delete<\/p>\n<p>    void delete(User user);<\/p>\n<p>    @Query(&quot;SELECT * FROM user&quot;)<\/p>\n<p>    List&lt;User&gt; getAll();<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001ROOM\u6570\u636e\u5e93\u7684\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728Room\u4e2d\u8fdb\u884c\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u901a\u5e38\u6d89\u53ca\u5230\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u6570\u636e\u5e93\u7684\u521b\u5efa\u3001\u5b9e\u4f53\u7684\u63d2\u5165\u3001\u6570\u636e\u7684\u67e5\u8be2\u3001\u66f4\u65b0\u548c\u5220\u9664\u3002\u901a\u8fc7RoomDatabase\u7684\u5b9e\u4f8b\u6765\u7ba1\u7406\u6570\u636e\u5e93\uff0c\u5e76\u901a\u8fc7DAO\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u636e\u5e93\u7684\u521b\u5efa<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Room.databaseBuilder()\u6216Room.inMemoryDatabaseBuilder()\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u5e93\u7684\u5b9e\u4f8b\u3002Room.databaseBuilder()\u4f1a\u5728\u8bbe\u5907\u7684\u6587\u4ef6\u7cfb\u7edf\u5185\u521b\u5efa\u6570\u636e\u5e93\uff0c\u800cRoom.inMemoryDatabaseBuilder()\u5219\u4f1a\u5728\u5185\u5b58\u4e2d\u521b\u5efa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">\/\/ \u521b\u5efa\u4e00\u4e2a\u6570\u636e\u5e93\u5b9e\u4f8b<\/p>\n<p>AppDatabase db = Room.databaseBuilder(context.getApplicationContext(),<\/p>\n<p>        AppDatabase.class, &quot;database-name&quot;).build();<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u7684\u63d2\u5165\u548c\u67e5\u8be2<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7DAO\u7684\u65b9\u6cd5\u5bf9\u6570\u636e\u5e93\u6267\u884c\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528UserDao\u7684insertAll()\u65b9\u6cd5\u63d2\u5165\u7528\u6237\u6570\u636e\uff0c\u4f7f\u7528getAll()\u65b9\u6cd5\u67e5\u8be2\u6240\u6709\u7528\u6237\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p><strong>\u5728Android\u5f00\u53d1\u4e2d\u6b63\u786e\u4f7f\u7528Room\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u5e94\u7528\u7684\u6027\u80fd\uff0c\u8fd8\u80fd\u63d0\u5347\u5f00\u53d1\u7684\u6548\u7387\u548c\u5e94\u7528\u7684\u6570\u636e\u5b89\u5168\u6027\u3002\u901a\u8fc7\u4e0a\u8ff0\u7684\u6b65\u9aa4\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5728\u81ea\u5df1\u7684\u5e94\u7528\u4e2d\u96c6\u6210\u548c\u4f7f\u7528Room\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u672c\u5730\u6570\u636e\u5e93<\/strong>\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5728 Android \u7cfb\u7edf\u4e2d\u4f7f\u7528 Room \u67b6\u6784\u7ec4\u4ef6\uff1f<\/strong><\/p>\n<ul>\n<li>\n<p>\u4e86\u89e3 Room\uff1a\u9996\u5148\uff0c\u9700\u8981\u4e86\u89e3 Room \u662f\u4ec0\u4e48\uff0c\u5b83\u662f Android \u67b6\u6784\u7ec4\u4ef6\u4e4b\u4e00\uff0c\u7528\u4e8e\u5728\u672c\u5730\u5b58\u50a8\u4e2d\u521b\u5efa\u548c\u7ba1\u7406 SQLite \u6570\u636e\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u5e76\u4e14\u80fd\u591f\u5728\u7f16\u8bd1\u65f6\u8fdb\u884c\u9759\u6001\u7c7b\u578b\u68c0\u67e5\uff0c\u4ece\u800c\u907f\u514d\u4e86\u8fd0\u884c\u65f6\u9519\u8bef\u3002<\/p>\n<\/li>\n<li>\n<p>\u5bfc\u5165 Room \u4f9d\u8d56\uff1a\u63a5\u4e0b\u6765\uff0c\u9700\u8981\u5728\u9879\u76ee\u4e2d\u5bfc\u5165 Room \u7684\u4f9d\u8d56\u3002\u5728 build.gradle \u6587\u4ef6\u4e2d\u6dfb\u52a0\u76f8\u5e94\u7684\u4f9d\u8d56\u9879\uff0c\u7136\u540e\u8fdb\u884c\u540c\u6b65\u3002\u8fd9\u6837\uff0c\u5c31\u53ef\u4ee5\u5728\u9879\u76ee\u4e2d\u4f7f\u7528 Room \u76f8\u5173\u7684\u7c7b\u548c\u63a5\u53e3\u3002<\/p>\n<\/li>\n<li>\n<p>\u521b\u5efa\u6570\u636e\u5e93\u548c\u5b9e\u4f53\uff1a\u7136\u540e\uff0c\u9700\u8981\u521b\u5efa\u6570\u636e\u5e93\u548c\u5b9e\u4f53\u3002\u4f7f\u7528 @Entity \u6ce8\u89e3\u6765\u5b9a\u4e49\u5b9e\u4f53\u7c7b\uff0c\u8868\u793a\u6570\u636e\u5e93\u4e2d\u7684\u4e00\u5f20\u8868\u3002\u8fd8\u53ef\u4ee5\u4f7f\u7528 @PrimaryKey\u3001@ColumnInfo \u7b49\u6ce8\u89e3\u6765\u5b9a\u4e49\u5b9e\u4f53\u7c7b\u7684\u4e3b\u952e\u548c\u5217\u4fe1\u606f\u3002\u540c\u65f6\uff0c\u8fd8\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u7ee7\u627f\u81ea RoomDatabase \u7684\u62bd\u8c61\u7c7b\u6765\u8868\u793a\u6574\u4e2a\u6570\u636e\u5e93\uff0c\u5e76\u4e14\u5728\u5176\u4e2d\u5b9a\u4e49\u6570\u636e\u5e93\u7684\u7248\u672c\u3001\u8868\u683c\u7b49\u4fe1\u606f\u3002<\/p>\n<\/li>\n<li>\n<p>\u521b\u5efa DAO \u63a5\u53e3\uff1a\u63a5\u4e0b\u6765\uff0c\u521b\u5efa DAO \u63a5\u53e3\uff0c\u7528\u4e8e\u5b9a\u4e49\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u7684\u65b9\u6cd5\u3002\u53ef\u4ee5\u4f7f\u7528 @Query\u3001@Insert\u3001@Update \u7b49\u6ce8\u89e3\u6765\u5b9a\u4e49\u5404\u79cd\u64cd\u4f5c\u3002\u540c\u65f6\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 LiveData \u6216 Flow \u6765\u5b9e\u73b0\u6570\u636e\u7684\u5b9e\u65f6\u66f4\u65b0\u548c\u5f02\u6b65\u64cd\u4f5c\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528 Room \u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff1a\u6700\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528 DAO \u63a5\u53e3\u4e2d\u5b9a\u4e49\u7684\u65b9\u6cd5\u6765\u4f7f\u7528 Room \u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3002\u53ef\u4ee5\u6267\u884c\u63d2\u5165\u3001\u67e5\u8be2\u3001\u66f4\u65b0\u3001\u5220\u9664\u7b49\u64cd\u4f5c\uff0c\u83b7\u53d6\u6570\u636e\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5f02\u6b65\u4efb\u52a1\u6216\u5728\u540e\u53f0\u7ebf\u7a0b\u4e2d\u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u4ee5\u907f\u514d\u4e3b\u7ebf\u7a0b\u963b\u585e\u3002<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u5982\u4f55\u5728 Android \u5f00\u53d1\u4e2d\u4f7f\u7528 Room \u67b6\u6784\u7ec4\u4ef6\u6765\u5b9e\u73b0\u6570\u636e\u7684\u6301\u4e45\u5316\u5b58\u50a8\uff1f<\/strong><\/p>\n<ul>\n<li>\n<p>Room \u67b6\u6784\u7ec4\u4ef6\u7684\u6982\u8ff0\uff1a\u9996\u5148\uff0c\u9700\u8981\u4e86\u89e3 Room \u662f\u4ec0\u4e48\uff0c\u5b83\u662f Android \u67b6\u6784\u7ec4\u4ef6\u4e4b\u4e00\uff0c\u7528\u4e8e\u5728\u672c\u5730\u5b58\u50a8\u4e2d\u521b\u5efa\u548c\u7ba1\u7406 SQLite \u6570\u636e\u5e93\u3002\u4e0e\u4f20\u7edf\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u76f8\u6bd4\uff0cRoom \u63d0\u4f9b\u4e86\u66f4\u7b80\u5355\u3001\u66f4\u9ad8\u6548\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u6570\u636e\u7684\u6301\u4e45\u5316\u5b58\u50a8\u3002<\/p>\n<\/li>\n<li>\n<p>\u521b\u5efa\u6570\u636e\u5e93\u548c\u5b9e\u4f53\uff1a\u5728\u5f00\u59cb\u4f7f\u7528 Room \u4e4b\u524d\uff0c\u9700\u8981\u521b\u5efa\u6570\u636e\u5e93\u548c\u5b9e\u4f53\u3002\u901a\u8fc7\u6ce8\u89e3\u65b9\u5f0f\u6765\u5b9a\u4e49\u5b9e\u4f53\u7c7b\uff0c\u5e76\u4e14\u4f7f\u7528 @Entity\u3001@PrimaryKey\u3001@ColumnInfo \u7b49\u6ce8\u89e3\u6765\u6307\u5b9a\u8868\u683c\u548c\u5217\u7684\u4fe1\u606f\u3002\u540c\u65f6\uff0c\u8fd8\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u7ee7\u627f\u81ea RoomDatabase \u7684\u62bd\u8c61\u7c7b\u6765\u8868\u793a\u6574\u4e2a\u6570\u636e\u5e93\uff0c\u5e76\u5728\u5176\u4e2d\u5b9a\u4e49\u6570\u636e\u5e93\u7684\u7248\u672c\u3001\u8868\u683c\u7b49\u4fe1\u606f\u3002<\/p>\n<\/li>\n<li>\n<p>\u521b\u5efa DAO \u63a5\u53e3\uff1a\u63a5\u4e0b\u6765\uff0c\u521b\u5efa DAO\uff08Data Access Object\uff09\u63a5\u53e3\uff0c\u7528\u4e8e\u5b9a\u4e49\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u7684\u65b9\u6cd5\u3002\u53ef\u4ee5\u4f7f\u7528 @Query \u6ce8\u89e3\u6765\u6267\u884c\u67e5\u8be2\u64cd\u4f5c\uff0c\u4f7f\u7528 @Insert\u3001@Update\u3001@Delete \u6ce8\u89e3\u6765\u6267\u884c\u63d2\u5165\u3001\u66f4\u65b0\u3001\u5220\u9664\u64cd\u4f5c\u3002\u8fd8\u53ef\u4ee5\u4f7f\u7528 LiveData \u6216 Flow \u6765\u5b9e\u73b0\u6570\u636e\u7684\u5b9e\u65f6\u66f4\u65b0\u548c\u5f02\u6b65\u64cd\u4f5c\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528 Room \u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff1a\u6700\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528 DAO \u63a5\u53e3\u4e2d\u5b9a\u4e49\u7684\u65b9\u6cd5\u6765\u4f7f\u7528 Room \u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3002\u53ef\u4ee5\u6267\u884c\u63d2\u5165\u3001\u67e5\u8be2\u3001\u66f4\u65b0\u3001\u5220\u9664\u7b49\u64cd\u4f5c\uff0c\u83b7\u53d6\u6570\u636e\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4e8b\u52a1\u3001\u5f02\u6b65\u4efb\u52a1\u6216\u5728\u540e\u53f0\u7ebf\u7a0b\u4e2d\u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u4ee5\u63d0\u9ad8\u6027\u80fd\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u5728 Android \u5f00\u53d1\u4e2d\uff0c\u4f7f\u7528 Room \u67b6\u6784\u7ec4\u4ef6\u4e0e\u4f7f\u7528\u4f20\u7edf SQLite \u6570\u636e\u5e93\u64cd\u4f5c\u7684\u6bd4\u8f83\u6709\u54ea\u4e9b\u4f18\u52bf\uff1f<\/strong><\/p>\n<ul>\n<li>\n<p>\u7c7b\u578b\u5b89\u5168\uff1a\u4f7f\u7528 Room \u67b6\u6784\u7ec4\u4ef6\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u8fdb\u884c\u9759\u6001\u7c7b\u578b\u68c0\u67e5\uff0c\u4ece\u800c\u907f\u514d\u4e86\u8fd0\u884c\u65f6\u9519\u8bef\u3002\u901a\u8fc7\u5b9a\u4e49\u5b9e\u4f53\u7c7b\u548c DAO \u63a5\u53e3\uff0c\u53ef\u4ee5\u66f4\u76f4\u89c2\u548c\u5b89\u5168\u5730\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3002<\/p>\n<\/li>\n<li>\n<p>\u7b80\u5316\u64cd\u4f5c\uff1a\u76f8\u6bd4\u4f20\u7edf\u7684 SQLite \u6570\u636e\u5e93\u64cd\u4f5c\uff0cRoom \u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u3001\u66f4\u9ad8\u6548\u7684 API\uff0c\u5927\u91cf\u51cf\u5c11\u4e86\u6837\u677f\u4ee3\u7801\u7684\u7f16\u5199\u3002\u4f7f\u7528\u6ce8\u89e3\u65b9\u5f0f\u5b9a\u4e49\u5b9e\u4f53\u7c7b\u548c DAO \u63a5\u53e3\uff0c\u4e0d\u9700\u8981\u624b\u52a8\u7f16\u5199 SQL \u8bed\u53e5\u3002<\/p>\n<\/li>\n<li>\n<p>\u5b9e\u65f6\u66f4\u65b0\uff1aRoom \u652f\u6301\u4f7f\u7528 LiveData \u6216 Flow \u6765\u5b9e\u73b0\u6570\u636e\u7684\u5b9e\u65f6\u66f4\u65b0\u548c\u5f02\u6b65\u64cd\u4f5c\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u76d1\u542c\u6570\u636e\u7684\u53d8\u5316\u5e76\u53ca\u65f6\u66f4\u65b0 UI\u3002\u8fd9\u662f\u4f20\u7edf SQLite \u6570\u636e\u5e93\u64cd\u4f5c\u6240\u4e0d\u5177\u5907\u7684\u7279\u6027\u3002<\/p>\n<\/li>\n<li>\n<p>\u6570\u636e\u5e93\u8fc1\u79fb\uff1a\u5728\u6570\u636e\u5e93\u7ed3\u6784\u53d8\u66f4\u65f6\uff0cRoom \u63d0\u4f9b\u4e86\u6570\u636e\u5e93\u8fc1\u79fb\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u81ea\u52a8\u5347\u7ea7\u6570\u636e\u5e93\u7248\u672c\uff0c\u5e76\u4e14\u4fdd\u7559\u65e7\u6570\u636e\u3002\u76f8\u6bd4\u4f20\u7edf\u7684\u624b\u52a8\u5904\u7406\u6570\u636e\u5e93\u5347\u7ea7\uff0c\u66f4\u52a0\u65b9\u4fbf\u548c\u53ef\u9760\u3002<\/p>\n<\/li>\n<li>\n<p>\u9ad8\u6027\u80fd\uff1aRoom \u4f7f\u7528 SQLite \u4f5c\u4e3a\u5e95\u5c42\u6570\u636e\u5e93\u5f15\u64ce\uff0c\u5177\u6709\u9ad8\u6027\u80fd\u548c\u9ad8\u6548\u7684\u6570\u636e\u5b58\u53d6\u80fd\u529b\u3002\u540c\u65f6\uff0cRoom \u8fd8\u652f\u6301\u5728\u540e\u53f0\u7ebf\u7a0b\u4e2d\u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u907f\u514d\u4e3b\u7ebf\u7a0b\u963b\u585e\uff0c\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"\u5728Android\u7cfb\u7edf\u4e2d\uff0c\u4f7f\u7528Room\u67b6\u6784\u7ec4\u4ef6\u53ef\u4ee5\u6781\u5927\u5730\u7b80\u5316\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u6d41\u7a0b\uff0c\u63d0\u9ad8\u5e94\u7528\u6027\u80fd\u3001\u7b80\u5316\u6570\u636e\u5e93\u64cd\u4f5c\u3001\u786e\u4fdd [&hellip;]","protected":false},"author":3,"featured_media":303285,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/303244"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=303244"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/303244\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/303285"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=303244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=303244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=303244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}