在工程里的build中添加:
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
在model里的build中添加:
apply plugin: 'org.greenrobot.greendao'
android里添加:
greendao {
schemaVersion 1
daoPackage 'com.koma.greendao.gen'
targetGenDir 'src/main/java'
}
最后添加依赖:
compile 'org.greenrobot:greendao:3.2.0'
添加User类:
@Entity
public class User {
@Id(autoincrement = true)
private Long id;
private String name;
@Transient
private int tempUsageCount;
@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;
}
}
编译项目,User实体类会自动编译,生成get、set方法并且会在com.anye.greendao.gen目录下生成三个文件:DaoMaster,DaoSesion,UserDao.
创建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;
}
MainActivity页面
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button mAdd,mDelete,mUpdate,mFind;
private TextView mContext;
private User mUser;
private UserDao mUserDao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = (TextView) findViewById(R.id.mContext);
mAdd = (Button) findViewById(R.id.button);
mDelete = (Button) findViewById(R.id.button2);
mUpdate = (Button) findViewById(R.id.button3);
mFind = (Button) findViewById(R.id.button4);
mAdd.setOnClickListener(this);
mDelete.setOnClickListener(this);
mUpdate.setOnClickListener(this);
mFind.setOnClickListener(this);
mUserDao = MyApplication.getInstances().getDaoSession().getUserDao();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
addDate();
break;
case R.id.button2:
deleteDate();
break;
case R.id.button3:
updateDate();
break;
case R.id.button4:
findDate();
break;
}
}
/**
* 增加数据
*/
private void addDate() {
mUser = new User(null,"anye3");
mUserDao.insert(mUser);
mContext.setText(mUser.getName());
}
/**
* 删除数据
*/
private void deleteDate() {
mUserDao.deleteAll();
}
/**
* 更改数据
*/
private void updateDate() {
mUser = new User((long)1,"anye0803");
mUserDao.update(mUser);
mContext.setText(mUser.getName());
}
/**
* 查找数据
*/
private void findDate() {
List<User> users = mUserDao.loadAll();
String userName = "";
for (int i = 0; i < users.size(); i++) {
userName += users.get(i).getName()+",";
}
mContext.setText("查询全部数据==>"+userName);
}
}
最后忘了在Application中添加 :android:name=".MyApplication"
main布局
<TextView
android:id="@+id/mContext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="添加"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="删除"
/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="修改"
/>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="查找"
/>