参考博客:http://blog.csdn.net/qq_30034925/article/details/54729734
1.在project的build文件中配置
dependencies {
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
}
2.在module中进行配置
dependencies {
compile 'org.greenrobot:greendao:3.2.0'
compile 'org.greenrobot:greendao-generator:3.2.0'
}
接着在模块build.gradle的顶部添加
apply plugin: 'org.greenrobot.greendao'
3.在module中进行配置 android下
//GreenDao的个性配置 greendao { schemaVersion 1 //数据库版本 daoPackage 'com.sn.greendaoycf.dao' //自动生成工具类的包名 targetGenDir 'src/main/java' //路径 }4.代码
封装类里写数据库的字段
@Entity
public class User {
@Id
private Long id;
// @NotNull
private String text;
private String comment;
private java.util.Date date;
按住 ctrl+F9将会自动为我们生成需要的类和代码
5.创建App 继承Application 在清单文件中写application的name值
public class MyApp extends Application {
public static final boolean ENCRYPTED = true;
private DaoSession daoSession;
@Override
public void onCreate() {
super.onCreate();
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, ENCRYPTED ? "users-db-encrypted" : "users-db");
Database db = helper.getWritableDb();
daoSession = new DaoMaster(db).newSession();
}
public DaoSession getDaoSession() {
return daoSession;
}
6.数据库的应用
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private UserDao userDao;
private AutoCompleteTextView text;
private AutoCompleteTextView comment;
private Button butAddData;
private Button butShowData;
private Button butDelData;
private Button butModifyData;
private TextView tShow;
private EditText num;
private Query<User> userQuery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件
DaoSession daoSession = ((MyApp) getApplication()).getDaoSession();
userDao = daoSession.getUserDao();
initView();
}
private void initView() {
text = (AutoCompleteTextView) findViewById(R.id.edit_text);
comment = (AutoCompleteTextView) findViewById(R.id.edit_comment);
tShow = (TextView) findViewById(R.id.text_show_data);
num = (EditText) findViewById(R.id.exit_id);
butAddData = (Button) findViewById(R.id.but_add_data);
butShowData = (Button) findViewById(R.id.but_show_data);
butDelData = (Button) findViewById(R.id.but_del_data);
butModifyData = (Button) findViewById(R.id.but_modify_data);
butAddData.setOnClickListener(this);
butShowData.setOnClickListener(this);
butDelData.setOnClickListener(this);
butModifyData.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.but_add_data://增
String test = text.getText().toString();
String con = comment.getText().toString();
User user = new User();
user.setComment(con);
user.setDate(new Date());
user.setText(test);
userDao.insert(user);
text.setText("");
comment.setText("");
break;
case R.id.but_show_data://查询
//按找id z-a排序查询
// userQuery = userDao.queryBuilder().orderDesc(UserDao.Properties.Id).build();
//按找id a-a排序查询
userQuery = userDao.queryBuilder().orderAsc(UserDao.Properties.Id).build();
//查询满足指定属性值的结果
//userQuery = userDao.queryBuilder().where(UserDao.Properties.Text.eq(33),UserDao.Properties.Comment.eq(22)).build();
List<User> datalist= userQuery.list();
StringBuffer res = new StringBuffer();
for (User user1 : datalist) {
res.append("id= "+user1.getId());
res.append(" text="+user1.getText());
res.append(" comment="+user1.getComment()+"\n");
//res.append(" date="+user1.getDate()+"\n");
}
tShow.setText(res.toString());
break;
case R.id.but_del_data://删除
String index = num.getText().toString().trim();
if(TextUtils.isEmpty(index)){
Toast.makeText(MainActivity.this, "不能输入为空", Toast.LENGTH_SHORT).show();
return;
}
userDao.deleteByKey(Long.valueOf(index));
num.setText("");
break;
case R.id.but_modify_data://修改
String www = num.getText().toString().trim();
if(TextUtils.isEmpty(www)){
Toast.makeText(MainActivity.this, "不能输入为空", Toast.LENGTH_SHORT).show();
return;
}
String test2 = text.getText().toString();
String con2 = comment.getText().toString();
User user2 = new User();
user2.setId(Long.valueOf(www));
user2.setComment(con2);
user2.setDate(new Date());
user2.setText(test2);
userDao.update(user2);
text.setText("");
comment.setText("");
num.setText("");
break;
}
}
}
<LinearLayout
android:orientation="vertical"
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="com.bwei.administrator.greendao.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<AutoCompleteTextView
android:id="@+id/edit_comment"
android:layout_width="0dp"
android:hint="comment"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp" />
<AutoCompleteTextView
android:id="@+id/edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="text"
android:layout_weight="1"
android:padding="5dp"
/>
</LinearLayout>
<Button
android:id="@+id/but_add_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加数据"
android:padding="5dp"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#ff0"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示数据"
android:id="@+id/but_show_data"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_show_data"
android:layout_gravity="center"
android:gravity="center"
android:hint="这里将显示数据库中的数据"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/but_del_data"
android:text="删除"/>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="输入要删除或者要修改的id值"
android:id="@+id/exit_id"
android:inputType="number"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/but_modify_data"
android:text="修改"/>
</LinearLayout>