先在第一个 build.gradle里面添加
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
***classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'***
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
再在第二个 build.gradle里面添加
apply plugin: 'org.greenrobot.greendao'
greendao {
schemaVersion 1//数据库版本号
daoPackage 'com.com.sky.downloader.greendao'//设置DaoMaster、DaoSession、Dao包名
targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录
//targetGenDirTest:设置生成单元测试目录
//generateTests:设置自动生成单元测试用例
}
implementation 'org.greenrobot:greendao:3.2.2'
然后添加bean类 User
@Entity
public class User {
@Id(autoincrement = true)
private Long id;
private String name;
private int age;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
@Generated(hash = 1309193360)
public User(Long id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@Generated(hash = 586692638)
public User() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
然后点击标题栏的Build-Make Project,就会自动生成GreenDao的类了
.activity_main
<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"
tools:context=".MainActivity"
android:orientation="vertical"
android:gravity="center">
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
/>
<EditText
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
/>
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加"
tools:layout_editor_absoluteX="140dp"
tools:layout_editor_absoluteY="77dp" />
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"
tools:layout_editor_absoluteX="140dp"
tools:layout_editor_absoluteY="155dp" />
<Button
android:id="@+id/update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改"
tools:layout_editor_absoluteX="140dp"
tools:layout_editor_absoluteY="231dp" />
<Button
android:id="@+id/select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询"
tools:layout_editor_absoluteX="142dp"
tools:layout_editor_absoluteY="315dp" />
</LinearLayout>
.MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText name;
private EditText age;
private Button add;
private Button delete;
private Button update;
private Button select;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
name = (EditText) findViewById(R.id.name);
age = (EditText) findViewById(R.id.age);
add = (Button) findViewById(R.id.add);
delete = (Button) findViewById(R.id.delete);
update = (Button) findViewById(R.id.update);
select = (Button) findViewById(R.id.select);
add.setOnClickListener(this);
delete.setOnClickListener(this);
update.setOnClickListener(this);
select.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add:
addData();
break;
case R.id.delete:
deleteData();
break;
case R.id.update:
updateData();
break;
case R.id.select:
selectData();
break;
}
}
private void selectData() {
List<User> users = App.getDaoSession().getUserDao().loadAll();
if(users==null || users.isEmpty()){
Toast.makeText(this, "数据库没有信息", Toast.LENGTH_SHORT).show();
return;
}
for (int x = 0; x <users.size() ; x++) {
User user1 = users.get(x);
Log.e("数据库信息",user1.getName()+"-----"+user1.getAge());
}
}
private void updateData() {
//修改是根据输入的名字去修改年龄
String nameUpdate = name.getText().toString().trim();
String ageUpdate = age.getText().toString().trim();
if(nameUpdate.equals("") || ageUpdate.equals("")){
Toast.makeText(this, "更改的数据不能为空", Toast.LENGTH_SHORT).show();
return;
}
int age = Integer.parseInt(ageUpdate);
UserDao userDao = App.getDaoSession().getUserDao();
List<User> users = userDao.loadAll();
if (users == null || users.isEmpty()) {
Toast.makeText(this, "数据库为空", Toast.LENGTH_SHORT).show();
return;
}
for (int x = 0; x <users.size() ; x++) {
User user = users.get(x);
if(user.getName().equals(nameUpdate)){
user.setAge(age);
App.getDaoSession().getUserDao().update(user);
Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
return;
}
}
Toast.makeText(this, "数据库没有该名字", Toast.LENGTH_SHORT).show();
}
private void deleteData() {
UserDao userDao = App.getDaoSession().getUserDao();
List<User> list = userDao.queryBuilder().list();
if (list == null || list.isEmpty()) {
Toast.makeText(this, "数据库为空", Toast.LENGTH_SHORT).show();
return;
}
for (int x = 0; x <list.size() ; x++) {
User user1 = list.get(x);
String userName = name.getText().toString().trim();
if(userName.equals("")){
Toast.makeText(this, "请输入要删除的名字", Toast.LENGTH_SHORT).show();
return;
}
if(user1.getName().equals(userName)){
userDao.delete(user1);
Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
return;
}
}
Toast.makeText(this, "没有该名字", Toast.LENGTH_SHORT).show();
}
private void addData() {
String nameString = name.getText().toString().trim();
String ageString = age.getText().toString().trim();
if(nameString.equals("") || ageString==null){
Toast.makeText(this, "不能为空", Toast.LENGTH_SHORT).show();
return;
}
List<User> users = App.getDaoSession().getUserDao().loadAll();
for (int x = 0; x <users.size() ; x++) {
User user1 = users.get(x);
if(user1.getName().equals(nameString)){
Toast.makeText(this, "名称已存在", Toast.LENGTH_SHORT).show();
return;
}
}
int ageInt = Integer.parseInt(ageString);
User user = new User();
user.setName(nameString);
user.setAge(ageInt);
App.getDaoSession().getUserDao().insert(user);
Toast.makeText(this, "插入成功", Toast.LENGTH_SHORT).show();
}
}