首先导入依赖
三个文件...........之后所有相关的数据库操作都依靠这三个文件了。
compile 'org.greenrobot:greendao:3.0.1'
compile 'org.greenrobot:greendao-generator:3.0.0'
然后在你的Module的build.gradle中加如下代码
apply plugin: 'org.greenrobot.greendao'
greendao {
schemaVersion 1
daoPackage 'com.bwei.com.a1110greendao.gen'
targetGenDir 'src/main/java'
}
schemaVersion是你的版本号
daoPackage放你的包名
targetGenDir是生成的文件的地方,然后在你的Project的build.gradle中添加
buildscript {
repositories {
jcenter()
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
classpath 'org.greenrobot:greendao-gradle-plugin:3.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
拿上面的代码跟你的对比,把没有的加进去,然后创建一个实体类
@Entity
public class User {
@Id
private Long id;
private String name;
@Transient
private int tempUsageCount; // not persisted
}
然后Make Project
@Id:通过这个注解标记的字段必须是Long类型的,这个字段在数据库中表示它就是主键,并且它默认就是自增的
@Entity :将我们的java普通类变为一个能够被greenDAO识别的数据库类型的实体类
@Transient:表明这个字段不会被写入数据库,只是作为一个普通的java类字段,用来临时存储数据的,不会被持久化
接下来让我们点击as中Build菜单栏中的Make Project,make完成之后会发现我们的User类中突然多了好多代码,这就是greenDAO自动为你生成的了
package com.bwei.com.a1110greendao;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Transient;
import org.greenrobot.greendao.annotation.Generated;
/**
* Created by 56553 on 2017/11/10.
*/
@Entity
public class User {
@Id
private Long id;
private String name;
@Transient
private int tempUsageCount; // not persisted
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
@Generated(hash = 873297011)
public User(Long id, String name) {
this.id = id;
this.name = name;
}
@Generated(hash = 586692638)
public User() {
}
}
三个文件...........之后所有相关的数据库操作都依靠这三个文件了。
依赖里面的那些代码别写错位置了,报看不懂的错,找错贼难