以下是我学习greenDao的过程,我要把它写的很长很长:
按照官方给的方式添加greendao依赖:
首先在项目gradle里添加maven仓以及插件:
- // In your root build.gradle file:
buildscript {
repositories {
jcenter()
mavenCentral() //添加maven仓
}
dependencies {
classpath ‘com.android.tools.build:gradle:2.3.1’//已有,如果版本低的话会报错的,
classpath ‘org.greenrobot:greendao-gradle-plugin:3.2.2’ // 添加插件更好的使用greendao,ruguobaocuo,jiang ban ben hao
}
然后在app的gradle中添加插件、依赖,以及初始化
(1) apply plugin: ‘org.greenrobot.greendao’ // 添加应用插件ruguobaocuo,jiang ban ben hao
(2) compile ‘org.greenrobot:greendao:3.2.2’ //添加依赖
(3) 配置GreenDao基本参数
greendao {
schemaVersion 1 //当前数据库版本
}
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao' // 添加应用插件
android {
compileSdkVersion 25
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "com.example.administrator.myapplication5"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
// 配置GreenDao基本参数
greendao {
schemaVersion 1 //当前数据库版本
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
compile 'com.mabeijianxi:small-video-record:1.2.2'
compile 'org.greenrobot:greendao:3.2.2' // add library
}
到此greendao的基本配置完成———————–
一、编写greendao所需要的实体类
首先理解他的注解:
1.Entity
@Entity配置一些详细信息,暂时未领悟等码完再说
@Entity(
// 如果你有一个以上的模式,你可以告诉greendao实体属于哪个模式(选择任何字符串作为名称)。
schema = "myschema",
// 标志允许实体类可有更新,删除,刷新方法
active = true,
// 指定数据库中表的名称。默认情况下,该名称基于实体类名。
nameInDb = "AWESOME_USERS",
// 在这里定义多个列的索引
indexes = {
@Index(value = "name DESC", unique = true)
},
// 如果DAO创建数据库表(默认为true),则设置标记去标识。如果有多个实体映射到一个表,或者在greenDAO之外创建表创建,将此设置为false。
createInDb = false,
// 是否应该生成所有的属性构造函数。一个无args构造函数总是需要的
generateConstructors = true,
// 是否生成属性的getter和setter
generateGettersSetters = true
)
作者:静心Study
链接:http://www.jianshu.com/p/b017ca5fe0ab
來源:简书
2 . Id
自增长,long/Long类型,目的:不重复(我是数据库小白吃)
3.index
4.unique
@Entity
public class User {
@Id(autoincrement = true)
private long Id;
@Index(unique = true)
private String stuNum;
private String name;
private String sex;
private long age;
}
编译项目:
@Entity
public class User {
@Id(autoincrement = true)
private long Id;
@Index(unique = true)
private String stuNum;
private String name;
private String sex;
private long age;
@Generated(hash = 1800020465)
public User(long Id, String stuNum, String name, String sex, long age) {
this.Id = Id;
this.stuNum = stuNum;
this.name = name;
this.sex = sex;
this.age = age;
}
@Generated(hash = 586692638)
public User() {
}
public long getId() {
return this.Id;
}
public void setId(long Id) {
this.Id = Id;
}
public String getStuNum() {
return this.stuNum;
}
public void setStuNum(String stuNum) {
this.stuNum = stuNum;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return this.sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public long getAge() {
return this.age;
}
public void setAge(long age) {
this.age = age;
}
}
Dao内容存储位置(自动生成):
今晚就学到这。。。