主项目的build.gradle
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
}
allprojects {
repositories {
google()
jcenter()
}
}
App的build.gradle
apply plugin: 'org.greenrobot.greendao'
greendao {
schemaVersion 1 //版本
daoPackage 'wanghuiqi.bawie.com.green_dao.user' // 一般为app包名+生成文件的文件夹名
targetGenDir 'src/main/java' //生成文件路径
}
implementation 'org.greenrobot:greendao:3.2.2'
implementation 'org.greenrobot:greendao-generator:3.2.2'
MyApplication
public class MyApplication extends Application {
private DaoMaster.DevOpenHelper helper;
public static MyApplication instances;
private DaoMaster.DevOpenHelper helper1;
private SQLiteDatabase db;
private DaoMaster daoMaster;
private DaoSession daoSession;
@Override
public void onCreate() {
super.onCreate();
instances=this;
setDatabase();
}
public static MyApplication getInstances(){
return instances;
}
private void setDatabase() {
helper1 = new DaoMaster.DevOpenHelper(this, "note-db", null);
db = helper1.getWritableDatabase();
daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
}
public DaoSession getDaoSession(){
return daoSession;
}
public SQLiteDatabase getDb(){
return db;
}
}
User 类
@Entity
public class User {
@Id(autoincrement = true)
private Long id;
@Property(nameInDb = "NAME")
private String name;
@Generated(hash = 873297011)
public User(Long id, String name) {
this.id = id;
this.name = name;
}
@Generated(hash = 586692638)
public User() {
}
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;
}
}
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private UserDao userDao;
private User user;
private Button insert;
private Button del;
private Button update;
private Button sel;
private TextView text;
private EditText ed_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//获取UserDao
userDao = MyApplication.getInstances().getDaoSession().getUserDao();
}
private void initView() {
insert = (Button) findViewById(R.id.insert);
del = (Button) findViewById(R.id.del);
update = (Button) findViewById(R.id.update);
sel = (Button) findViewById(R.id.sel);
text = (TextView) findViewById(R.id.text);
ed_text = findViewById(R.id.ed_text);
insert.setOnClickListener(this);
del.setOnClickListener(this);
update.setOnClickListener(this);
sel.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.insert:
//添加
user = new User(null, "王慧琦");
userDao.insert(user);
break;
case R.id.del:
String trim_del = ed_text.getText().toString().trim();
Integer integer_del = Integer.valueOf(trim_del);
//删除
userDao.deleteByKey((long) integer_del);
break;
case R.id.update:
//改
String trim_up = ed_text.getText().toString().trim();
Integer integer_up = Integer.valueOf(trim_up);
user = new User((long) integer_up, "W王慧琦0.0");
userDao.update(user);
break;
case R.id.sel:
//查
List<User> users = userDao.loadAll();
String userName = "";
for (int i = 0; i < users.size(); i++) {
userName += "," + users.get(i).getName() + users.get(i).getId();
}
text.setText("查询全部数据==>" + userName);
break;
}
}
}
布局文件
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加" />
<Button
android:id="@+id/del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除" />
<Button
android:id="@+id/update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改" />
<Button
android:id="@+id/sel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询" />
</LinearLayout>
<EditText
android:id="@+id/ed_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="jskhdja" />
</LinearLayout>