使用GreenDao框架首先要进行环境配置
一.首先再moudle gradle中添加环境配置
1.在顶部添加apply plugin: ‘org.greenrobot.greendao’
2.在android中添加
greendao{
schemaVersion 1 //指定数据库schema版本号,迁移等操作会用到
daoPackage ‘com.example.greendac.greendao’//包名是活的这是变动的
targetGenDir ‘src/main/java’//生成数据库文件的目录(不用改)
3.在dependencies中添加
compile ‘org.greenrobot:greendao:3.2.2’
二.接着在Project gradle中添加环境配置
在dependencies**中添加 classpath ‘org.greenrobot:greendao-gradle-plugin:3.2.0’
到此环境搭建就完成了
三.开始写代码
创建Category 实体类
@Entity //这个记得加上 不然链接不了数据库
public class Category {
@Id
private Long cat_id;//商品分类id
private String name;
private int parent_id;
private String cat_path;
private int goods_counth;
private int sort;
private int type_id;
private int list_show;
private String image;
然后点击绿色锤子 如下图
就会生成Category 实体Dao包
创建GreenDaoManager类
public class GreenDaoManager {
private static GreenDaoManager mInstance;
private DaoMaster daoMaster;//它以一定模式来管理我们Dao类中的数据对象,并创建DaoSession
private DaoSession daoSession;//他负责创建和管理Dao
public GreenDaoManager(){
if (mInstance==null){
DaoMaster.DevOpenHelper devOpenHelper=new DaoMaster.DevOpenHelper(MyApplication.getContext(),"mydb");
daoMaster=new DaoMaster(devOpenHelper.getWritableDb());
daoSession=daoMaster.newSession();
}
}
public static GreenDaoManager getmInstance(){
if (mInstance==null){
synchronized (GreenDaoManager.class){
if (mInstance==null){
mInstance=new GreenDaoManager();
}
}
}
return mInstance;
}
public DaoMaster getDaoMaster() {
return daoMaster;
}
public void setDaoMaster(DaoMaster daoMaster) {
this.daoMaster = daoMaster;
}
public DaoSession getDaoSession() {
return daoSession;
}
public void setDaoSession(DaoSession daoSession) {
this.daoSession = daoSession;
}
}
创建MyApplication 类
public class MyApplication extends Application {
private static Context context;
public static Context getContext() {
return context;
}
@Override
public void onCreate() {
super.onCreate();
context=getApplicationContext();
//全局配置GreenDao
GreenDaoManager.getmInstance();
}
}
编写页面布局activity_main3
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/btn_add"
android:text="@string/add"
android:onClick="add"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/btn_delate"
android:text="@string/delate"
android:onClick="delate"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/btn_update"
android:text="@string/update"
android:onClick="update"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/btn_query"
android:text="@string/query"
android:onClick="query"
/>
</LinearLayout>
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
Main3Activity代码
public class Main3Activity extends AppCompatActivity{
private static final String TAG=“Main3Activity”;
private ListView listView;
private CategoryDao categoryDao;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
listView= (ListView) findViewById(R.id.lv);
categoryDao=GreenDaoManager.getmInstance().getDaoSession().getCategoryDao();
}
public void add(View view){//增加数据
long id1= categoryDao.insert(new Category(“牛奶”,300));
categoryDao.insert(new Category(“酸奶”,400));
categoryDao.insert(new Category(“红酒”,600));
categoryDao.insert(new Category(“白酒”,700));
categoryDao.insert(new Category(“矿泉水”,200));
categoryDao.insert(new Category(“开水”,500));
Log.i(TAG,"add"+id1);
}
public void delate(View view){
categoryDao.deleteByKey(4L);//删除id为4 的数据
}
public void update(View view){
Category entiy=categoryDao.load(2L);//更改id为2 的数据内容
entiy.setName("baba");
entiy.setGoods_counth(1);
categoryDao.update(entiy);
}
public void query(View view){ //查询数据
List<Category> list= categoryDao.loadAll();
List<String>data=new ArrayList<>();
for(Category cat:list){
data.add("id"+cat.getCat_id()+",name"+cat.getName()+",count"+cat.getGoods_counth());
}
ArrayAdapter adapter=new ArrayAdapter(Main3Activity.this,android.R.layout.simple_list_item_1,data);
listView.setAdapter(adapter);
}
}