需要在build.gradle(是你创建project时的build.gradle下)
buildscript {
repositories {
jcenter()
//需要创建下面这个属性
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
//需要创建这个属性
classpath'org.greenrobot:greendao-gradle-plugin:3.2.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
我直接吧整个gradle放了进来方便你们看按照我标红的部分的复制到你们grable对应位置
//这个必须加 applyplugin:'org.greenrobot.greendao'
//这个是生成gen目录代码
greendao { schemaVersion 1 daoPackage 'com.jian.greendao.gen'//这个是生成代码保存的包名 targetGenDir 'src/main/java'//保存到java代码 }
//这个是依赖 compile'org.greenrobot:greendao:3.2.2'
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "com.example.greendaolianxi"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
greendao {
schemaVersion 1
daoPackage 'com.jian.greendao.gen'//这个是生成代码保存的包名
targetGenDir 'src/main/java'//保存到java代码
}
}
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:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile'org.greenrobot:greendao:3.2.2'
}
主方法中创建一个class类,里面的字段就是表的字段
但是第一个属性id必须是Long类型( L 小写不行),加上注释,可以一键生成。
代码如下:
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Generated;
/**
* author:Created by Chenxu on 2017/12/31.
*/
@Entity
public class MyUser {
@Id(autoincrement = true)
private Long id;
private String name;
private String sex;
private int age;
//下面的代码都是生成的...只需要创建好你所需要的字段,然后加入上面两个注解
//(@Entry 和 @Id)就行了,然后点击你AS的上方Build会有一个Make Moudle 或者快捷键生成代码ctrl+F9
//"你的项目名称",点击能自动生成下面代码,而且生成一个gen包...
@Generated(hash = 1329017192)
public MyUser(Long id, String name, String sex, int age) {
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
}
@Generated(hash = 623865568)
public MyUser() {
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
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 int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
}
在有关数据库的操作时,我们应该封装一个数据库的操作类 进行对数据库的增删改查
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.jian.greendao.gen.DaoMaster;
import com.jian.greendao.gen.DaoSession;
import com.jian.greendao.gen.MyUserDao;
/**
* author:Created by Chenxu on 2017/12/31.
*/
public class DBUtils {
//单例模式
private static volatile DBUtils instance;
private MyUserDao myUserDao;
public static DBUtils getInstance(Context context){
if(instance == null){
synchronized(DBUtils.class){
if(instance == null){
instance = new DBUtils(context);
}
}
}
return instance;
}
//定义一个方法,用来获取数据库对象
private DBUtils(Context context){
//创建一个数据库(参数分别是:上下文,数据库名,和版本号[自己练习时可以为空])
DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(context, "chenxu.db", null);
//一系类操作....
SQLiteDatabase writableDatabase = devOpenHelper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(writableDatabase);
DaoSession daoSession = daoMaster.newSession();
//得到一个具体的数据库操作对象
myUserDao = daoSession.getMyUserDao();
}
//创建一个公共方法,将数据库对象返回出去,以便于使用
public MyUserDao setDao(){
return myUserDao;
}
}
//最重要的来了,怎么使用呢,当然在Main里面进行操作了
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.jian.greendao.gen.MyUserDao;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_add;
private Button btn_delete;
private Button btn_update;
private Button btn_selete;
private MyUserDao myUserDaochen;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myUserDaochen = DBUtils.getInstance(this).setDao();
initView();
}
private void initView() {
btn_add = (Button) findViewById(R.id.btn_add);
btn_delete = (Button) findViewById(R.id.btn_delete);
btn_update = (Button) findViewById(R.id.btn_update);
btn_selete = (Button) findViewById(R.id.btn_selete);
btn_add.setOnClickListener(this);
btn_delete.setOnClickListener(this);
btn_update.setOnClickListener(this);
btn_selete.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_add:
MyUser myUser = new MyUser(null, "陈旭", "男", 20);
myUserDaochen.insert(myUser);
break;
case R.id.btn_delete:
MyUser myUser3 = new MyUser();
myUser3.setId(3L);
myUserDaochen.delete(myUser3);
break;
case R.id.btn_update:
MyUser myUser1 = new MyUser();
myUser1.setId(1L);
myUser1.setName("帅哥");
myUserDaochen.update(myUser1);
break;
case R.id.btn_selete:
//查询数据库里的所有对象
List<MyUser> myUsers = myUserDaochen.loadAll();
for (int i = 0; i < myUsers.size(); i++) {
MyUser myUser2 = myUsers.get(i);
Log.e("TAG","我是查询"+myUser2.getName());
}
break;
}
}
}
//把Main布局也给你,你还有什么理由出不来效果
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.example.greendaolianxi.MainActivity">
<Button
android:id="@+id/btn_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加"/>
<Button
android:id="@+id/btn_delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删除"/>
<Button
android:id="@+id/btn_update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="更改"/>
<Button
android:id="@+id/btn_selete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询"/>
</LinearLayout>