效果展示
![这里写图片描述](https://img-blog.csdn.net/20171115151226327?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzk0OTM3Nzc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
一,在as中导入相关的包
compile 'org.greenrobot:greendao:3.2.0'
二,在build.gradle中进行配置:
最上面引包
apply plugin: 'org.greenrobot.greendao'
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
三,自定义路径
在app的build.gradle里面的Android中配置
greendao {
schemaVersion 1
daoPackage '更改自己的包名'
targetGenDir 'src/main/java'
}
在gradle的根模块中加入上述代码,就完成了我们的基本配置了。
属性介绍:
schemaVersion–> 指定数据库schema版本号,迁移等操作会用到;
daoPackage –> dao的包名,包名默认是entity所在的包;
targetGenDir –> 生成数据库文件的目录;
四,创建一个User的实体类
@Entity
public class User {
@Id
private Long id;
private String name;
@Transient
private int tempUsageCount;
}
五,MakeProject
编译项目,User实体类会自动编译,生成get、set方法并且会在com.anye.greendao.gen目录下生成三个文件;
![这里写图片描述](https://img-blog.csdn.net/20171115152959100?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzk0OTM3Nzc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
MyApplication(一定要在清单文件里配置它)
public class MyApplication extends Application {
private DaoMaster.DevOpenHelper mHelper;
private SQLiteDatabase db;
private DaoMaster mDaoMaster;
private DaoSession mDaoSession;
public static MyApplication instances;
@Override public void onCreate() {
super.onCreate();
instances = this;
setDatabase();
}
public static MyApplication getInstances(){
return instances;
}
/**
* 设置greenDao
*/
private void setDatabase() {
mHelper = new DaoMaster.DevOpenHelper(this, "notes-db", null);
db = mHelper.getWritableDatabase();
mDaoMaster = new DaoMaster(db);
mDaoSession = mDaoMaster.newSession();
}
public DaoSession getDaoSession() {
return mDaoSession;
}
public SQLiteDatabase getDb() {
return db;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
主MainActivity
private UserDao userDao;
private Button butten1,butten2,butten3,butten4;
private Long id;
private User anye3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
butten1=(Button)findViewById(R.id.butten1);
butten2=(Button)findViewById(R.id.butten2);
butten3=(Button)findViewById(R.id.butten3);
butten4=(Button)findViewById(R.id.butten4);
userDao = MyApplication.getInstances().getDaoSession().getUserDao();
butten1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
anye3 = new User(null, "anye3");
userDao.insert(anye3);
Log.d("添加了:===",""+ anye3);
}
});
butten4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
userDao.deleteByKey(id);
Log.d("删除了",""+id);
}
});
butten3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
anye3 = new User(id, "anye0803");
userDao.update(anye3);
Log.d("修改了:",""+anye3);
}
});
butten2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
List<User> users = userDao.loadAll();
String userName = "";
for (int i = 0; i < users.size(); i++) {
userName += users.get(i).getName()+",";
id = users.get(i).getId();
}
Log.d("查询全部数据==>",""+userName);
}
});
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
activity_main
<Button
android:id="@+id/butten1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加"
/>
<Button
android:id="@+id/butten2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询"
/>
<Button
android:id="@+id/butten3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="修改"
/>
<Button
android:id="@+id/butten4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删除"
/>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
谢谢观看,小编祝大家生活愉快!(多多关注小编,会有非常之多精彩分享哦!)