{"id":226,"date":"2016-10-31T08:35:30","date_gmt":"2016-10-31T08:35:30","guid":{"rendered":"https:\/\/boochlin.com\/?p=226"},"modified":"2016-10-31T09:39:30","modified_gmt":"2016-10-31T09:39:30","slug":"greendao-3-introduction","status":"publish","type":"post","link":"https:\/\/boochlin.com\/?p=226","title":{"rendered":"GreenDao 3 Introduction"},"content":{"rendered":"<p><a href=\"https:\/\/boochlin.com\/wp-content\/uploads\/2016\/10\/greenDAO-vs-OrmLite-vs-ActiveAndroid.png\"><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone size-full wp-image-227\" src=\"https:\/\/boochlin.com\/wp-content\/uploads\/2016\/10\/greenDAO-vs-OrmLite-vs-ActiveAndroid.png\" alt=\"greendao-vs-ormlite-vs-activeandroid\" width=\"1022\" height=\"619\" \/><\/a><\/p>\n<p>\u9996\u5148\u5148\u4f86\u4e00\u5f35 GreenDao 3 \u5b98\u65b9\u7684\u81ea\u5439\u81ea\u64c2\uff0c\u96d6\u7136\u770b\u8d77\u4f86\u8ddf GreenDao 2 \u5dee\u4e0d\u591a\uff0c\u5c31\u662f\u5c2c\u7206\u5176\u4ed6\u76ee\u524d\u6240\u6709 ORM Lib \u7684\u6548\u80fd\uff0c\u6703\u9019\u6a23\u8aaa\u662f\u56e0\u70ba\u4e4b\u524d\u5c31\u662f\u7528 GreenDao 2 \u4f5c\u70ba\u4e3b\u8981\u958b\u767c DB \u6846\u67b6\uff0c\u7576\u6642\u5c31\u770b\u5230\u540c\u4e00\u5f35\u5716\u8868\u3002\u8a73\u60c5\u53ef\u4ee5\u770b\u9019\u4e00\u7bc7 \u300e<a href=\"https:\/\/boochlin.com\/?p=85\">Android ORM Library- Green DAO\uff08\u4e00\uff09 introduction<\/a>\u300f\u3002\u57fa\u65bc\u4ee5\u4e0a\u7406\u7531\uff0c\u9019\u6b21\u66f4\u65b0\uff0c\u81ea\u7136\u5c31\u958b\u958b\u5fc3\u5fc3\u5f97\u4f86\u8a66\u8a66\u770b\u3002\u81f3\u65bc\u597d\u8655\u58de\u8655\u4f7f\u7528\u539f\u56e0\u60c5\u5883\uff0c\u5168\u90e8\u8ddf\u524d\u4e00\u7bc7\u4e00\u6a21\u4e00\u6a23\uff0c\u4e0d\u518d\u8d05\u8ff0\u3002<!--more--><\/p>\n<p>\u96d6\u7136 Greendao 2 \u6642\u7528\u7684\u5c31\u4e0d\u662f\u5f88\u6df1\u5165\uff0c\u4f46\u662f\u73fe\u5728\u4e00\u6bd4\u8f03\u767c\u73fe\uff0c\u9019\u6b21\u6539\u9032\u6700\u5927\u7684\u5c31\u662f\u5efa\u5236\u57fa\u672c DAO \u7684\u65b9\u6cd5\uff0c\u6d3b\u7528\u4e86 android studio \u4e2d\u7684 gradle plugin\u3002\u6703\u9019\u6a23\u8aaa\u5c31\u662f\u4ee5\u524d\u7684\u65b9\u6cd5\u5be6\u5728\u4e0d\u597d\u4e0a\u624b\uff0c\u5fc5\u9700\u8981\u5148\u5728 project \u5916\u5efa\u7f6e\u4e00\u500b java file\uff0c\u5c0d\uff0c\u5c31\u662f\u6709\u4e00\u500b public static main \u7684\u90a3\u7a2e\u7a0b\u5f0f\uff0c\u7136\u5f8c\u628a\u6b04\u4f4d\u5beb\u9032\u53bb\uff0c\u5728\u642d\u914d Gradle \u7684\u63d2\u4ef6\u300egreendao generator\u300f\u624d\u80fd\u81ea\u52d5\u751f\u6210 DaoMaster , DaoSession , xxxDao , xxxEnity \u7b49 Class\uff0c\u9019\u4ef6\u4e8b\u5927\u5927\u5730\u8b93\u6211\u4e0d\u60f3\u8981\u96a8\u4fbf\u53bb\u6539\u52d5 db \uff0c\u56e0\u70ba\u5be6\u5728\u9ebb\u7169\uff0c\u800c\u4e14\u90a3\u500b java file \u6839\u672c\u4e0d\u6703\u5728\u7528\u5230\uff0c\u53ef\u8aaa\u662f\u5ee2 code.<\/p>\n<p>\u800c\u73fe\u5728\u5247\u662f\u5927\u5927\u6539\u9032\u90a3\u4e00\u5806\u6211\u90fd\u4e0d\u77e5\u9053\u4e0a\u9762\u518d\u8aaa\u5565\u7684\u90e8\u4efd\uff0c\u56e0\u6b64\u63a5\u4e0b\u4f86\u6162\u6162\u4e00\u6b65\u4e00\u6b65\u5f97\u4f86\u89e3\u8aaa\u5230\u5e95\u8b8a\u6210\u600e\u9ebc\u6a23\u4e86<\/p>\n<h4>step 1. \u4e00\u6a23\u5148\u5728 build.gradle \u88e1\u52a0\u5165<\/h4>\n<pre>buildscript {\r\n    repositories {\r\n        mavenCentral()\r\n    }\r\n    dependencies {\r\n        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.0'\r\n    }\r\n}\r\n\r\napply plugin: 'org.greenrobot.greendao'\r\n\r\ndependencies {\r\n    compile 'org.greenrobot:greendao:3.2.0'\r\n}\r\n<\/pre>\n<h4>step 2 . \u7136\u5f8c\u53b2\u5bb3\u7684\u90e8\u4efd\u5c31\u4f86\u5566\uff0c\u53ef\u4ee5\u958b\u59cb\u5275 dao<\/h4>\n<p>\u9019\u908a\u4ee5\u5b98\u65b9\u7bc4\u4f8b note \u7269\u4ef6\u4f86\u5beb\uff0c\u4ee5 id , text , date \u4f5c\u70ba\u6b04\u4f4d\u540d\u7a31\u3002\u53ea\u8981\u5728\u8981\u4f7f\u7528\u7684\u5c08\u6848\u4e2d\u591a\u52a0\u500b class \uff0c \u50cf\u662f Note.java \uff0c\u4e26\u5728 class \u4e0a\u52a0\u5165 annotation Entity \u5c31\u53ef\u4ee5\u641e\u5b9a\uff0c \u63a5\u4e0b\u4f86\u6839\u64da\u9700\u6c42\u8a2d\u5b9a\u5176\u4ed6 annotation \u50cf\u662f Id , NotNull \u00a0, Unique \u4e4b\u985e\u7684\u3002<\/p>\n<pre>@Entity\r\npublic class Note {\r\n \r\n    @Id\r\n    private Long id;\r\n \r\n    @NotNull\r\n    private String text;\r\n    private Date date;\r\n}\r\n<\/pre>\n<p>\u88dc\u5145\uff1a<\/p>\n<ul>\n<li>The <strong>@Id<\/strong> annotation selects a <span id=\"crayon-581312328c291171407260\" class=\"crayon-syntax crayon-syntax-inline crayon-theme-github crayon-theme-github-inline crayon-font-consolas\"><span class=\"crayon-pre crayon-code\"><span class=\"crayon-t\">long<\/span><\/span><\/span>\/ <span id=\"crayon-581312328c292495330257\" class=\"crayon-syntax crayon-syntax-inline crayon-theme-github crayon-theme-github-inline crayon-font-consolas\"><span class=\"crayon-pre crayon-code\"><span class=\"crayon-t\">Long<\/span><\/span><\/span> property as the entity ID. In database terms, it\u2019s the primary key. The parameter <strong>autoincrement<\/strong>\u00a0is a flag to make the ID value ever increasing (not reusing old values).<\/li>\n<li><strong>@Property<\/strong> lets you define a\u00a0non-default column name, which the\u00a0property is mapped to. If absent, greenDAO will use the field name in a SQL-ish fashion (upper case, underscores instead of camel case, for example <span id=\"crayon-581312328c294818694285\" class=\"crayon-syntax crayon-syntax-inline crayon-theme-github crayon-theme-github-inline crayon-font-consolas\"><span class=\"crayon-pre crayon-code\"><span class=\"crayon-v\">customName<\/span><\/span><\/span> will become <span id=\"crayon-581312328c296388810555\" class=\"crayon-syntax crayon-syntax-inline crayon-theme-github crayon-theme-github-inline crayon-font-consolas\"><span class=\"crayon-pre crayon-code\"><span class=\"crayon-v\">CUSTOM_NAME<\/span><\/span><\/span>). <em>Note: you currently can only use inline constants to specify a column name.<\/em><\/li>\n<li><strong>@NotNull<\/strong> makes the property a \u201cNOT NULL\u201d column on the database side. Usually it makes sense to mark\u00a0primitive types (long, int, short, byte) with @NotNull, while having nullable values with wrapper classes (Long, Integer, Short, Byte).<\/li>\n<li><strong>@Transient<\/strong> marks properties to be excluded from persistence. Use those for temporary states, etc. Alternatively, you can also use the <strong>transient<\/strong> keyword from Java.<\/li>\n<\/ul>\n<h4>step3 . Note.java \u5beb\u597d\u5f8c\u76f4\u63a5\u6309 project make \uff0c\u7136\u5f8c\u559d\u500b\u5496\u5561\uff0c Note.java \u5c31\u6703\u6416\u8eab\u4e00\u904d\u6210&#8230;..<\/h4>\n<p>\u300e\u4e00\u500b\u52a0\u5f37\u7248\u7684 Note.java\u300f<\/p>\n<p>\u591a\u4e86\u4e00\u5806\u6709\u7684\u6c92\u6709\u7684 setter , getter \uff0c\u6c92\u932f\u9019\u6a23\u5c31\u5b8c\u6210\u5566\uff0c\u4e00\u500b\u5168\u65b0\u7684 entity\uff0c\u53e6\u5916\u5565\u5c0f DaoMaster , DaoSession , xxxDao \u4e5f\u90fd\u8ddf\u8005\u4e00\u8d77\u8dd1\u51fa\u4f86\u3002\u9810\u8a2d package\u00a0\u6703\u662f note.java \u7684 package \u3002gen dir \u662f src\/java\/main<\/p>\n<pre>@Entity(indexes = {\r\n    @Index(value = \"text, date DESC\", unique = true)\r\n})\r\npublic class Note {\r\n\r\n    @Id\r\n    private Long id;\r\n\r\n    @NotNull\r\n    private String text;\r\n    private String comment;\r\n    private java.util.Date date;\r\n\r\n    @Convert(converter = NoteTypeConverter.class, columnType = String.class)\r\n    private NoteType type;\r\n\r\n    @Generated(hash = 1272611929)\r\n    public Note() {\r\n    }\r\n\r\n    public Note(Long id) {\r\n        this.id = id;\r\n    }\r\n\r\n    @Generated(hash = 1686394253)\r\n    public Note(Long id, @NotNull String text, String comment, java.util.Date date, NoteType type) {\r\n        this.id = id;\r\n        this.text = text;\r\n        this.comment = comment;\r\n        this.date = date;\r\n        this.type = type;\r\n    }\r\n\r\n    public Long getId() {\r\n        return id;\r\n    }\r\n\r\n    public void setId(Long id) {\r\n        this.id = id;\r\n    }\r\n\r\n    @NotNull\r\n    public String getText() {\r\n        return text;\r\n    }\r\n\r\n    \/** Not-null value; ensure this value is available before it is saved to the database. *\/\r\n    public void setText(@NotNull String text) {\r\n        this.text = text;\r\n    }\r\n\r\n    public String getComment() {\r\n        return comment;\r\n    }\r\n\r\n    public void setComment(String comment) {\r\n        this.comment = comment;\r\n    }\r\n\r\n    public java.util.Date getDate() {\r\n        return date;\r\n    }\r\n\r\n    public void setDate(java.util.Date date) {\r\n        this.date = date;\r\n    }\r\n\r\n    public NoteType getType() {\r\n        return type;\r\n    }\r\n\r\n    public void setType(NoteType type) {\r\n        this.type = type;\r\n    }\r\n\r\n}\r\n<\/pre>\n<p>\u5982\u679c\u4e0d\u559c\u6b61\u9019\u500b\u751f\u6210\u7684\u4f4d\u5740\u3002\u5c31\u5728 gradle \u52a0\u5165\u8a2d\u5b9a<\/p>\n<pre>greendao {\r\n    schemaVersion 2\r\n    daoPackage  'com.android.heaton.database'\r\n    targetGenDir 'src\/main\/java'\r\n}\r\n<\/pre>\n<p>\u88dc\u5145\uff1a<\/p>\n<ul>\n<li><strong>schemaVersion:<\/strong> The current version of the database schema. This is used by the <span id=\"crayon-581312328c282692943989\" class=\"crayon-syntax crayon-syntax-inline crayon-theme-github crayon-theme-github-inline crayon-font-consolas\"><span class=\"crayon-pre crayon-code\"><span class=\"crayon-o\">*<\/span><span class=\"crayon-v\">OpenHelpers<\/span><\/span><\/span> classes to migrate between schema versions. If you change your entity\/database schema, this value has to be increased. <em>Defaults to 1.\u7248\u672c\u865f<\/em><\/li>\n<li><strong>daoPackage<\/strong>:\u00a0The package name for generated DAOs, DaoMaster, and DaoSession.\u00a0<em>Defaults to the package name of your source entities. \u751f\u6210 Package PATH<\/em><\/li>\n<li><strong>targetGenDir:<\/strong> The location where generated sources should be stored at.\u00a0<em>Defaults to the generated source folder inside the build directory<\/em> ( <span id=\"crayon-581312328c285295200712\" class=\"crayon-syntax crayon-syntax-inline crayon-theme-github crayon-theme-github-inline crayon-font-consolas\"><span class=\"crayon-pre crayon-code\"><span class=\"crayon-v\">build<\/span><span class=\"crayon-o\">\/<\/span><span class=\"crayon-v\">generated<\/span><span class=\"crayon-o\">\/<\/span><span class=\"crayon-v\">source<\/span><span class=\"crayon-o\">\/<\/span><span class=\"crayon-v\">greendao<\/span><\/span><\/span>).<\/li>\n<li><strong>generateTests:<\/strong>\u00a0Set to true to automatically generate unit tests.<\/li>\n<li><strong>targetGenDirTests:<\/strong>\u00a0The base directory where generated unit tests should be stored at. <em>Defaults to<span id=\"crayon-581312328c287648279350\" class=\"crayon-syntax crayon-syntax-inline crayon-theme-github crayon-theme-github-inline crayon-font-consolas\"><span class=\"crayon-pre crayon-code\"><span class=\"crayon-v\">src<\/span><span class=\"crayon-o\">\/<\/span><span class=\"crayon-v\">androidTest<\/span><span class=\"crayon-o\">\/<\/span><span class=\"crayon-v\">java<\/span><\/span><\/span><\/em>.<\/li>\n<\/ul>\n<h4><strong>step 4. \u00a0\u5230\u9019\u968e\u6bb5\u5c31\u662f\u4e00\u822c CRUD<\/strong><\/h4>\n<p><span style=\"font-weight: 400;\">Query:<\/span><span style=\"font-weight: 400;\">\u901a\u5e38\u90fd\u6703\u4f7f\u7528 QueryBuilder \u4f5c\u70baquery\u7684\u4e3b\u8981\u8f14\u52a9\u5de5\u5177\uff0cwhere , or \u4e4b\u985e\u7684 sql statement \u7528\u7269\u4ef6\u7684\u65b9\u5f0f\u8868\u73fe\u51fa\u4f86<\/span><\/p>\n<pre>QueryBuilder qb = userDao.queryBuilder();\r\nqb.where(Properties.FirstName.eq(\"Joe\"),\r\nqb.or(Properties.YearOfBirth.gt(1970),\r\nqb.and(Properties.YearOfBirth.eq(1970), Properties.MonthOfBirth.ge(10))));\r\nList youngJoes = qb.list();\r\n<\/pre>\n<ul>\n<li style=\"font-weight: 400;\"><strong>list()<\/strong><span style=\"font-weight: 400;\"> All entities are loaded into memory. The result is typically an <\/span><span style=\"font-weight: 400;\">ArrayList<\/span><span style=\"font-weight: 400;\"> with no magic involved. Easiest to use. \u6700\u5e38\u898b\u7684\u4e00\u73ed\u7528\u6cd5\uff0c\u8fd4\u56de ArrayList<\/span><\/li>\n<li style=\"font-weight: 400;\"><strong>listLazy()<\/strong><span style=\"font-weight: 400;\"> Entities are loaded into memory on-demand. Once an element in the list is accessed for the first time, it is loaded and cached for future use. Must be closed. \u5206\u968e\u6bb5\u8b80\u53d6\uff0c\u7576\u771f\u7684\u9700\u8981\u6642\uff0c\u624d\u6703\u8b80\u53d6\u3002<\/span><\/li>\n<li style=\"font-weight: 400;\"><strong>listLazyUncached()<\/strong><span style=\"font-weight: 400;\"> A \u201cvirtual\u201d list of entities: any access to a list element results in loading its data from the database. Must be closed. \u4e0d\u7d93\u904e memory cache\uff0c\u76f4\u63a5\u53bb db access\u00a0<\/span><\/li>\n<li><strong>listIterator()<\/strong><span style=\"font-weight: 400;\"> Let\u2019s you iterate through results by loading the data on-demand (lazily). Data is not cached. Must be closed. \u57fa\u672c\u4e0a\u540c\u7b2c\u4e00\u500b<\/span><\/li>\n<\/ul>\n<p><strong>Insert:<\/strong><\/p>\n<pre>\/\/ Insert one object\r\nNoteDataDao.insert(new NoteData());\r\n\/\/ Insert and update object\r\nNoteDataDao.insertOrReplace(new NoteData());\r\n\/\/ block insert objects\r\nNoteDataDao.insertInTx(List list);\r\n<\/pre>\n<p><strong>Update:<\/strong><\/p>\n<pre>NoteDataDao.update(new NoteData());\r\nNoteDataDao.updateKeyAfterInsert(new NoteData() , 2);\r\nNoteDataDao.updateInTx(list);\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Delete<\/strong><\/p>\n<pre>NoteDataDao.deleteAll();\r\nNoteDataDao.deleteByKey(list);\r\nNoteDataDao.deleteInTx(list);\r\n\/\/  \u6700\u5e38\u898b\u7684\u9084\u662f\u7d66\u4e88\u689d\u4ef6\u5f0f\u7136\u5f8c\u522a\u9664\u7b26\u5408\u689d\u4ef6\u7684\u7269\u4ef6\uff0c\u9019\u5c31\u4e00\u6a23\u9700\u8981 query builder \r\n\/\/  \u53bb build \u51fa delete sql statement  \r\nNoteDataDao.queryBuilder(XXX condition ).buildDelete().executeDeleteWithoutDetachingEntities();\r\n<\/pre>\n<p>ps. Greendao 3 \u4f9d\u7136\u6c92\u6709\u53ef\u4ee5\u8a2d\u5b9a entity \u6b04\u4f4d\u521d\u59cb\u503c\u529f\u80fd\uff0c\u8a73\u60c5\u4f9d\u6a23\u53ef\u4ee5\u770b<a href=\"https:\/\/boochlin.com\/?p=92\">Android ORM Library \u2013 Green DAO\uff08\u4e8c\uff09Question: Why can\u2019t set default value for entities<\/a><\/p>\n<p>\u56e0\u70ba\u4e4b\u524d\u5df2\u7d93\u5beb\u904e\u4e00\u7bc7\u5f88\u8a73\u7d30\u7684\uff0c\u9019\u6b21\u5c31\u4e0d\u8a73\u8ff0\uff0c\u53ea\u8ac7\u5dee\u7570\u90e8\u5206\uff0c\u611f\u8b1d\u6536\u770b<\/p>\n<p>&nbsp;<\/p>\n<p>reference:<\/p>\n<p><a href=\"http:\/\/greenrobot.org\/greendao\/documentation\/\">http:\/\/greenrobot.org\/greendao\/documentation\/<\/a><br \/>\n<a href=\"http:\/\/www.cnblogs.com\/whoislcj\/p\/5651396.html\">Android\u6570\u636e\u5b58\u50a8\u4e4bGreenDao 3.0 \u8be6\u89e3<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9996\u5148\u5148\u4f86\u4e00\u5f35 GreenDao 3 \u5b98\u65b9\u7684\u81ea\u5439\u81ea\u64c2\uff0c\u96d6\u7136\u770b\u8d77\u4f86\u8ddf GreenDao 2 \u5dee\u4e0d\u591a\uff0c\u5c31\u662f\u5c2c\u7206\u5176\u4ed6\u76ee\u524d\u6240\u6709 ORM Lib \u7684\u6548\u80fd\uff0c\u6703\u9019\u6a23\u8aaa\u662f\u56e0\u70ba\u4e4b\u524d\u5c31\u662f\u7528 GreenDao 2 \u4f5c\u70ba\u4e3b\u8981\u958b&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50,23,52],"tags":[6,53,24],"_links":{"self":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/226"}],"collection":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=226"}],"version-history":[{"count":0,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/226\/revisions"}],"wp:attachment":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}