1.添加依赖
compile'org.greenrobot:greendao:3.0.1' compile'org.greenrobot:greendao-generator:3.0.0'
2.在app下build.gradle中添加配置
在最上面添加这一行
apply plugin: 'org.greenrobot.greendao'
在android下添加这几行分别是版本 包名 地址
greendao {
schemaVersion 1
daoPackage 'huxiangjun1508b20171106.bwie.com.greendaostudy'
targetGenDir 'src/main/java'
}
3.在项目的build.gradle中dependencies里添加
classpath 'org.greenrobot:greendao-gradle-plugin:3.0.0'
4.写一个bean类,切记不要忘了加注解,然后编译会出来三个类
@Entity
public class User {
@Id
private Long id;
@Property(nameInDb = "username")
private String username;
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
@Generated(hash = 79420138)
public User(Long id, String username) {
this.id = id;
this.username = username;
}
@Generated(hash = 586692638)
public User() {
}
}
5.创建一个MyApp初始化
public class MyApp extends Application {
private static MyApp myApp;
private DaoSession daoSession;
@Override
public void onCreate() {
super.onCreate();
myApp = this;
setDb();
}
public static MyApp getMyApp(){
return myApp;
}
private void setDb() {
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this , "mydb" , null);
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
}
public DaoSession getdaoSession(){
return daoSession;
}
}
6.在mainactivity中写逻辑
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button bt_add , bt_delete , bt_find , bt_updata;
private UserDao userDao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_add = (Button) findViewById(R.id.bt_add);
bt_delete = (Button) findViewById(R.id.bt_delete);
bt_find = (Button) findViewById(R.id.bt_find);
bt_updata = (Button) findViewById(R.id.bt_updata);
bt_add.setOnClickListener(this);
bt_delete.setOnClickListener(this);
bt_find.setOnClickListener(this);
bt_updata.setOnClickListener(this);
DaoSession daoSession = MyApp.getMyApp().getdaoSession();
userDao = daoSession.getUserDao();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_add:
List<User> users = new ArrayList<>();
User user = new User(Long.valueOf(1), "陈琳");
User user2 = new User(Long.valueOf(2), "胡相旭");
users.add(user);
users.add(user2);
for (int i = 0; i < users.size(); i++) {
userDao.insert(users.get(i));
}
Toast.makeText(this , "添加数据成功" , Toast.LENGTH_SHORT ).show();
break;
case R.id.bt_delete:
User delete = userDao.load(Long.valueOf(1));
userDao.delete(delete);
Toast.makeText(this , "删除数据成功" , Toast.LENGTH_SHORT ).show();
break;
case R.id.bt_find:
User load = userDao.load(Long.valueOf(1));
String username = load.getUsername();
Toast.makeText(this , "查询数据成功" + username , Toast.LENGTH_SHORT ).show();
List<User> userList = userDao.loadAll();
for (User userinfo : userList){
String username1 = userinfo.getUsername();
Toast.makeText(this , "查询数据成功" + username1 , Toast.LENGTH_SHORT ).show();
}
break;
case R.id.bt_updata:
User updata = userDao.load(Long.valueOf(1));
updata.setUsername("陈琳白痴");
userDao.update(updata);
break;
}
}
}
7.布局
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/bt_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加数据"
/>
<Button
android:id="@+id/bt_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除数据"
/>
<Button
android:id="@+id/bt_updata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="更新数据"
/>
<Button
android:id="@+id/bt_find"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询数据"
/>
</LinearLayout>