引用最新版greendao
implementation "org.greenrobot:greendao:3.2.2"
配置生成路径和版本号
greendao {
schemaVersion 1
daoPackage 'com.xk.greendao.db'
targetGenDir 'src/main/java'
}
说明:版本号为1,生成文件放在com.xk.greendao.db
下的这个路径里src/main/java
.
点击Mark Project
键,就会生成相关文件
greendao 表的注解说明(我为了方便就按照我的项目里建的表来说了)
表:
import com.xk.gvido.app.model.net.bean.video.VideoSimpleInfoBean;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Index;
import org.greenrobot.greendao.annotation.NotNull;
import org.greenrobot.greendao.annotation.Property;
import java.util.Date;
/**
* @author Xiong Ke
* @date 2017/10/19
*/
@Entity(nameInDb = "browsing_history")
public class BrowsingHistory {
@Property(nameInDb = "_title")
private String title;
@Property(nameInDb = "_pic")
private String pic;
@Id
@NotNull
@Index(unique = true)
@Property(nameInDb = "_dataId")
private String dataId;
@Property(nameInDb = "_score")
private String score;
@Property(nameInDb = "_airTime")
private String airTime;
@Property(nameInDb = "_moreURL")
private String moreURL;
@Property(nameInDb = "_loadURL")
private String loadURL;
@Property(nameInDb = "_duration")
private String duration;
@Property(nameInDb = "_description")
private String description;
@Property(nameInDb = "_angleIcon")
private String angleIcon;
@Property(nameInDb = "_roomId")
private String roomId;
@Property(nameInDb = "_shareURL")
private String shareURL;
@Property(nameInDb = "_loadType")
private String loadType;
@Property(nameInDb = "_browsingDate")
private Date browsingDate;
public static BrowsingHistory newInstance(VideoSimpleInfoBean videoSimpleInfoBean) {
return new BrowsingHistory(videoSimpleInfoBean.title
, videoSimpleInfoBean.pic
, videoSimpleInfoBean.dataId
, videoSimpleInfoBean.score
, videoSimpleInfoBean.airTime
, videoSimpleInfoBean.moreURL
, videoSimpleInfoBean.loadURL
, videoSimpleInfoBean.duration
, videoSimpleInfoBean.description
, videoSimpleInfoBean.angleIcon
, videoSimpleInfoBean.roomId
, videoSimpleInfoBean.shareURL
, videoSimpleInfoBean.loadType
, new Date());
}
@Generated(hash = 1357393467)
public BrowsingHistory(String title, String pic, @NotNull String dataId,
String score, String airTime, String moreURL, String loadURL,
String duration, String description, String angleIcon, String roomId,
String shareURL, String loadType, Date browsingDate) {
this.title = title;
this.pic = pic;
this.dataId = dataId;
this.score = score;
this.airTime = airTime;
this.moreURL = moreURL;
this.loadURL = loadURL;
this.duration = duration;
this.description = description;
this.angleIcon = angleIcon;
this.roomId = roomId;
this.shareURL = shareURL;
this.loadType = loadType;
this.browsingDate = browsingDate;
}
@Generated(hash = 1959202334)
public BrowsingHistory() {
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPic() {
return this.pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public String getDataId() {
return this.dataId;
}
public void setDataId(String dataId) {
this.dataId = dataId;
}
public String getScore() {
return this.score;
}
public void setScore(String score) {
this.score = score;
}
public String getAirTime() {
return this.airTime;
}
public void setAirTime(String airTime) {
this.airTime = airTime;
}
public String getMoreURL() {
return this.moreURL;
}
public void setMoreURL(String moreURL) {
this.moreURL = moreURL;
}
public String getLoadURL() {
return this.loadURL;
}
public void setLoadURL(String loadURL) {
this.loadURL = loadURL;
}
public String getDuration() {
return this.duration;
}
public void setDuration(String duration) {
this.duration = duration;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public String getAngleIcon() {
return this.angleIcon;
}
public void setAngleIcon(String angleIcon) {
this.angleIcon = angleIcon;
}
public String getRoomId() {
return this.roomId;
}
public void setRoomId(String roomId) {
this.roomId = roomId;
}
public String getShareURL() {
return this.shareURL;
}
public void setShareURL(String shareURL) {
this.shareURL = shareURL;
}
public String getLoadType() {
return this.loadType;
}
public void setLoadType(String loadType) {
this.loadType = loadType;
}
public Date getBrowsingDate() {
return this.browsingDate;
}
public void setBrowsingDate(Date browsingDate) {
this.browsingDate = browsingDate;
}
}
说明:
a. @Entity(nameInDb = "browsing_history")
生成的表的名字
b.
@Id
表的主键
@Index(unique = true)
主键索引唯一
@Property(nameInDb = "_dataId")
键值得名字
c. 每次创建新表时,都要提高表的版本号,然后重新编辑
3.使用方法
public class AppDbHelper implements DbHelper {
private static AppDbHelper appDbHelper;
private static DaoSession mDaoSession;
private AppDbHelper() {
}
public static AppDbHelper getInstance() {
if (appDbHelper == null) {
synchronized (AppDbHelper.class) {
if (appDbHelper == null) {
appDbHelper = new AppDbHelper();
}
}
}
return appDbHelper;
}
//application中初始化
public static void init(Application application){
DaoMaster.DevOpenHelper dbOpenHelper = new DaoMaster.DevOpenHelper(application, DbConfig.DATABASE_NAME);
Database db = dbOpenHelper.getWritableDb();
mDaoSession = new DaoMaster(db).newSession();
if (BuildConfig.DEBUG) {
QueryBuilder.LOG_SQL = true;
QueryBuilder.LOG_VALUES = true;
}
}
@Override
public Observable insertOrReplaceHotSearch(final List searchHotRecommends) {
return Observable.fromCallable(() -> {
mDaoSession.getSearchHotRecommendDao().deleteAll();
mDaoSession.getSearchHotRecommendDao().insertOrReplaceInTx(
searchHotRecommends.toArray(new SearchHotRecommend[searchHotRecommends.size()]));
return 1L;
});
}
@Override
public Observable> getHotSearch() {
return Observable.just(mDaoSession.getSearchHotRecommendDao().loadAll());
}
@Override
public Observable insertOrReplacePictures(final List
public class DbConfig {
public static final String DATABASE_NAME = "gVideo";
}
说明:
DbHelper
是我写的一个接口类,这个类来实现的.