GreenDao3使用完全解析

11,gradle配置(官网样例地址https://github.com/greenrobot/greenDAO/blob/master/examples/RxDaoExample/build.gradle) 2 3Module的gradle 里安装如下配置(官方给的配置样例) 4 5buildscript { 6 repositories { 7 jcenter() 8 mavenCentral() 9 } 10 11 dependencies { 12 classpath 'com.android.tools.build:gradle:2.3.1' 13 classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' 14 } 15} 16apply plugin: 'com.android.application' 17apply plugin: 'org.greenrobot.greendao' 18android { 19 compileSdkVersion 25 20 buildToolsVersion "25.0.3" 21 defaultConfig { 22 applicationId "com.example.administrator.myapplication" 23 minSdkVersion 15 24 targetSdkVersion 25 25 versionCode 1 26 versionName "1.0" 27 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 28 } 29 buildTypes { 30 release { 31 minifyEnabled false 32 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 33 } 34 } 35} 36 37greendao { 38 targetGenDir 'src/main/java' 39 daoPackage 'com.example.administrator.myapplication' 40 /* 41 *schemaVersion 当前数据库结构的版本。结构版本变化时在OpenHelpers中被使用到。当你改变实体或者数据的结构时,这个值应该增加。 42 daoPackage 生成的DAO,DaoMaster和DaoSession的包名。默认是实体的包名。 43 targetGenDir 生成源文件的路径。默认源文件目录是在build目录中的(build/generated/source/greendao)。 44 generateTests 设置是否自动生成单元测试。 45 targetGenDirTest 生成的单元测试的根目录。 46 * */ 47} 48dependencies { 49 compile fileTree(include: ['*.jar'], dir: 'libs') 50 androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 51 exclude group: 'com.android.support', module: 'support-annotations' 52 }) 53 compile 'com.android.support:appcompat-v7:25.3.1' 54 compile 'com.android.support.constraint:constraint-layout:1.0.2' 55 testCompile 'junit:junit:4.12' 56 compile 'org.greenrobot:greendao:3.2.2' 57 compile 'org.greenrobot:greendao-generator:3.2.2' 58} 59uploadArchives.enabled = false 60 61 622,GreenDao注解详解 63 64/* 65 * A、 66 @Entity用于描述实体类名,其中active表示update/delete/refresh 方法是否自动生成,默认为false. 67 createInDb表示是否在数据库中创建表,默认为true,如果为false,将不创建该表. 68 generateConstructors表示是否自动生成构造方法(一个有参构造,一个无参构造). 69 indexes表示制定查询数据返回的默认排序规则.(@Index中的value制定排序的数据表中的列明加上排序规则即(ASC/DESC), 70 name表示......,unique表示是否唯一即SQL中的去重复 71 如果按照多个字段来排序可以这样(比如(indexes={@Index(value="ID ASC"),@Index(value="AGE DESC")}或者 72 indexes={@Index(value="ID ASC AGE DESC")}))) 73 nameInDb表示该实体对应的数据表的名称,默认为实体名的拼音全大写 74 generateGettersSetters表示是否自动生成get/set方法,默认为true 75 B、@Id表示该字段是主键,autoincrement表示是否自增,默认为false. 76 C、@Property用于描述字段,nameInDb表示该字段在数据表中对应的列名,默认是实体字段名称. 77 D、@NotNull表示该字段不为null. 78 E、@Transient 表示在创建数据表时候忽略这个字段,也就是在创建表的时候不会创建这个字段. 79 F、@ToOne表示一对一的关系,也就是多条这个实体只对应一条标识的实体joinProperty标识这个实体表中的哪个字段和标识的实体表的主键关联. 80 G、@ToMany标识一对多的关系,也就是一条该实体数据通过指定列名和标识的数据实体的指定列名对应关系(@referencedJoinProperty表示当前标识的实体对应的数据表的 81 主键,@joinProperties表示当前表和标识的实体对应的数据表的属性对应关系) 82 H、@Convert定义当前标识的实体和数据表中字段之间装换规则.converter表示转换器.columnType表示对应的数据表列名在表中的数据类型, 83* */ 84 85 86以下是这些注释的详细例子: 87 88 89package com.example.administrator.myapplication; 90 91import org.greenrobot.greendao.DaoException; 92import org.greenrobot.greendao.annotation.Convert; 93import org.greenrobot.greendao.annotation.Generated; 94import org.greenrobot.greendao.annotation.Index; 95import org.greenrobot.greendao.annotation.JoinProperty; 96import org.greenrobot.greendao.annotation.NotNull; 97import org.greenrobot.greendao.annotation.Property; 98import org.greenrobot.greendao.annotation.Transient; 99import java.util.List; 100/** 101 * Created by Administrator on 2017/5/26. 102 */ 103@org.greenrobot.greendao.annotation.Entity( 104 active = true//表示update/delete/refresh 方法是否自动生成,默认为false 105 ,createInDb = true//表示是否在数据库中创建该表,默认为true 106 ,generateConstructors = true//表示是否生成构造方法(一般有两个,一个有参数,一个无参数),默认为false 107 ,indexes ={ @Index(value ="Id ASC"),@Index(value = "Age DESC")} 108 //indexes表示数据表查询返回数据默认排序,name中的字段是该实体在数据表中的列名,value表示改实体的真实名称,unique表示是否唯一(默认为false) 109 ,nameInDb = "STUDENT"//表示数据表的名称默认为实体类的名称 110 ,generateGettersSetters = true//表示是否生成get/set方法,默认为true 111) 112public class Student { 113 @org.greenrobot.greendao.annotation.Id(autoincrement = true)//设置数据表主键,autoincrement设置是否自增 114 @Property(nameInDb = "ID")//设置该字段在数据表中的名字,当字段设置为主键时候,那么该字段必须为Long型 115 public Long Id; 116 @NotNull 117 @Property(nameInDb = "AGE") 118 public int Age; 119 @NotNull 120 @Property(nameInDb = "NAME") 121 public String Name; 122 @Transient//表示在创建表是否会忽略掉这个字段 123 public String TransientName; 124 @org.greenrobot.greendao.annotation.ToOne(joinProperty = "Id")//设置多对一关系,即一条该实体数据对应多条ToOne数据的关系 125 public ToOne One; 126 @org.greenrobot.greendao.annotation.ToMany(referencedJoinProperty = "Age",joinProperties ={ 127 @JoinProperty( referencedName ="Age",name = "Age")//建立多表连接,多对多的关系,当前表的name表示的字段和referencedName 128 //说表示的另外一个表的字段关联查询 129 }) 130 public List<ToMany> mToManyList; 131 @Property(nameInDb = "TEST") 132 @Convert( converter = com.example.administrator.myapplication.Convert.class,columnType = String.class)//字段转换器,将实体数据装换为数据表中的数据 133 public Entity test; 134 /** Used to resolve relations */ 135 @Generated(hash = 2040040024) 136 private transient DaoSession daoSession; 137 /** Used for active entity operations. */ 138 @Generated(hash = 1943931642) 139 private transient StudentDao myDao; 140 @Generated(hash = 510098685) 141 public Student(Long Id, int Age, @NotNull String Name, Entity test) { 142 this.Id = Id; 143 this.Age = Age; 144 this.Name = Name; 145 this.test = test; 146 } 147 @Generated(hash = 1556870573) 148 public Student() { 149 } 150 public Long getId() { 151 return this.Id; 152 } 153 public void setId(Long Id) { 154 this.Id = Id; 155 } 156 public int getAge() { 157 return this.Age; 158 } 159 public void setAge(int Age) { 160 this.Age = Age; 161 } 162 public String getName() { 163 return this.Name; 164 } 165 public void setName(String Name) { 166 this.Name = Name; 167 } 168 public com.example.administrator.myapplication.Entity getTest() { 169 return this.test; 170 } 171 public void setTest(Entity test) { 172 this.test = test; 173 } 174 @Generated(hash = 907378687) 175 private transient Long One__resolvedKey; 176 /** To-one relationship, resolved on first access. */ 177 @Generated(hash = 1410041857) 178 public ToOne getOne() { 179 Long __key = this.Id; 180 if (One__resolvedKey == null || !One__resolvedKey.equals(__key)) { 181 final DaoSession daoSession = this.daoSession; 182 if (daoSession == null) { 183 throw new DaoException("Entity is detached from DAO context"); 184 } 185 ToOneDao targetDao = daoSession.getToOneDao(); 186 ToOne OneNew = targetDao.load(__key); 187 synchronized (this) { 188 One = OneNew; 189 One__resolvedKey = __key; 190 } 191 } 192 return One; 193 } 194 /** called by internal mechanisms, do not call yourself. */ 195 @Generated(hash = 2129737595) 196 public void setOne(ToOne One) { 197 synchronized (this) { 198 this.One = One; 199 Id = One == null ? null : One.getId(); 200 One__resolvedKey = Id; 201 } 202 } 203 /** 204 * To-many relationship, resolved on first access (and after reset). 205 * Changes to to-many relations are not persisted, make changes to the target entity. 206 */ 207 @Generated(hash = 2087991423) 208 public List<ToMany> getMToManyList() { 209 if (mToManyList == null) { 210 final DaoSession daoSession = this.daoSession; 211 if (daoSession == null) { 212 throw new DaoException("Entity is detached from DAO context"); 213 } 214 ToManyDao targetDao = daoSession.getToManyDao(); 215 List<ToMany> mToManyListNew = targetDao._queryStudent_MToManyList(Age); 216 synchronized (this) { 217 if (mToManyList == null) { 218 mToManyList = mToManyListNew; 219 } 220 } 221 } 222 return mToManyList; 223 } 224 /** Resets a to-many relationship, making the next get call to query for a fresh result. */ 225 @Generated(hash = 1358693666) 226 public synchronized void resetMToManyList() { 227 mToManyList = null; 228 } 229 /** 230 * Convenient call for {@link org.greenrobot.greendao.AbstractDao#delete(Object)}. 231 * Entity must attached to an entity context. 232 */ 233 @Generated(hash = 128553479) 234 public void delete() { 235 if (myDao == null) { 236 throw new DaoException("Entity is detached from DAO context"); 237 } 238 myDao.delete(this); 239 } 240 /** 241 * Convenient call for {@link org.greenrobot.greendao.AbstractDao#refresh(Object)}. 242 * Entity must attached to an entity context. 243 */ 244 @Generated(hash = 1942392019) 245 public void refresh() { 246 if (myDao == null) { 247 throw new DaoException("Entity is detached from DAO context"); 248 } 249 myDao.refresh(this); 250 } 251 /** 252 * Convenient call for {@link org.greenrobot.greendao.AbstractDao#update(Object)}. 253 * Entity must attached to an entity context. 254 */ 255 @Generated(hash = 713229351) 256 public void update() { 257 if (myDao == null) { 258 throw new DaoException("Entity is detached from DAO context"); 259 } 260 myDao.update(this); 261 } 262 /** called by internal mechanisms, do not call yourself. */ 263 @Generated(hash = 1701634981) 264 public void __setDaoSession(DaoSession daoSession) { 265 this.daoSession = daoSession; 266 myDao = daoSession != null ? daoSession.getStudentDao() : null; 267 } 268} 269 270 271 272package com.example.administrator.myapplication; 273 274import org.greenrobot.greendao.annotation.Entity; 275import org.greenrobot.greendao.annotation.Index; 276import org.greenrobot.greendao.annotation.NotNull; 277import org.greenrobot.greendao.annotation.Property; 278import org.greenrobot.greendao.annotation.Unique; 279import org.greenrobot.greendao.annotation.Generated; 280import org.greenrobot.greendao.DaoException; 281 282/** 283 * Created by Administrator on 2017/5/26. 284 */ 285@Entity( 286 nameInDb = "TOMANY" 287 ,indexes = {@Index(value = "Id ASC")} 288 ,active = true) 289public class ToMany { 290 @org.greenrobot.greendao.annotation.Id(autoincrement = true) 291 @NotNull 292 @Unique 293 @Property(nameInDb = "ID") 294 public Long Id; 295 @Property(nameInDb = "NAME") 296 @NotNull 297 public String Name; 298 @Property(nameInDb = "AGE") 299 @NotNull 300 public int Age; 301 /** Used to resolve relations */ 302 @Generated(hash = 2040040024) 303 private transient DaoSession daoSession; 304 /** Used for active entity operations. */ 305 @Generated(hash = 1963614772) 306 private transient ToManyDao myDao; 307 @Generated(hash = 695029563) 308 public ToMany(@NotNull Long Id, @NotNull String Name, int Age) { 309 this.Id = Id; 310 this.Name = Name; 311 this.Age = Age; 312 } 313 @Generated(hash = 1463867877) 314 public ToMany() { 315 } 316 public Long getId() { 317 return this.Id; 318 } 319 public void setId(Long Id) { 320 this.Id = Id; 321 } 322 public String getName() { 323 return this.Name; 324 } 325 public void setName(String Name) { 326 this.Name = Name; 327 } 328 public int getAge() { 329 return this.Age; 330 } 331 public void setAge(int Age) { 332 this.Age = Age; 333 } 334 /** 335 * Convenient call for {@link org.greenrobot.greendao.AbstractDao#delete(Object)}. 336 * Entity must attached to an entity context. 337 */ 338 @Generated(hash = 128553479) 339 public void delete() { 340 if (myDao == null) { 341 throw new DaoException("Entity is detached from DAO context"); 342 } 343 myDao.delete(this); 344 } 345 /** 346 * Convenient call for {@link org.greenrobot.greendao.AbstractDao#refresh(Object)}. 347 * Entity must attached to an entity context. 348 */ 349 @Generated(hash = 1942392019) 350 public void refresh() { 351 if (myDao == null) { 352 throw new DaoException("Entity is detached from DAO context"); 353 } 354 myDao.refresh(this); 355 } 356 /** 357 * Convenient call for {@link org.greenrobot.greendao.AbstractDao#update(Object)}. 358 * Entity must attached to an entity context. 359 */ 360 @Generated(hash = 713229351) 361 public void update() { 362 if (myDao == null) { 363 throw new DaoException("Entity is detached from DAO context"); 364 } 365 myDao.update(this); 366 } 367 /** called by internal mechanisms, do not call yourself. */ 368 @Generated(hash = 1942134982) 369 public void __setDaoSession(DaoSession daoSession) { 370 this.daoSession = daoSession; 371 myDao = daoSession != null ? daoSession.getToManyDao() : null; 372 } 373} 374 375 376 377 378package com.example.administrator.myapplication; 379 380import org.greenrobot.greendao.annotation.Entity; 381import org.greenrobot.greendao.annotation.Index; 382import org.greenrobot.greendao.annotation.NotNull; 383import org.greenrobot.greendao.annotation.Property; 384import org.greenrobot.greendao.annotation.Unique; 385import org.greenrobot.greendao.annotation.Generated; 386import org.greenrobot.greendao.DaoException; 387 388/** 389 * Created by Administrator on 2017/5/26. 390 */ 391@Entity( 392 nameInDb = "TOONE" 393 ,indexes = {@Index(value = "Id ASC")} 394 ,active = true 395) 396public class ToOne { 397 @org.greenrobot.greendao.annotation.Id(autoincrement = true) 398 @Property(nameInDb = "ID") 399 @NotNull 400 @Unique 401 public Long Id; 402 @Property(nameInDb = "NAME") 403 @NotNull 404 public String Name; 405 @Property(nameInDb = "AGE") 406 @NotNull 407 public int Age; 408 /** Used to resolve relations */ 409 @Generated(hash = 2040040024) 410 private transient DaoSession daoSession; 411 /** Used for active entity operations. */ 412 @Generated(hash = 705594293) 413 private transient ToOneDao myDao; 414 @Generated(hash = 1228318430) 415 public ToOne(@NotNull Long Id, @NotNull String Name, int Age) { 416 this.Id = Id; 417 this.Name = Name; 418 this.Age = Age; 419 } 420 @Generated(hash = 348073711) 421 public ToOne() { 422 } 423 public Long getId() { 424 return this.Id; 425 } 426 public void setId(Long Id) { 427 this.Id = Id; 428 } 429 public String getName() { 430 return this.Name; 431 } 432 public void setName(String Name) { 433 this.Name = Name; 434 } 435 public int getAge() { 436 return this.Age; 437 } 438 public void setAge(int Age) { 439 this.Age = Age; 440 } 441 /** 442 * Convenient call for {@link org.greenrobot.greendao.AbstractDao#delete(Object)}. 443 * Entity must attached to an entity context. 444 */ 445 @Generated(hash = 128553479) 446 public void delete() { 447 if (myDao == null) { 448 throw new DaoException("Entity is detached from DAO context"); 449 } 450 myDao.delete(this); 451 } 452 /** 453 * Convenient call for {@link org.greenrobot.greendao.AbstractDao#refresh(Object)}. 454 * Entity must attached to an entity context. 455 */ 456 @Generated(hash = 1942392019) 457 public void refresh() { 458 if (myDao == null) { 459 throw new DaoException("Entity is detached from DAO context"); 460 } 461 myDao.refresh(this); 462 } 463 /** 464 * Convenient call for {@link org.greenrobot.greendao.AbstractDao#update(Object)}. 465 * Entity must attached to an entity context. 466 */ 467 @Generated(hash = 713229351) 468 public void update() { 469 if (myDao == null) { 470 throw new DaoException("Entity is detached from DAO context"); 471 } 472 myDao.update(this); 473 } 474 /** called by internal mechanisms, do not call yourself. */ 475 @Generated(hash = 1862744651) 476 public void __setDaoSession(DaoSession daoSession) { 477 this.daoSession = daoSession; 478 myDao = daoSession != null ? daoSession.getToOneDao() : null; 479 } 480} 481 482 483 484package com.example.administrator.myapplication; 485 486import org.greenrobot.greendao.annotation.Entity; 487import org.greenrobot.greendao.annotation.Index; 488import org.greenrobot.greendao.annotation.NotNull; 489import org.greenrobot.greendao.annotation.Property; 490import org.greenrobot.greendao.annotation.Unique; 491import org.greenrobot.greendao.annotation.Generated; 492import org.greenrobot.greendao.DaoException; 493 494/** 495 * Created by Administrator on 2017/5/25. 496 */ 497@Entity( 498 nameInDb = "USER" 499 ,active = true 500 ,indexes = {@Index(value = "Id ASC")} 501) 502public class User { 503 @org.greenrobot.greendao.annotation.Id(autoincrement = true) 504 @Property(nameInDb = "ID") 505 @NotNull 506 @Unique 507 public Long Id; 508 @Property(nameInDb = "NAME") 509 @NotNull 510 public String Name; 511 @Property(nameInDb = "AGE") 512 @NotNull 513 public int Age; 514 /** Used to resolve relations */ 515 @Generated(hash = 2040040024) 516 private transient DaoSession daoSession; 517 /** Used for active entity operations. */ 518 @Generated(hash = 1507654846) 519 private transient UserDao myDao; 520 @Generated(hash = 1925281583) 521 public User(@NotNull Long Id, @NotNull String Name, int Age) { 522 this.Id = Id; 523 this.Name = Name; 524 this.Age = Age; 525 } 526 @Generated(hash = 586692638) 527 public User() { 528 } 529 public Long getId() { 530 return this.Id; 531 } 532 public void setId(Long Id) { 533 this.Id = Id; 534 } 535 public String getName() { 536 return this.Name; 537 } 538 public void setName(String Name) { 539 this.Name = Name; 540 } 541 public int getAge() { 542 return this.Age; 543 } 544 public void setAge(int Age) { 545 this.Age = Age; 546 } 547 /** 548 * Convenient call for {@link org.greenrobot.greendao.AbstractDao#delete(Object)}. 549 * Entity must attached to an entity context. 550 */ 551 @Generated(hash = 128553479) 552 public void delete() { 553 if (myDao == null) { 554 throw new DaoException("Entity is detached from DAO context"); 555 } 556 myDao.delete(this); 557 } 558 /** 559 * Convenient call for {@link org.greenrobot.greendao.AbstractDao#refresh(Object)}. 560 * Entity must attached to an entity context. 561 */ 562 @Generated(hash = 1942392019) 563 public void refresh() { 564 if (myDao == null) { 565 throw new DaoException("Entity is detached from DAO context"); 566 } 567 myDao.refresh(this); 568 } 569 /** 570 * Convenient call for {@link org.greenrobot.greendao.AbstractDao#update(Object)}. 571 * Entity must attached to an entity context. 572 */ 573 @Generated(hash = 713229351) 574 public void update() { 575 if (myDao == null) { 576 throw new DaoException("Entity is detached from DAO context"); 577 } 578 myDao.update(this); 579 } 580 /** called by internal mechanisms, do not call yourself. */ 581 @Generated(hash = 2059241980) 582 public void __setDaoSession(DaoSession daoSession) { 583 this.daoSession = daoSession; 584 myDao = daoSession != null ? daoSession.getUserDao() : null; 585 } 586} 587 588 589 590 5913,目前删除数据delete系列的方法只支持有主键的数据表,没主键的数据表目前只能通过sql语句来删除. 592 593 594 5954,Insert系列方法,如果设置了主键并且主键为null则GreenDao会自动为我们添加设置主键(自增方式); 596 5975,GreenDao缓存实现是将查询返回的结果保存到数组中如果有就从缓存的数组中查找,没有就从查询结果返回的游标中查找. 598 599以Query.List()方法为例 600 601 602public List<T> list() { 603 //检查线程是否是同一线程,如果不是就抛出异常 604 checkThread(); 605 //查询返回游标,一般情况下这个返回的游标是CrossProcessCursor对象 606 607 Cursor cursor = dao.getDatabase().rawQuery(sql, parameters); 608 return daoAccess.loadAllAndCloseCursor(cursor); 609 610} 611 612接着跳到 613 614 615public List<T> loadAllAndCloseCursor(Cursor cursor) { 616 return dao.loadAllAndCloseCursor(cursor); 617} 618 619最终到 620 621protected List<T> loadAllFromCursor(Cursor cursor) { 622 int count = cursor.getCount(); 623 if (count == 0) { 624 return new ArrayList<T>(); 625 } 626 List<T> list = new ArrayList<T>(count); 627 CursorWindow window = null; 628 boolean useFastCursor = false; 629 //如果是CrossProcessCursor对象 630 if (cursor instanceof CrossProcessCursor) { 631 window = ((CrossProcessCursor) cursor).getWindow(); 632 if (window != null) { // E.g. Robolectric has no Window at this point 633 //如果两者数量相等,就将useFastCursor置为true 634 if (window.getNumRows() == count) { 635 cursor = new FastCursor(window); 636 useFastCursor = true; 637 } else { 638 DaoLog.d("Window vs. result size: " + window.getNumRows() + "/" + count); 639 } 640 } 641 } 642 643 if (cursor.moveToFirst()) { 644 if (identityScope != null) { 645 identityScope.lock(); 646 identityScope.reserveRoom(count); 647 } 648 649 try { 650 //useFastCursor为false,IdentityScopeType置为IdentityScopeType.Session表示使用缓存 651 //这个是否useFastCursor就为true 652 if (!useFastCursor && window != null && identityScope != null) { 653 //如果不适用就转到这个方法 654 loadAllUnlockOnWindowBounds(cursor, window, list); 655 } else { 656 do { 657 //否则转到这个方法 658 list.add(loadCurrent(cursor, 0, false)); 659 } while (cursor.moveToNext()); 660 } 661 } finally { 662 if (identityScope != null) { 663 identityScope.unlock(); 664 } 665 } 666 } 667 return list; 668} 669 670 671final protected T loadCurrent(Cursor cursor, int offset, boolean lock) { 672 //如果identityScopeLong不为null,表示优先使用缓存中的数据 673 if (identityScopeLong != null) { 674 if (offset != 0) { 675 // Occurs with deep loads (left outer joins) 676 if (cursor.isNull(pkOrdinal + offset)) { 677 return null; 678 } 679 } 680 681 long key = cursor.getLong(pkOrdinal + offset); 682 T entity = lock ? identityScopeLong.get2(key) : identityScopeLong.get2NoLock(key); 683 if (entity != null) { 684 return entity; 685 } else { 686 entity = readEntity(cursor, offset); 687 attachEntity(entity); 688 if (lock) { 689 identityScopeLong.put2(key, entity); 690 } else { 691 identityScopeLong.put2NoLock(key, entity); 692 } 693 return entity; 694 } 695 } else if (identityScope != null) { 696 K key = readKey(cursor, offset); 697 if (offset != 0 && key == null) { 698 // Occurs with deep loads (left outer joins) 699 return null; 700 } 701 T entity = lock ? identityScope.get(key) : identityScope.getNoLock(key); 702 if (entity != null) { 703 return entity; 704 } else { 705 entity = readEntity(cursor, offset); 706 attachEntity(key, entity, lock); 707 return entity; 708 } 709 } else { 710 // Check offset, assume a value !=0 indicating a potential outer join, so check PK 711 if (offset != 0) { 712 K key = readKey(cursor, offset); 713 if (key == null) { 714 // Occurs with deep loads (left outer joins) 715 return null; 716 } 717 } 718 T entity = readEntity(cursor, offset); 719 attachEntity(entity); 720 return entity; 721 } 722} 723 7246,如果想清除指定表缓存,调用 725 726DaoConfig ().clearIdentityScope (); 727 728或者 729 730AbstractDao.detachAll() 731 732 733/** 734 * Detaches all entities (of type T) from the identity scope (session). Subsequent query results won't return any 735 * previously loaded objects. 736 */ 737public void detachAll() { 738 if (identityScope != null) { 739 identityScope.clear(); 740 } 741} 742 743 744,如果想清除所有表的缓存调用 745 746DaoSession.clear () 747 748如果想在查询返回结果中多返回几条数据库中没有的数据条数可以调用,可以按照这样做 749 750protected final void attachEntity(K key, T entity, boolean lock) { 751 attachEntity(entity); 752 if (identityScope != null && key != null) { 753 if (lock) { 754 identityScope.put(key, entity); 755 } else { 756 identityScope.putNoLock(key, entity); 757 } 758 } 759} 760 761,如果想在查询结果中返回少几条指定数据,但是不删除数据表中的数据记录可以这样做 762 763/** Detaches an entity from the identity scope (session). Subsequent query results won't return this object. */ 764public boolean detach(T entity) { 765 if (identityScope != null) { 766 K key = getKeyVerified(entity); 767 return identityScope.detach(key, entity); 768 } else { 769 return false; 770 } 771} 772 773以上只针对设置启用数据库缓存的情况 774 7757,AbstractDao.refresh(T entity)是将entity实体恢复成数据表里的状态(如果启用了缓存也把缓存中的记录重置); 776 777AbstractDao.update(T entity)将对改实体的改动更新到数据表(如果启用了缓存,也把缓存中的记录更新); 778 779AbstractDao.delete(T entity)将该实体对应的数据表记录删除掉(如果启用了缓存,也把缓存中的记录删除掉); 780 781AbstractDao.save系列方法,如果数据库中存在该条数据就更新,如果不存在就添加. 782 7838,GreenDao的delete系列删除数据记录的方法目前只支持有主键的数据表; 784 7859,GreenDao也支持Rx操作,例如 786 787 788 RxDao<Student,Long> rxDAO=new RxDao < Student, Long > ( mDaoSession.getStudentDao ()); 789rxDAO.insertInTx ( sStudents ).subscribe ( new Action1 < Iterable < Student > > ( ) { 790 @Override 791 public void call ( Iterable < Student > pStudents ) { 792 793 } 794} );
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )