From 6b84523ec00139dc6a34625e9a0e2e3d6516cff3 Mon Sep 17 00:00:00 2001 From: SYH <1017401752@qq.com> Date: Mon, 16 Dec 2024 23:17:19 +0800 Subject: [PATCH] 1 --- .../monkeybook/dao/BookContentBeanDao.java | 98 +++++--- .../monke/monkeybook/dao/BookInfoBeanDao.java | 212 +++++++++++------- .../monkeybook/dao/BookShelfBeanDao.java | 137 ++++++----- .../monkeybook/dao/ChapterListBeanDao.java | 148 +++++++----- .../com/monke/monkeybook/dao/DaoMaster.java | 89 +++++--- .../com/monke/monkeybook/dao/DaoSession.java | 159 ++++++------- 6 files changed, 522 insertions(+), 321 deletions(-) diff --git a/app/src/main/java/com/monke/monkeybook/dao/BookContentBeanDao.java b/app/src/main/java/com/monke/monkeybook/dao/BookContentBeanDao.java index 6e40f73..92c5d2f 100644 --- a/app/src/main/java/com/monke/monkeybook/dao/BookContentBeanDao.java +++ b/app/src/main/java/com/monke/monkeybook/dao/BookContentBeanDao.java @@ -1,28 +1,30 @@ +//指定了当前类BookContentBeanDao所在的包路径,即com.monke.monkeybook.dao package com.monke.monkeybook.dao; - +//导入必要的类,包含GreenDAO、SQLite和Android数据库操作的相关类 import android.database.Cursor; import android.database.sqlite.SQLiteStatement; - import org.greenrobot.greendao.AbstractDao; import org.greenrobot.greendao.Property; import org.greenrobot.greendao.internal.DaoConfig; import org.greenrobot.greendao.database.Database; import org.greenrobot.greendao.database.DatabaseStatement; - +// 导入实体类 BookContentBean import com.monke.monkeybook.bean.BookContentBean; // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. -/** +/** * DAO for table "BOOK_CONTENT_BEAN". -*/ + */ +//继承自GreenDAO的AbstractDao类,BookContentBean为实体类,String为主键类型 public class BookContentBeanDao extends AbstractDao { - + //定义常量TABLENAME,表示该表的名称 public static final String TABLENAME = "BOOK_CONTENT_BEAN"; - /** * Properties of entity BookContentBean.
* Can be used for QueryBuilder and for referencing column names. - */ + * BookContentBean是实体类的属性,可以用于QueryBuilder以及引用数据库表字段名。 + */ + // 定义实体类属性映射:对应数据库表字段名的属性 public static class Properties { public final static Property DurChapterUrl = new Property(0, String.class, "durChapterUrl", true, "DUR_CHAPTER_URL"); public final static Property DurChapterIndex = new Property(1, int.class, "durChapterIndex", false, "DUR_CHAPTER_INDEX"); @@ -30,18 +32,24 @@ public class BookContentBeanDao extends AbstractDao { public final static Property Tag = new Property(3, String.class, "tag", false, "TAG"); }; - + //构造函数,传入DaoConfig,用于初始化DAO public BookContentBeanDao(DaoConfig config) { - super(config); + super(config); //调用父类构造函数,初始化Dao配置 } - + + // 构造函数,传入DaoConfig和DaoSession,用于初始化DAO和会话管理 public BookContentBeanDao(DaoConfig config, DaoSession daoSession) { - super(config, daoSession); + super(config, daoSession); //调用父类构造函数,初始化Dao配置和会话 } - /** Creates the underlying database table. */ + /** + * Creates the underlying database table. + * 创建数据库表 + */ public static void createTable(Database db, boolean ifNotExists) { + //如果ifNotExists为true,则在创建表时确保该表不存在 String constraint = ifNotExists? "IF NOT EXISTS ": ""; + //使用SQL执行表创建语句 db.execSQL("CREATE TABLE " + constraint + "\"BOOK_CONTENT_BEAN\" (" + // "\"DUR_CHAPTER_URL\" TEXT PRIMARY KEY NOT NULL ," + // 0: durChapterUrl "\"DUR_CHAPTER_INDEX\" INTEGER NOT NULL ," + // 1: durChapterIndex @@ -49,85 +57,110 @@ public class BookContentBeanDao extends AbstractDao { "\"TAG\" TEXT);"); // 3: tag } - /** Drops the underlying database table. */ + /** Drops the underlying database table. + * 删除数据库表 + */ public static void dropTable(Database db, boolean ifExists) { + //使用SQL删除表,确保表存在时删除 String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"BOOK_CONTENT_BEAN\""; db.execSQL(sql); } @Override protected final void bindValues(DatabaseStatement stmt, BookContentBean entity) { + //清除绑定的值,确保每次绑定时都是干净的 stmt.clearBindings(); - + + //绑定durChapterUrl字段到SQL语句的第一个位置 String durChapterUrl = entity.getDurChapterUrl(); if (durChapterUrl != null) { + //将章节URL字符串绑定到SQL语句的第一个位置 stmt.bindString(1, durChapterUrl); } - stmt.bindLong(2, entity.getDurChapterIndex()); - + + //绑定durChapterIndex字段到SQL语句的第二个位置 + stmt.bindLong(2, entity.getDurChapterIndex());//将章节索引绑定到SQL语句的第二个位置 + + //绑定durCapterContent字段到SQL语句的第三个位置 String durCapterContent = entity.getDurCapterContent(); if (durCapterContent != null) { + //将章节内容绑定到SQL语句的第三个位置 stmt.bindString(3, durCapterContent); } - + + //绑定tag字段到SQL语句的第四个位置 String tag = entity.getTag(); if (tag != null) { + //将标签绑定到SQL语句的第四个位置 stmt.bindString(4, tag); } } @Override protected final void bindValues(SQLiteStatement stmt, BookContentBean entity) { + //清除绑定的值,确保每次绑定时都是干净的 stmt.clearBindings(); - + + //绑定durChapterUrl字段到SQL语句的第一个位置 String durChapterUrl = entity.getDurChapterUrl(); if (durChapterUrl != null) { + //将章节URL字符串绑定到SQL语句的第一个位置 stmt.bindString(1, durChapterUrl); } - stmt.bindLong(2, entity.getDurChapterIndex()); - + //绑定durChapterIndex字段到SQL语句的第二个位置 + stmt.bindLong(2, entity.getDurChapterIndex()); //将章节索引绑定到SQL语句的第二个位置 + + //绑定durCapterContent字段到SQL语句的第三个位置 String durCapterContent = entity.getDurCapterContent(); if (durCapterContent != null) { + //将章节内容绑定到SQL语句的第三个位置 stmt.bindString(3, durCapterContent); } - + + //绑定tag字段到SQL语句的第四个位置 String tag = entity.getTag(); if (tag != null) { + //将标签绑定到SQL语句的第四个位置 stmt.bindString(4, tag); } } @Override public String readKey(Cursor cursor, int offset) { + //从Cursor中读取主键(durChapterUrl),若为null则返回null return cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0); - } + } @Override public BookContentBean readEntity(Cursor cursor, int offset) { + //根据Cursor数据构建一个BookContentBean实体类对象,并返回 BookContentBean entity = new BookContentBean( // - cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0), // durChapterUrl - cursor.getInt(offset + 1), // durChapterIndex - cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // durCapterContent - cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3) // tag + cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0), // durChapterUrl + cursor.getInt(offset + 1), // durChapterIndex + cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // durCapterContent + cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3) // tag ); return entity; } - + @Override public void readEntity(Cursor cursor, BookContentBean entity, int offset) { + //从Cursor中读取值并设置到BookContentBean对象的相应属性中 entity.setDurChapterUrl(cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0)); entity.setDurChapterIndex(cursor.getInt(offset + 1)); entity.setDurCapterContent(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2)); entity.setTag(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3)); - } - + } + @Override protected final String updateKeyAfterInsert(BookContentBean entity, long rowId) { + //返回更新时需要的主键字段,这里是durChapterUrl return entity.getDurChapterUrl(); } - + @Override public String getKey(BookContentBean entity) { + //获取实体对象的主键值,返回durChapterUrl if(entity != null) { return entity.getDurChapterUrl(); } else { @@ -137,7 +170,8 @@ public class BookContentBeanDao extends AbstractDao { @Override protected final boolean isEntityUpdateable() { + //指示实体是否可以被更新,这里返回true,表示支持更新 return true; } - + } diff --git a/app/src/main/java/com/monke/monkeybook/dao/BookInfoBeanDao.java b/app/src/main/java/com/monke/monkeybook/dao/BookInfoBeanDao.java index 3e17cf5..ac4edfb 100644 --- a/app/src/main/java/com/monke/monkeybook/dao/BookInfoBeanDao.java +++ b/app/src/main/java/com/monke/monkeybook/dao/BookInfoBeanDao.java @@ -1,156 +1,202 @@ +//定义类所在的包路径,表示该文件属于com.monke.monkeybook.dao包 package com.monke.monkeybook.dao; -import android.database.Cursor; -import android.database.sqlite.SQLiteStatement; +//导入必要的类,包含GreenDAO和Android数据库操作的相关类 +import android.database.Cursor;// 导入Cursor类,用于数据库查询结果的处理 +import android.database.sqlite.SQLiteStatement;//导入SQLiteStatement类,用于执行SQL语句 -import org.greenrobot.greendao.AbstractDao; -import org.greenrobot.greendao.Property; -import org.greenrobot.greendao.internal.DaoConfig; -import org.greenrobot.greendao.database.Database; -import org.greenrobot.greendao.database.DatabaseStatement; +import org.greenrobot.greendao.AbstractDao;//导入AbstractDao类,这是一个GreenDao框架的抽象类,用于定义数据库操作的基类 +import org.greenrobot.greendao.Property;//导入Property类,用于定义数据库表中的字段属性 +import org.greenrobot.greendao.internal.DaoConfig;//导入DaoConfig类,用于配置DAO对象 +import org.greenrobot.greendao.database.Database;//导入Database类,用于操作数据库 +import org.greenrobot.greendao.database.DatabaseStatement;//导入BookInfoBean实体类 -import com.monke.monkeybook.bean.BookInfoBean; +import com.monke.monkeybook.bean.BookInfoBean;//导入BookInfoBean类,这是数据库中要操作的实体类 // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. -/** +/** * DAO for table "BOOK_INFO_BEAN". -*/ -public class BookInfoBeanDao extends AbstractDao { + * DAO 类,用于操作 "BOOK_INFO_BEAN" 表 + */ +//BookInfoBeanDao继承自AbstractDao,操作BookInfoBean对象,主键类型为String +public class BookInfoBeanDao extends AbstractDao { + //定义常量TABLENAME,表示数据库表名 public static final String TABLENAME = "BOOK_INFO_BEAN"; /** * Properties of entity BookInfoBean.
* Can be used for QueryBuilder and for referencing column names. - */ + * BookInfoBean 实体类的属性,可以用于 QueryBuilder 以及引用数据库表字段名。 + */ public static class Properties { - public final static Property Name = new Property(0, String.class, "name", false, "NAME"); - public final static Property Tag = new Property(1, String.class, "tag", false, "TAG"); - public final static Property NoteUrl = new Property(2, String.class, "noteUrl", true, "NOTE_URL"); - public final static Property ChapterUrl = new Property(3, String.class, "chapterUrl", false, "CHAPTER_URL"); - public final static Property FinalRefreshData = new Property(4, long.class, "finalRefreshData", false, "FINAL_REFRESH_DATA"); - public final static Property CoverUrl = new Property(5, String.class, "coverUrl", false, "COVER_URL"); - public final static Property Author = new Property(6, String.class, "author", false, "AUTHOR"); - public final static Property Introduce = new Property(7, String.class, "introduce", false, "INTRODUCE"); - public final static Property Origin = new Property(8, String.class, "origin", false, "ORIGIN"); + //定义实体类属性映射:对应数据库表字段名的属性 + public final static Property Name = new Property(0, String.class, "name", false, "NAME");//属性0:名称(name),String类型,非主键,数据库列名NAME + public final static Property Tag = new Property(1, String.class, "tag", false, "TAG");//属性1:标签(tag),String类型,非主键,数据库列名TAG + public final static Property NoteUrl = new Property(2, String.class, "noteUrl", true, "NOTE_URL");// 属性2:笔记URL(noteUrl),String类型,主键,非空,数据库列名NOTE_URL + public final static Property ChapterUrl = new Property(3, String.class, "chapterUrl", false, "CHAPTER_URL");//属性3:章节URL(chapterUrl),String类型,非主键,数据库列名CHAPTER_URL + public final static Property FinalRefreshData = new Property(4, long.class, "finalRefreshData", false, "FINAL_REFRESH_DATA");//属性4:最后刷新时间(finalRefreshData),long类型,非主键,数据库列名FINAL_REFRESH_DATA + public final static Property CoverUrl = new Property(5, String.class, "coverUrl", false, "COVER_URL");//属性5:封面URL(coverUrl),String类型,非主键,数据库列名COVER_URL + public final static Property Author = new Property(6, String.class, "author", false, "AUTHOR");//属性6:作者(author),String类型,非主键,数据库列名AUTHOR + public final static Property Introduce = new Property(7, String.class, "introduce", false, "INTRODUCE");//属性7:简介(introduce),String类型,非主键,数据库列名INTRODUCE + public final static Property Origin = new Property(8, String.class, "origin", false, "ORIGIN");//属性8:来源(origin),String类型,非主键,数据库列名ORIGIN }; - + //构造函数,使用DaoConfig配置创建DAO对象 public BookInfoBeanDao(DaoConfig config) { super(config); } - + + //构造函数,使用DaoConfig和DaoSession配置创建DAO对象 public BookInfoBeanDao(DaoConfig config, DaoSession daoSession) { super(config, daoSession); } - /** Creates the underlying database table. */ + /** Creates the underlying database table. + * 创建数据库表 + */ + //创建数据库表的方法 public static void createTable(Database db, boolean ifNotExists) { + //判断是否需要添加IF NOT EXISTS条件 String constraint = ifNotExists? "IF NOT EXISTS ": ""; - db.execSQL("CREATE TABLE " + constraint + "\"BOOK_INFO_BEAN\" (" + // - "\"NAME\" TEXT," + // 0: name - "\"TAG\" TEXT," + // 1: tag - "\"NOTE_URL\" TEXT PRIMARY KEY NOT NULL ," + // 2: noteUrl - "\"CHAPTER_URL\" TEXT," + // 3: chapterUrl - "\"FINAL_REFRESH_DATA\" INTEGER NOT NULL ," + // 4: finalRefreshData - "\"COVER_URL\" TEXT," + // 5: coverUrl - "\"AUTHOR\" TEXT," + // 6: author - "\"INTRODUCE\" TEXT," + // 7: introduce - "\"ORIGIN\" TEXT);"); // 8: origin + //使用SQL执行表创建语句 + db.execSQL("CREATE TABLE " + constraint + "\"BOOK_INFO_BEAN\" (" + + "\"NAME\" TEXT," + //NAME字段,TEXT类型 + "\"TAG\" TEXT," + //TAG字段,TEXT类型 + "\"NOTE_URL\" TEXT PRIMARY KEY NOT NULL ," + //NOTE_URL字段,TEXT类型,主键,非空 + "\"CHAPTER_URL\" TEXT," + //CHAPTER_URL字段,TEXT类型 + "\"FINAL_REFRESH_DATA\" INTEGER NOT NULL ," + //FINAL_REFRESH_DATA字段,INTEGER类型,非空 + "\"COVER_URL\" TEXT," + //COVER_URL字段,TEXT类型 + "\"AUTHOR\" TEXT," + //AUTHOR字段,TEXT类型 + "\"INTRODUCE\" TEXT," + //INTRODUCE字段,TEXT类型 + "\"ORIGIN\" TEXT);"); //ORIGIN字段,TEXT类型 } - /** Drops the underlying database table. */ + /** Drops the underlying database table. + * 删除底层数据库表 + */ public static void dropTable(Database db, boolean ifExists) { - String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"BOOK_INFO_BEAN\""; - db.execSQL(sql); + //使用SQL删除表,确保表存在时删除 + String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"BOOK_INFO_BEAN\"";//构造删除表的SQL语句 + db.execSQL(sql);//执行删除表的SQL语句 } @Override + //将BookInfoBean实体对象的值绑定到DatabaseStatement对象中 protected final void bindValues(DatabaseStatement stmt, BookInfoBean entity) { + //清除绑定的值,确保每次绑定时都是干净的 stmt.clearBindings(); - + + //绑定name字段到SQL语句的第一个位置 String name = entity.getName(); if (name != null) { + //将书名绑定到SQL语句的第一个位置 stmt.bindString(1, name); } - + + //绑定tag字段到SQL语句的第二个位置 String tag = entity.getTag(); if (tag != null) { + //将标签绑定到SQL语句的第二个位置 stmt.bindString(2, tag); } - + + //绑定noteUrl字段到SQL语句的第三个位置 String noteUrl = entity.getNoteUrl(); if (noteUrl != null) { + //将书籍URL绑定到SQL语句的第三个位置 stmt.bindString(3, noteUrl); } - + + //绑定chapterUrl字段到SQL语句的第四个位置 String chapterUrl = entity.getChapterUrl(); if (chapterUrl != null) { + //将章节URL绑定到SQL语句的第四个位置 stmt.bindString(4, chapterUrl); } + //将最后刷新时间(long 类型)绑定到SQL语句的第五个位置 stmt.bindLong(5, entity.getFinalRefreshData()); - + + //绑定coverUrl字段到SQL语句的第六个位置 String coverUrl = entity.getCoverUrl(); if (coverUrl != null) { + //将封面图URL绑定到SQL语句的第六个位置 stmt.bindString(6, coverUrl); } - + + //绑定author字段到SQL语句的第七个位置 String author = entity.getAuthor(); if (author != null) { + //将作者绑定到SQL语句的第七个位置 stmt.bindString(7, author); } - + + //绑定introduce字段到SQL语句的第八个位置 String introduce = entity.getIntroduce(); if (introduce != null) { + //将简介绑定到SQL语句的第八个位置 stmt.bindString(8, introduce); } - + + //绑定origin字段到SQL语句的第九个位置 String origin = entity.getOrigin(); if (origin != null) { + //将来源绑定到SQL语句的第九个位置 stmt.bindString(9, origin); } } @Override + //将BookInfoBean实体对象的值绑定到SQLiteStatement对象中,与上一个方法功能类似,只是对象不同 protected final void bindValues(SQLiteStatement stmt, BookInfoBean entity) { + //清除绑定的值,确保每次绑定时都是干净的 stmt.clearBindings(); - + + //绑定name字段值 String name = entity.getName(); if (name != null) { stmt.bindString(1, name); } - + + //绑定tag字段值 String tag = entity.getTag(); if (tag != null) { stmt.bindString(2, tag); } - + + //绑定noteUrl字段值 String noteUrl = entity.getNoteUrl(); if (noteUrl != null) { stmt.bindString(3, noteUrl); } - + + //绑定chapterUrl字段值 String chapterUrl = entity.getChapterUrl(); if (chapterUrl != null) { stmt.bindString(4, chapterUrl); } + //绑定finalRefreshData字段值 stmt.bindLong(5, entity.getFinalRefreshData()); - + + //绑定coverUrl字段值 String coverUrl = entity.getCoverUrl(); if (coverUrl != null) { stmt.bindString(6, coverUrl); } - + + //绑定author字段值 String author = entity.getAuthor(); if (author != null) { stmt.bindString(7, author); } - + + //绑定introduce字段值 String introduce = entity.getIntroduce(); if (introduce != null) { stmt.bindString(8, introduce); } - + + //绑定origin字段值 String origin = entity.getOrigin(); if (origin != null) { stmt.bindString(9, origin); @@ -158,47 +204,57 @@ public class BookInfoBeanDao extends AbstractDao { } @Override + //从Cursor中读取主键 public String readKey(Cursor cursor, int offset) { + //主键在第三列(offset + 2),如果为空返回null,否则返回字符串 return cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2); - } + } @Override + //从Cursor中读取BookInfoBean实体 public BookInfoBean readEntity(Cursor cursor, int offset) { + //创建BookInfoBean对象 BookInfoBean entity = new BookInfoBean( // - cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0), // name - cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // tag - cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // noteUrl - cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // chapterUrl - cursor.getLong(offset + 4), // finalRefreshData - cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // coverUrl - cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // author - cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // introduce - cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8) // origin + cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0), // name + cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // tag + cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // noteUrl + cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // chapterUrl + cursor.getLong(offset + 4), // finalRefreshData + cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // coverUrl + cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // author + cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // introduce + cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8) // origin ); + //返回创建好的BookInfoBean对象 return entity; } - + @Override + //将Cursor中的数据读取到已存在的BookInfoBean实体中 public void readEntity(Cursor cursor, BookInfoBean entity, int offset) { - entity.setName(cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0)); - entity.setTag(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1)); - entity.setNoteUrl(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2)); - entity.setChapterUrl(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3)); - entity.setFinalRefreshData(cursor.getLong(offset + 4)); - entity.setCoverUrl(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5)); - entity.setAuthor(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6)); - entity.setIntroduce(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7)); - entity.setOrigin(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8)); - } - + entity.setName(cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0));//name + entity.setTag(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));//tag + entity.setNoteUrl(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));//noteUrl + entity.setChapterUrl(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));//chapterUrl + entity.setFinalRefreshData(cursor.getLong(offset + 4));//finalRefreshData + entity.setCoverUrl(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));//coverUrl + entity.setAuthor(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6));//author + entity.setIntroduce(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));//introduce + entity.setOrigin(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8));//origin + } + @Override + //插入数据后更新主键 protected final String updateKeyAfterInsert(BookInfoBean entity, long rowId) { + //使用noteUrl作为主键 return entity.getNoteUrl(); } - + @Override + //获取主键 public String getKey(BookInfoBean entity) { if(entity != null) { + //返回noteUrl作为主键 return entity.getNoteUrl(); } else { return null; @@ -206,8 +262,10 @@ public class BookInfoBeanDao extends AbstractDao { } @Override + //是否可更新实体 protected final boolean isEntityUpdateable() { + //可更新 return true; } - + } diff --git a/app/src/main/java/com/monke/monkeybook/dao/BookShelfBeanDao.java b/app/src/main/java/com/monke/monkeybook/dao/BookShelfBeanDao.java index ba34fcb..b0eb9f3 100644 --- a/app/src/main/java/com/monke/monkeybook/dao/BookShelfBeanDao.java +++ b/app/src/main/java/com/monke/monkeybook/dao/BookShelfBeanDao.java @@ -1,132 +1,163 @@ +// 包声明,声明该类位于com.monke.monkeybook.dao包下 package com.monke.monkeybook.dao; -import android.database.Cursor; -import android.database.sqlite.SQLiteStatement; +import android.database.Cursor; // 导入Cursor类,用于从数据库游标中读取数据 +import android.database.sqlite.SQLiteStatement; // 导入SQLiteStatement类,用于执行预编译的SQL语句 -import org.greenrobot.greendao.AbstractDao; -import org.greenrobot.greendao.Property; -import org.greenrobot.greendao.internal.DaoConfig; -import org.greenrobot.greendao.database.Database; -import org.greenrobot.greendao.database.DatabaseStatement; +import org.greenrobot.greendao.AbstractDao; // 导入AbstractDao类,GreenDao框架中用于数据库操作的抽象基类 +import org.greenrobot.greendao.Property; // 导入Property类,用于定义数据库表字段的属性 +import org.greenrobot.greendao.internal.DaoConfig; // 导入DaoConfig类,用于配置Dao对象的配置信息 +import org.greenrobot.greendao.database.Database; // 导入Database类,GreenDao框架中用于数据库操作的类 +import org.greenrobot.greendao.database.DatabaseStatement; // 导入DatabaseStatement类,用于执行SQL语句 +// 导入BookShelfBean类,这是数据库操作的对象 import com.monke.monkeybook.bean.BookShelfBean; // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. -/** +/** * DAO for table "BOOK_SHELF_BEAN". -*/ + */ +// BookShelfBeanDao类继承AbstractDao,操作BookShelfBean对象,主键类型为String public class BookShelfBeanDao extends AbstractDao { - + // 数据库表名常量 public static final String TABLENAME = "BOOK_SHELF_BEAN"; /** * Properties of entity BookShelfBean.
* Can be used for QueryBuilder and for referencing column names. - */ + */ public static class Properties { - public final static Property NoteUrl = new Property(0, String.class, "noteUrl", true, "NOTE_URL"); - public final static Property DurChapter = new Property(1, int.class, "durChapter", false, "DUR_CHAPTER"); - public final static Property DurChapterPage = new Property(2, int.class, "durChapterPage", false, "DUR_CHAPTER_PAGE"); - public final static Property FinalDate = new Property(3, long.class, "finalDate", false, "FINAL_DATE"); - public final static Property Tag = new Property(4, String.class, "tag", false, "TAG"); + public final static Property NoteUrl = new Property(0, String.class, "noteUrl", true, "NOTE_URL"); // 属性0:noteUrl,String类型,主键,数据库列名NOTE_URL + public final static Property DurChapter = new Property(1, int.class, "durChapter", false, "DUR_CHAPTER"); // 属性1:durChapter,int类型,非主键,数据库列名DUR_CHAPTER + public final static Property DurChapterPage = new Property(2, int.class, "durChapterPage", false, "DUR_CHAPTER_PAGE"); // 属性2:durChapterPage,int类型,非主键,数据库列名DUR_CHAPTER_PAGE + public final static Property FinalDate = new Property(3, long.class, "finalDate", false, "FINAL_DATE"); // 属性3:finalDate,long类型,非主键,数据库列名FINAL_DATE + public final static Property Tag = new Property(4, String.class, "tag", false, "TAG"); // 属性4:tag,String类型,非主键,数据库列名TAG }; - + // 构造函数,使用DaoConfig配置初始化Dao public BookShelfBeanDao(DaoConfig config) { super(config); } - + + // 构造函数,使用DaoConfig和DaoSession配置初始化Dao public BookShelfBeanDao(DaoConfig config, DaoSession daoSession) { super(config, daoSession); } - /** Creates the underlying database table. */ + /** Creates the underlying database table. + * 创建数据库表 + */ + // 创建BOOK_SHELF_BEAN表的方法 public static void createTable(Database db, boolean ifNotExists) { + // 根据ifNotExists判断是否需要添加"IF NOT EXISTS"条件 String constraint = ifNotExists? "IF NOT EXISTS ": ""; + // 执行SQL语句创建表 db.execSQL("CREATE TABLE " + constraint + "\"BOOK_SHELF_BEAN\" (" + // - "\"NOTE_URL\" TEXT PRIMARY KEY NOT NULL ," + // 0: noteUrl - "\"DUR_CHAPTER\" INTEGER NOT NULL ," + // 1: durChapter - "\"DUR_CHAPTER_PAGE\" INTEGER NOT NULL ," + // 2: durChapterPage - "\"FINAL_DATE\" INTEGER NOT NULL ," + // 3: finalDate - "\"TAG\" TEXT);"); // 4: tag + "\"NOTE_URL\" TEXT PRIMARY KEY NOT NULL ," + //NOTE_URL字段,TEXT类型,主键,非空 + "\"DUR_CHAPTER\" INTEGER NOT NULL ," + // DUR_CHAPTER字段,INTEGER类型,非空 + "\"DUR_CHAPTER_PAGE\" INTEGER NOT NULL ," + // DUR_CHAPTER_PAGE字段,INTEGER类型,非空 + "\"FINAL_DATE\" INTEGER NOT NULL ," + // FINAL_DATE字段,INTEGER类型,非空 + "\"TAG\" TEXT);"); // TAG字段,TEXT类型 } - /** Drops the underlying database table. */ + /** Drops the underlying database table. + * 删除数据库表 + */ + // 删除BOOK_SHELF_BEAN表的方法 public static void dropTable(Database db, boolean ifExists) { + // 构造删除表的SQL语句 String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"BOOK_SHELF_BEAN\""; + // 执行SQL语句删除表 db.execSQL(sql); } @Override + // 将BookShelfBean实体对象的值绑定到DatabaseStatement语句中 protected final void bindValues(DatabaseStatement stmt, BookShelfBean entity) { + // 清除之前的绑定 stmt.clearBindings(); - + String noteUrl = entity.getNoteUrl(); if (noteUrl != null) { + // 绑定noteUrl值到第一个参数位置 stmt.bindString(1, noteUrl); } - stmt.bindLong(2, entity.getDurChapter()); - stmt.bindLong(3, entity.getDurChapterPage()); - stmt.bindLong(4, entity.getFinalDate()); - + stmt.bindLong(2, entity.getDurChapter()); // 绑定durChapter值到第二个参数位置 + stmt.bindLong(3, entity.getDurChapterPage()); // 绑定durChapterPage值到第三个参数位置 + stmt.bindLong(4, entity.getFinalDate()); // 绑定finalDate值到第四个参数位置 + String tag = entity.getTag(); if (tag != null) { - stmt.bindString(5, tag); + stmt.bindString(5, tag); // 绑定tag值到第五个参数位置 } } @Override + // 将BookShelfBean实体对象的值绑定到SQLiteStatement语句中,与上一个方法功能类似,只是对象不同 protected final void bindValues(SQLiteStatement stmt, BookShelfBean entity) { + // 清除之前的绑定 stmt.clearBindings(); - + String noteUrl = entity.getNoteUrl(); if (noteUrl != null) { + // 绑定noteUrl值到第一个参数位置 stmt.bindString(1, noteUrl); } - stmt.bindLong(2, entity.getDurChapter()); - stmt.bindLong(3, entity.getDurChapterPage()); - stmt.bindLong(4, entity.getFinalDate()); - + stmt.bindLong(2, entity.getDurChapter()); // 绑定durChapter值到第二个参数位置 + stmt.bindLong(3, entity.getDurChapterPage()); // 绑定durChapterPage值到第三个参数位置 + stmt.bindLong(4, entity.getFinalDate()); // 绑定finalDate值到第四个参数位置 + String tag = entity.getTag(); if (tag != null) { + // 绑定tag值到第五个参数位置 stmt.bindString(5, tag); } } @Override + // 从游标中读取主键 public String readKey(Cursor cursor, int offset) { + // 从偏移量offset+0的位置读取主键值,如果为空返回null,否则返回字符串 return cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0); - } + } @Override + // 从游标中读取BookShelfBean实体 public BookShelfBean readEntity(Cursor cursor, int offset) { + // 创建BookShelfBean对象 BookShelfBean entity = new BookShelfBean( // - cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0), // noteUrl - cursor.getInt(offset + 1), // durChapter - cursor.getInt(offset + 2), // durChapterPage - cursor.getLong(offset + 3), // finalDate - cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4) // tag + cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0), // noteUrl + cursor.getInt(offset + 1), // durChapter + cursor.getInt(offset + 2), // durChapterPage + cursor.getLong(offset + 3), // finalDate + cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4) // tag ); + // 返回创建好的BookShelfBean对象 return entity; } - + @Override + // 将游标中的数据读取到已存在的BookShelfBean实体中 public void readEntity(Cursor cursor, BookShelfBean entity, int offset) { - entity.setNoteUrl(cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0)); - entity.setDurChapter(cursor.getInt(offset + 1)); - entity.setDurChapterPage(cursor.getInt(offset + 2)); - entity.setFinalDate(cursor.getLong(offset + 3)); - entity.setTag(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4)); - } - + entity.setNoteUrl(cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0)); // noteUrl + entity.setDurChapter(cursor.getInt(offset + 1)); // durChapter + entity.setDurChapterPage(cursor.getInt(offset + 2)); // durChapterPage + entity.setFinalDate(cursor.getLong(offset + 3)); // finalDate + entity.setTag(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4)); // tag + } + @Override + // 插入数据后更新主键 protected final String updateKeyAfterInsert(BookShelfBean entity, long rowId) { + // 返回noteUrl作为主键 return entity.getNoteUrl(); } - + @Override + // 获取主键 public String getKey(BookShelfBean entity) { if(entity != null) { + // 返回noteUrl作为主键 return entity.getNoteUrl(); } else { return null; @@ -134,8 +165,10 @@ public class BookShelfBeanDao extends AbstractDao { } @Override + // 判断实体是否可更新 protected final boolean isEntityUpdateable() { + // 返回true,表示实体可更新 return true; } - + } diff --git a/app/src/main/java/com/monke/monkeybook/dao/ChapterListBeanDao.java b/app/src/main/java/com/monke/monkeybook/dao/ChapterListBeanDao.java index d883f64..fc4de8e 100644 --- a/app/src/main/java/com/monke/monkeybook/dao/ChapterListBeanDao.java +++ b/app/src/main/java/com/monke/monkeybook/dao/ChapterListBeanDao.java @@ -1,162 +1,200 @@ +// 包声明 package com.monke.monkeybook.dao; -import android.database.Cursor; -import android.database.sqlite.SQLiteStatement; +import android.database.Cursor; // 导入Cursor类,用于从数据库游标中读取数据 +import android.database.sqlite.SQLiteStatement; // 导入SQLiteStatement类,用于执行预编译的SQL语句 -import org.greenrobot.greendao.AbstractDao; -import org.greenrobot.greendao.Property; -import org.greenrobot.greendao.internal.DaoConfig; -import org.greenrobot.greendao.database.Database; -import org.greenrobot.greendao.database.DatabaseStatement; +import org.greenrobot.greendao.AbstractDao; // 导入AbstractDao类,GreenDao框架中用于数据库操作的抽象基类 +import org.greenrobot.greendao.Property; // 导入Property类,用于定义数据库表字段的属性 +import org.greenrobot.greendao.internal.DaoConfig; // 导入DaoConfig类,用于配置Dao对象的配置信息 +import org.greenrobot.greendao.database.Database; // 导入Database类,GreenDao框架中用于数据库操作的类 +import org.greenrobot.greendao.database.DatabaseStatement; // 导入DatabaseStatement类,用于执行SQL语句 +// 导入ChapterListBean类,这是数据库操作的对象 import com.monke.monkeybook.bean.ChapterListBean; // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. -/** +/** * DAO for table "CHAPTER_LIST_BEAN". -*/ + */ +// ChapterListBeanDao类继承AbstractDao,操作ChapterListBean对象,主键类型为String public class ChapterListBeanDao extends AbstractDao { - + // 数据库表名常量 public static final String TABLENAME = "CHAPTER_LIST_BEAN"; /** * Properties of entity ChapterListBean.
* Can be used for QueryBuilder and for referencing column names. - */ + */ public static class Properties { - public final static Property NoteUrl = new Property(0, String.class, "noteUrl", false, "NOTE_URL"); - public final static Property DurChapterIndex = new Property(1, int.class, "durChapterIndex", false, "DUR_CHAPTER_INDEX"); - public final static Property DurChapterUrl = new Property(2, String.class, "durChapterUrl", true, "DUR_CHAPTER_URL"); - public final static Property DurChapterName = new Property(3, String.class, "durChapterName", false, "DUR_CHAPTER_NAME"); - public final static Property Tag = new Property(4, String.class, "tag", false, "TAG"); - public final static Property HasCache = new Property(5, Boolean.class, "hasCache", false, "HAS_CACHE"); + public final static Property NoteUrl = new Property(0, String.class, "noteUrl", false, "NOTE_URL"); // 属性0:noteUrl,String类型,非主键,数据库列名NOTE_URL + public final static Property DurChapterIndex = new Property(1, int.class, "durChapterIndex", false, "DUR_CHAPTER_INDEX"); // 属性1:durChapterIndex,int类型,非主键,数据库列名DUR_CHAPTER_INDEX + public final static Property DurChapterUrl = new Property(2, String.class, "durChapterUrl", true, "DUR_CHAPTER_URL"); // 属性2:durChapterUrl,String类型,主键,数据库列名DUR_CHAPTER_URL + public final static Property DurChapterName = new Property(3, String.class, "durChapterName", false, "DUR_CHAPTER_NAME"); // 属性3:durChapterName,String类型,非主键,数据库列名DUR_CHAPTER_NAME + public final static Property Tag = new Property(4, String.class, "tag", false, "TAG"); // 属性4:tag,String类型,非主键,数据库列名TAG + public final static Property HasCache = new Property(5, Boolean.class, "hasCache", false, "HAS_CACHE"); // 属性5:hasCache,Boolean类型,非主键,数据库列名HAS_CACHE }; - + // 构造函数,使用DaoConfig配置初始化Dao public ChapterListBeanDao(DaoConfig config) { super(config); } - + + // 构造函数,使用DaoConfig和DaoSession配置初始化Dao public ChapterListBeanDao(DaoConfig config, DaoSession daoSession) { super(config, daoSession); } /** Creates the underlying database table. */ + // 创建CHAPTER_LIST_BEAN表的方法 public static void createTable(Database db, boolean ifNotExists) { + // 根据ifNotExists判断是否需要添加"IF NOT EXISTS"条件 String constraint = ifNotExists? "IF NOT EXISTS ": ""; + // 执行SQL语句创建表 db.execSQL("CREATE TABLE " + constraint + "\"CHAPTER_LIST_BEAN\" (" + // - "\"NOTE_URL\" TEXT," + // 0: noteUrl - "\"DUR_CHAPTER_INDEX\" INTEGER NOT NULL ," + // 1: durChapterIndex - "\"DUR_CHAPTER_URL\" TEXT PRIMARY KEY NOT NULL ," + // 2: durChapterUrl - "\"DUR_CHAPTER_NAME\" TEXT," + // 3: durChapterName - "\"TAG\" TEXT," + // 4: tag - "\"HAS_CACHE\" INTEGER);"); // 5: hasCache + "\"NOTE_URL\" TEXT," + // NOTE_URL字段,TEXT类型 + "\"DUR_CHAPTER_INDEX\" INTEGER NOT NULL ," + // DUR_CHAPTER_INDEX字段,INTEGER类型,非空 + "\"DUR_CHAPTER_URL\" TEXT PRIMARY KEY NOT NULL ," + // DUR_CHAPTER_URL字段,TEXT类型,主键,非空 + "\"DUR_CHAPTER_NAME\" TEXT," + // DUR_CHAPTER_NAME字段,TEXT类型 + "\"TAG\" TEXT," + // TAG字段,TEXT类型 + "\"HAS_CACHE\" INTEGER);"); // HAS_CACHE字段,INTEGER类型 } - /** Drops the underlying database table. */ + /** Drops the underlying database table. + * 删除数据库表 + */ + // 删除CHAPTER_LIST_BEAN表的方法 public static void dropTable(Database db, boolean ifExists) { + // 构造删除表的SQL语句 String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"CHAPTER_LIST_BEAN\""; + // 执行SQL语句删除表 db.execSQL(sql); } @Override + // 将ChapterListBean实体对象的值绑定到DatabaseStatement语句中 protected final void bindValues(DatabaseStatement stmt, ChapterListBean entity) { + // 清除之前的绑定 stmt.clearBindings(); - + String noteUrl = entity.getNoteUrl(); if (noteUrl != null) { + // 绑定noteUrl值到第一个参数位置 stmt.bindString(1, noteUrl); } + // 绑定durChapterIndex值到第二个参数位置 stmt.bindLong(2, entity.getDurChapterIndex()); - + String durChapterUrl = entity.getDurChapterUrl(); if (durChapterUrl != null) { + // 绑定durChapterUrl值到第三个参数位置 stmt.bindString(3, durChapterUrl); } - + String durChapterName = entity.getDurChapterName(); if (durChapterName != null) { + // 绑定durChapterName值到第四个参数位置 stmt.bindString(4, durChapterName); } - + String tag = entity.getTag(); if (tag != null) { + // 绑定tag值到第五个参数位置 stmt.bindString(5, tag); } - + Boolean hasCache = entity.getHasCache(); if (hasCache != null) { + // 绑定hasCache值到第六个参数位置,true为1,false为0 stmt.bindLong(6, hasCache ? 1L: 0L); } } @Override + // 将ChapterListBean实体对象的值绑定到SQLiteStatement语句中,与上一个方法功能类似,只是对象不同 protected final void bindValues(SQLiteStatement stmt, ChapterListBean entity) { + // 清除之前的绑定 stmt.clearBindings(); - + String noteUrl = entity.getNoteUrl(); if (noteUrl != null) { + // 绑定noteUrl值到第一个参数位置 stmt.bindString(1, noteUrl); } + // 绑定durChapterIndex值到第二个参数位置 stmt.bindLong(2, entity.getDurChapterIndex()); - + String durChapterUrl = entity.getDurChapterUrl(); if (durChapterUrl != null) { + // 绑定durChapterUrl值到第三个参数位置 stmt.bindString(3, durChapterUrl); } - + String durChapterName = entity.getDurChapterName(); if (durChapterName != null) { + // 绑定durChapterName值到第四个参数位置 stmt.bindString(4, durChapterName); } - + String tag = entity.getTag(); if (tag != null) { + // 绑定tag值到第五个参数位置 stmt.bindString(5, tag); } - + Boolean hasCache = entity.getHasCache(); if (hasCache != null) { + // 绑定hasCache值到第六个参数位置,true为1,false为0 stmt.bindLong(6, hasCache ? 1L: 0L); } } @Override + // 从游标中读取主键 public String readKey(Cursor cursor, int offset) { + // 从偏移量offset+2的位置读取主键值,如果为空返回null,否则返回字符串 return cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2); - } + } @Override + // 从游标中读取ChapterListBean实体 public ChapterListBean readEntity(Cursor cursor, int offset) { + // 创建ChapterListBean对象 ChapterListBean entity = new ChapterListBean( // - cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0), // noteUrl - cursor.getInt(offset + 1), // durChapterIndex - cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // durChapterUrl - cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // durChapterName - cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // tag - cursor.isNull(offset + 5) ? null : cursor.getShort(offset + 5) != 0 // hasCache + cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0), // noteUrl + cursor.getInt(offset + 1), // durChapterIndex + cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // durChapterUrl + cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // durChapterName + cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // tag + cursor.isNull(offset + 5) ? null : cursor.getShort(offset + 5) != 0 // hasCache 将short转换为boolean ); + // 返回创建好的ChapterListBean对象 return entity; } - + @Override + // 将游标中的数据读取到已存在的ChapterListBean实体中 public void readEntity(Cursor cursor, ChapterListBean entity, int offset) { - entity.setNoteUrl(cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0)); - entity.setDurChapterIndex(cursor.getInt(offset + 1)); - entity.setDurChapterUrl(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2)); - entity.setDurChapterName(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3)); - entity.setTag(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4)); - entity.setHasCache(cursor.isNull(offset + 5) ? null : cursor.getShort(offset + 5) != 0); - } - + entity.setNoteUrl(cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0)); // noteUrl + entity.setDurChapterIndex(cursor.getInt(offset + 1)); // durChapterIndex + entity.setDurChapterUrl(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2)); // durChapterUrl + entity.setDurChapterName(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3)); // durChapterName + entity.setTag(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4)); // tag + entity.setHasCache(cursor.isNull(offset + 5) ? null : cursor.getShort(offset + 5) != 0); // hasCache 将short转换为boolean + } + @Override + // 插入数据后更新主键 protected final String updateKeyAfterInsert(ChapterListBean entity, long rowId) { + // 返回durChapterUrl作为主键 return entity.getDurChapterUrl(); } - + @Override + // 获取主键 public String getKey(ChapterListBean entity) { if(entity != null) { + // 返回durChapterUrl作为主键 return entity.getDurChapterUrl(); } else { return null; @@ -164,8 +202,10 @@ public class ChapterListBeanDao extends AbstractDao { } @Override + // 判断实体是否可更新 protected final boolean isEntityUpdateable() { + // 返回true,表示实体可更新 return true; } - + } diff --git a/app/src/main/java/com/monke/monkeybook/dao/DaoMaster.java b/app/src/main/java/com/monke/monkeybook/dao/DaoMaster.java index d302a59..fa361b4 100644 --- a/app/src/main/java/com/monke/monkeybook/dao/DaoMaster.java +++ b/app/src/main/java/com/monke/monkeybook/dao/DaoMaster.java @@ -1,109 +1,142 @@ +// 包名声明 package com.monke.monkeybook.dao; -import android.content.Context; -import android.database.sqlite.SQLiteDatabase; -import android.database.sqlite.SQLiteDatabase.CursorFactory; -import android.util.Log; +import android.content.Context; // 导入Context类,用于获取应用程序上下文 +import android.database.sqlite.SQLiteDatabase; // 导入SQLiteDatabase类,用于操作数据库 +import android.database.sqlite.SQLiteDatabase.CursorFactory; // 导入CursorFactory类,用于创建游标工厂 +import android.util.Log; // 导入Log类,用于输出日志信息 -import org.greenrobot.greendao.AbstractDaoMaster; -import org.greenrobot.greendao.database.StandardDatabase; -import org.greenrobot.greendao.database.Database; -import org.greenrobot.greendao.database.DatabaseOpenHelper; -import org.greenrobot.greendao.identityscope.IdentityScopeType; +import org.greenrobot.greendao.AbstractDaoMaster; // 导入AbstractDaoMaster类,GreenDao框架中用于管理DAO的抽象基类 +import org.greenrobot.greendao.database.StandardDatabase; // 导入StandardDatabase类,GreenDao框架中基于SQLite的数据库实现 +import org.greenrobot.greendao.database.Database; // 导入Database类,GreenDao框架中用于数据库操作的接口 +import org.greenrobot.greendao.database.DatabaseOpenHelper; // 导入DatabaseOpenHelper类,GreenDao框架中用于打开和管理数据库的帮助类 +import org.greenrobot.greendao.identityscope.IdentityScopeType; // 导入IdentityScopeType类,GreenDao框架中用于指定标识符作用域类型的枚举 // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. /** * Master of DAO (schema version 1): knows all DAOs. */ +// DaoMaster类继承AbstractDaoMaster,是所有DAO的管理者 public class DaoMaster extends AbstractDaoMaster { + // 数据库模式版本号 public static final int SCHEMA_VERSION = 1; /** Creates underlying database table using DAOs. */ + // 创建所有数据库表的方法 public static void createAllTables(Database db, boolean ifNotExists) { - BookContentBeanDao.createTable(db, ifNotExists); - BookInfoBeanDao.createTable(db, ifNotExists); - BookShelfBeanDao.createTable(db, ifNotExists); - ChapterListBeanDao.createTable(db, ifNotExists); - DownloadChapterBeanDao.createTable(db, ifNotExists); - SearchHistoryBeanDao.createTable(db, ifNotExists); + BookContentBeanDao.createTable(db, ifNotExists); // 创建BookContentBean表 + BookInfoBeanDao.createTable(db, ifNotExists); // 创建BookInfoBean表 + BookShelfBeanDao.createTable(db, ifNotExists); // 创建BookShelfBean表 + ChapterListBeanDao.createTable(db, ifNotExists); // 创建ChapterListBean表 + DownloadChapterBeanDao.createTable(db, ifNotExists); // 创建DownloadChapterBean表 + SearchHistoryBeanDao.createTable(db, ifNotExists); // 创建SearchHistoryBean表 } /** Drops underlying database table using DAOs. */ + // 删除所有数据库表的方法 public static void dropAllTables(Database db, boolean ifExists) { - BookContentBeanDao.dropTable(db, ifExists); - BookInfoBeanDao.dropTable(db, ifExists); - BookShelfBeanDao.dropTable(db, ifExists); - ChapterListBeanDao.dropTable(db, ifExists); - DownloadChapterBeanDao.dropTable(db, ifExists); - SearchHistoryBeanDao.dropTable(db, ifExists); + BookContentBeanDao.dropTable(db, ifExists); // 删除BookContentBean表 + BookInfoBeanDao.dropTable(db, ifExists); // 删除BookInfoBean表 + BookShelfBeanDao.dropTable(db, ifExists); // 删除BookShelfBean表 + ChapterListBeanDao.dropTable(db, ifExists); // 删除ChapterListBean表 + DownloadChapterBeanDao.dropTable(db, ifExists); // 删除DownloadChapterBean表 + SearchHistoryBeanDao.dropTable(db, ifExists); // 删除SearchHistoryBean表 } /** * WARNING: Drops all table on Upgrade! Use only during development. * Convenience method using a {@link DevOpenHelper}. */ + // 创建一个开发模式的DaoSession public static DaoSession newDevSession(Context context, String name) { + // 获取可写的数据库 Database db = new DevOpenHelper(context, name).getWritableDb(); + // 创建DaoMaster实例 DaoMaster daoMaster = new DaoMaster(db); + // 创建DaoSession实例并返回 return daoMaster.newSession(); } + // 构造函数,使用SQLiteDatabase对象初始化DaoMaster public DaoMaster(SQLiteDatabase db) { + // 使用StandardDatabase包装SQLiteDatabase this(new StandardDatabase(db)); } + // 构造函数,使用Database对象初始化DaoMaster public DaoMaster(Database db) { + // 调用父类的构造函数 super(db, SCHEMA_VERSION); - registerDaoClass(BookContentBeanDao.class); - registerDaoClass(BookInfoBeanDao.class); - registerDaoClass(BookShelfBeanDao.class); - registerDaoClass(ChapterListBeanDao.class); - registerDaoClass(DownloadChapterBeanDao.class); - registerDaoClass(SearchHistoryBeanDao.class); + registerDaoClass(BookContentBeanDao.class); // 注册BookContentBeanDao + registerDaoClass(BookInfoBeanDao.class); // 注册BookInfoBeanDao + registerDaoClass(BookShelfBeanDao.class); // 注册BookShelfBeanDao + registerDaoClass(ChapterListBeanDao.class); // 注册ChapterListBeanDao + registerDaoClass(DownloadChapterBeanDao.class); // 注册DownloadChapterBeanDao + registerDaoClass(SearchHistoryBeanDao.class); // 注册SearchHistoryBeanDao } + // 创建一个新的DaoSession,使用默认的IdentityScopeType.Session public DaoSession newSession() { + // 创建DaoSession实例 return new DaoSession(db, IdentityScopeType.Session, daoConfigMap); } + // 创建一个新的DaoSession,使用指定的IdentityScopeType public DaoSession newSession(IdentityScopeType type) { + // 创建DaoSession实例 return new DaoSession(db, type, daoConfigMap); } /** * Calls {@link #createAllTables(Database, boolean)} in {@link #onCreate(Database)} - */ + // OpenHelper抽象类,继承DatabaseOpenHelper,用于创建和打开数据库 public static abstract class OpenHelper extends DatabaseOpenHelper { + // 构造函数,指定上下文和数据库名称 public OpenHelper(Context context, String name) { + // 调用父类的构造函数 super(context, name, SCHEMA_VERSION); } - + // 构造函数,指定上下文、数据库名称和游标工厂 public OpenHelper(Context context, String name, CursorFactory factory) { + // 调用父类的构造函数 super(context, name, factory, SCHEMA_VERSION); } @Override + // 创建数据库时调用 public void onCreate(Database db) { + // 输出日志信息 Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION); + // 创建所有数据库表 createAllTables(db, false); } } /** WARNING: Drops all table on Upgrade! Use only during development. */ + // DevOpenHelper类,继承OpenHelper,用于开发阶段的数据库升级 public static class DevOpenHelper extends OpenHelper { + // 构造函数,指定上下文和数据库名称 public DevOpenHelper(Context context, String name) { + // 调用父类的构造函数 super(context, name); } + // 构造函数,指定上下文、数据库名称和游标工厂 public DevOpenHelper(Context context, String name, CursorFactory factory) { + // 调用父类的构造函数 super(context, name, factory); } @Override + // 数据库升级时调用 public void onUpgrade(Database db, int oldVersion, int newVersion) { + // 输出日志信息 Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables"); + // 删除所有数据库表 dropAllTables(db, true); + // 重新创建数据库表 onCreate(db); } } diff --git a/app/src/main/java/com/monke/monkeybook/dao/DaoSession.java b/app/src/main/java/com/monke/monkeybook/dao/DaoSession.java index 4b6ffbf..c2dc7f4 100644 --- a/app/src/main/java/com/monke/monkeybook/dao/DaoSession.java +++ b/app/src/main/java/com/monke/monkeybook/dao/DaoSession.java @@ -1,118 +1,121 @@ +// 包名声明 package com.monke.monkeybook.dao; - +// 导入Map接口,用于存储键值对 import java.util.Map; -import org.greenrobot.greendao.AbstractDao; -import org.greenrobot.greendao.AbstractDaoSession; -import org.greenrobot.greendao.database.Database; -import org.greenrobot.greendao.identityscope.IdentityScopeType; -import org.greenrobot.greendao.internal.DaoConfig; - -import com.monke.monkeybook.bean.BookContentBean; -import com.monke.monkeybook.bean.BookInfoBean; -import com.monke.monkeybook.bean.BookShelfBean; -import com.monke.monkeybook.bean.ChapterListBean; -import com.monke.monkeybook.bean.DownloadChapterBean; -import com.monke.monkeybook.bean.SearchHistoryBean; - -import com.monke.monkeybook.dao.BookContentBeanDao; -import com.monke.monkeybook.dao.BookInfoBeanDao; -import com.monke.monkeybook.dao.BookShelfBeanDao; -import com.monke.monkeybook.dao.ChapterListBeanDao; -import com.monke.monkeybook.dao.DownloadChapterBeanDao; -import com.monke.monkeybook.dao.SearchHistoryBeanDao; +import org.greenrobot.greendao.AbstractDao; // 导入AbstractDao类,GreenDao框架中数据库操作的抽象基类 +import org.greenrobot.greendao.AbstractDaoSession; // 导入AbstractDaoSession类,GreenDao框架中DAO会话的抽象基类 +import org.greenrobot.greendao.database.Database; // 导入Database类,GreenDao框架中数据库操作的接口 +import org.greenrobot.greendao.identityscope.IdentityScopeType; // 导入IdentityScopeType类,GreenDao框架中用于指定标识符作用域类型的枚举 +import org.greenrobot.greendao.internal.DaoConfig; // 导入DaoConfig类,GreenDao框架中用于配置Dao对象的配置信息 + +import com.monke.monkeybook.bean.BookContentBean; // 导入BookContentBean类,书籍内容Bean +import com.monke.monkeybook.bean.BookInfoBean; // 导入BookInfoBean类,书籍信息Bean +import com.monke.monkeybook.bean.BookShelfBean; // 导入BookShelfBean类,书架Bean +import com.monke.monkeybook.bean.ChapterListBean; // 导入ChapterListBean类,章节列表Bean +import com.monke.monkeybook.bean.DownloadChapterBean; // 导入DownloadChapterBean类,下载章节Bean +import com.monke.monkeybook.bean.SearchHistoryBean; // 导入SearchHistoryBean类,搜索历史Bean + +import com.monke.monkeybook.dao.BookContentBeanDao; // 导入BookContentBeanDao类 +import com.monke.monkeybook.dao.BookInfoBeanDao; // 导入BookInfoBeanDao类 +import com.monke.monkeybook.dao.BookShelfBeanDao; // 导入BookShelfBeanDao类 +import com.monke.monkeybook.dao.ChapterListBeanDao; // 导入ChapterListBeanDao类 +import com.monke.monkeybook.dao.DownloadChapterBeanDao; // 导入DownloadChapterBeanDao类 +import com.monke.monkeybook.dao.SearchHistoryBeanDao; // 导入SearchHistoryBeanDao类 // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. /** * {@inheritDoc} - * + * * @see org.greenrobot.greendao.AbstractDaoSession */ +// DaoSession类继承AbstractDaoSession,管理数据库会话 public class DaoSession extends AbstractDaoSession { - private final DaoConfig bookContentBeanDaoConfig; - private final DaoConfig bookInfoBeanDaoConfig; - private final DaoConfig bookShelfBeanDaoConfig; - private final DaoConfig chapterListBeanDaoConfig; - private final DaoConfig downloadChapterBeanDaoConfig; - private final DaoConfig searchHistoryBeanDaoConfig; + private final DaoConfig bookContentBeanDaoConfig; // BookContentBeanDao的配置信息 + private final DaoConfig bookInfoBeanDaoConfig; // BookInfoBeanDao的配置信息 + private final DaoConfig bookShelfBeanDaoConfig; // BookShelfBeanDao的配置信息 + private final DaoConfig chapterListBeanDaoConfig; // ChapterListBeanDao的配置信息 + private final DaoConfig downloadChapterBeanDaoConfig; // DownloadChapterBeanDao的配置信息 + private final DaoConfig searchHistoryBeanDaoConfig; // SearchHistoryBeanDao的配置信息 - private final BookContentBeanDao bookContentBeanDao; - private final BookInfoBeanDao bookInfoBeanDao; - private final BookShelfBeanDao bookShelfBeanDao; - private final ChapterListBeanDao chapterListBeanDao; - private final DownloadChapterBeanDao downloadChapterBeanDao; - private final SearchHistoryBeanDao searchHistoryBeanDao; + private final BookContentBeanDao bookContentBeanDao; // BookContentBeanDao实例 + private final BookInfoBeanDao bookInfoBeanDao; // BookInfoBeanDao实例 + private final BookShelfBeanDao bookShelfBeanDao; // BookShelfBeanDao实例 + private final ChapterListBeanDao chapterListBeanDao; // ChapterListBeanDao实例 + private final DownloadChapterBeanDao downloadChapterBeanDao; // DownloadChapterBeanDao实例 + private final SearchHistoryBeanDao searchHistoryBeanDao; // SearchHistoryBeanDao实例 public DaoSession(Database db, IdentityScopeType type, Map>, DaoConfig> - daoConfigMap) { + daoConfigMap) {// DaoSession的构造函数,传入Database、IdentityScopeType和DaoConfig Map + // 调用父类的构造函数 super(db); - bookContentBeanDaoConfig = daoConfigMap.get(BookContentBeanDao.class).clone(); - bookContentBeanDaoConfig.initIdentityScope(type); + bookContentBeanDaoConfig = daoConfigMap.get(BookContentBeanDao.class).clone(); // 从map中获取BookContentBeanDao的配置信息并克隆 + bookContentBeanDaoConfig.initIdentityScope(type); // 初始化BookContentBeanDao的标识符作用域 - bookInfoBeanDaoConfig = daoConfigMap.get(BookInfoBeanDao.class).clone(); - bookInfoBeanDaoConfig.initIdentityScope(type); + bookInfoBeanDaoConfig = daoConfigMap.get(BookInfoBeanDao.class).clone(); // 从map中获取BookInfoBeanDao的配置信息并克隆 + bookInfoBeanDaoConfig.initIdentityScope(type); // 初始化BookInfoBeanDao的标识符作用域 - bookShelfBeanDaoConfig = daoConfigMap.get(BookShelfBeanDao.class).clone(); - bookShelfBeanDaoConfig.initIdentityScope(type); + bookShelfBeanDaoConfig = daoConfigMap.get(BookShelfBeanDao.class).clone(); // 从map中获取BookShelfBeanDao的配置信息并克隆 + bookShelfBeanDaoConfig.initIdentityScope(type); // 初始化BookShelfBeanDao的标识符作用域 - chapterListBeanDaoConfig = daoConfigMap.get(ChapterListBeanDao.class).clone(); - chapterListBeanDaoConfig.initIdentityScope(type); + chapterListBeanDaoConfig = daoConfigMap.get(ChapterListBeanDao.class).clone(); // 从map中获取ChapterListBeanDao的配置信息并克隆 + chapterListBeanDaoConfig.initIdentityScope(type); // 初始化ChapterListBeanDao的标识符作用域 - downloadChapterBeanDaoConfig = daoConfigMap.get(DownloadChapterBeanDao.class).clone(); - downloadChapterBeanDaoConfig.initIdentityScope(type); + downloadChapterBeanDaoConfig = daoConfigMap.get(DownloadChapterBeanDao.class).clone(); // 从map中获取DownloadChapterBeanDao的配置信息并克隆 + downloadChapterBeanDaoConfig.initIdentityScope(type); // 初始化DownloadChapterBeanDao的标识符作用域 - searchHistoryBeanDaoConfig = daoConfigMap.get(SearchHistoryBeanDao.class).clone(); - searchHistoryBeanDaoConfig.initIdentityScope(type); + searchHistoryBeanDaoConfig = daoConfigMap.get(SearchHistoryBeanDao.class).clone(); // 从map中获取SearchHistoryBeanDao的配置信息并克隆 + searchHistoryBeanDaoConfig.initIdentityScope(type); // 初始化SearchHistoryBeanDao的标识符作用域 - bookContentBeanDao = new BookContentBeanDao(bookContentBeanDaoConfig, this); - bookInfoBeanDao = new BookInfoBeanDao(bookInfoBeanDaoConfig, this); - bookShelfBeanDao = new BookShelfBeanDao(bookShelfBeanDaoConfig, this); - chapterListBeanDao = new ChapterListBeanDao(chapterListBeanDaoConfig, this); - downloadChapterBeanDao = new DownloadChapterBeanDao(downloadChapterBeanDaoConfig, this); - searchHistoryBeanDao = new SearchHistoryBeanDao(searchHistoryBeanDaoConfig, this); + bookContentBeanDao = new BookContentBeanDao(bookContentBeanDaoConfig, this); // 创建BookContentBeanDao实例 + bookInfoBeanDao = new BookInfoBeanDao(bookInfoBeanDaoConfig, this); // 创建BookInfoBeanDao实例 + bookShelfBeanDao = new BookShelfBeanDao(bookShelfBeanDaoConfig, this); // 创建BookShelfBeanDao实例 + chapterListBeanDao = new ChapterListBeanDao(chapterListBeanDaoConfig, this); // 创建ChapterListBeanDao实例 + downloadChapterBeanDao = new DownloadChapterBeanDao(downloadChapterBeanDaoConfig, this); // 创建DownloadChapterBeanDao实例 + searchHistoryBeanDao = new SearchHistoryBeanDao(searchHistoryBeanDaoConfig, this); // 创建SearchHistoryBeanDao实例 - registerDao(BookContentBean.class, bookContentBeanDao); - registerDao(BookInfoBean.class, bookInfoBeanDao); - registerDao(BookShelfBean.class, bookShelfBeanDao); - registerDao(ChapterListBean.class, chapterListBeanDao); - registerDao(DownloadChapterBean.class, downloadChapterBeanDao); - registerDao(SearchHistoryBean.class, searchHistoryBeanDao); + registerDao(BookContentBean.class, bookContentBeanDao); // 注册BookContentBeanDao + registerDao(BookInfoBean.class, bookInfoBeanDao); // 注册BookInfoBeanDao + registerDao(BookShelfBean.class, bookShelfBeanDao); // 注册BookShelfBeanDao + registerDao(ChapterListBean.class, chapterListBeanDao); // 注册ChapterListBeanDao + registerDao(DownloadChapterBean.class, downloadChapterBeanDao); // 注册DownloadChapterBeanDao + registerDao(SearchHistoryBean.class, searchHistoryBeanDao); // 注册SearchHistoryBeanDao } - - public void clear() { - bookContentBeanDaoConfig.getIdentityScope().clear(); - bookInfoBeanDaoConfig.getIdentityScope().clear(); - bookShelfBeanDaoConfig.getIdentityScope().clear(); - chapterListBeanDaoConfig.getIdentityScope().clear(); - downloadChapterBeanDaoConfig.getIdentityScope().clear(); - searchHistoryBeanDaoConfig.getIdentityScope().clear(); + + public void clear() {// 清除所有DAO的标识符作用域 + bookContentBeanDaoConfig.getIdentityScope().clear(); // 清除BookContentBeanDao的标识符作用域 + bookInfoBeanDaoConfig.getIdentityScope().clear(); // 清除BookInfoBeanDao的标识符作用域 + bookShelfBeanDaoConfig.getIdentityScope().clear(); // 清除BookShelfBeanDao的标识符作用域 + chapterListBeanDaoConfig.getIdentityScope().clear(); // 清除ChapterListBeanDao的标识符作用域 + downloadChapterBeanDaoConfig.getIdentityScope().clear(); // 清除DownloadChapterBeanDao的标识符作用域 + searchHistoryBeanDaoConfig.getIdentityScope().clear(); // 清除SearchHistoryBeanDao的标识符作用域 } - public BookContentBeanDao getBookContentBeanDao() { - return bookContentBeanDao; + public BookContentBeanDao getBookContentBeanDao() {// 获取BookContentBeanDao实例 + return bookContentBeanDao;// 返回BookContentBeanDao实例 } - public BookInfoBeanDao getBookInfoBeanDao() { - return bookInfoBeanDao; + public BookInfoBeanDao getBookInfoBeanDao() {// 获取BookInfoBeanDao实例 + return bookInfoBeanDao;// 返回BookInfoBeanDao实例 } - public BookShelfBeanDao getBookShelfBeanDao() { - return bookShelfBeanDao; + public BookShelfBeanDao getBookShelfBeanDao() {// 获取BookShelfBeanDao实例 + return bookShelfBeanDao;// 返回BookShelfBeanDao实例 } - public ChapterListBeanDao getChapterListBeanDao() { - return chapterListBeanDao; + public ChapterListBeanDao getChapterListBeanDao() {// 获取ChapterListBeanDao实例 + return chapterListBeanDao;// 返回ChapterListBeanDao实例 } - public DownloadChapterBeanDao getDownloadChapterBeanDao() { - return downloadChapterBeanDao; + public DownloadChapterBeanDao getDownloadChapterBeanDao() {// 获取DownloadChapterBeanDao实例 + return downloadChapterBeanDao;// 返回DownloadChapterBeanDao实例 } - public SearchHistoryBeanDao getSearchHistoryBeanDao() { - return searchHistoryBeanDao; + public SearchHistoryBeanDao getSearchHistoryBeanDao() {// 获取SearchHistoryBeanDao实例 + return searchHistoryBeanDao;// 返回SearchHistoryBeanDao实例 } }