Android本地数据库Greendao

1.build.grade中配置

在你的根build.gradle文件中

	buildscript {
    repositories {
        jcenter()
        mavenCentral() // add repository
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.1'
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin
    }
	}
 
	// In your app projects build.gradle file:
	//在你的应用程序项目build.gradle文件
	apply plugin: 'com.android.application'
	apply plugin: 'org.greenrobot.greendao' // apply plugin
 
	dependencies {
	//依赖
    implementation 'org.greenrobot:greendao:3.2.2' // add library
	}
	//更改路径
	greendao{
	schemaVersion 1          //指定数据库schema版本号,迁移等操作会用到包名是活的这是	变动的
 	daoPackage 'soexample.umeng.com.greendaodemo.mydao'        //切换为你自己的包名
 	targetGenDir 'src/main/java'         //生成数据库文件的目录
	}

2.建一个模型

建一个模型,此模型负责你要储存的数据

	@Entity
	public class NewBean {
    @Id
    private Long id;
    private String name;
    //写到这里后点击一下build-rebuild.proproject,重新生成
    @Generated(hash = 666424658)
    public NewBean(Long id, String name) {
        this.id = id;
        this.name = name;
    }
    @Generated(hash = 1213378749)
    public NewBean() {
    }
    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;
    }

    @Override
    public String toString() {
        return "NewBean{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
	}

3.新建一个类

新建一个类,继承Application

	public class App extends Application {
    private static DaoSession daoSession;

    @Override
    public void onCreate() {
        super.onCreate();

        //参数1:上下文
        //参数2:数据库名称
        //参数3:游标工厂类
        DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(this, "alex", null);
        //获取DataBase对象
        SQLiteDatabase db = openHelper.getWritableDatabase();
        //创建DaoMaster对象,所需DateBase数据库对象
        DaoMaster daoMaster = new DaoMaster(db);
        //创建DaoSession对象
        daoSession = daoMaster.newSession();
  	  }

   	 public static DaoSession getDaoSession() {
        return daoSession;
 	   }
	}

4.在activity或fragment中操作

Activity中crud操作

	public class MainActivity extends AppCompatActivity {

    @BindView(R.id.btn_add)
    Button btnAdd;
    @BindView(R.id.btn_query)
    Button btnQuery;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.btn_delete)
    Button btnDelete;
    @BindView(R.id.btn_update)
    Button btnUpdate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn_add, R.id.btn_delete, R.id.btn_update, R.id.btn_query})
    public void onViewClicked(View view) {
        NewBeanDao dao = NewApp.getDao().getNewBeanDao();
        NewBean bean=new NewBean();
        switch (view.getId()) {
            case R.id.btn_add:
                //增加数据(死数据)
                for (int i = 0; i < 10; i++) {
                    User user = new User();
                    user.setName("司马" + i);
                    userDao.insertOrReplace(user);
                }
                //将bean增加到自己的集合中,newslist为你解析出来的集合:
                for(int i=0;i<newslist.size();i++)
                {
                    bean.setName(newslist.get(i).getTitle());
                    dao.insertOrReplace(bean);
                }
                break;
            case R.id.btn_delete:
                //删除数据
                userDao.deleteByKey((long) 1);
                //全删
                dao.deleteAll();
                break;
            case R.id.btn_update:
            	//修改一条
                User cusomter = new User();
                cusomter.setId((long) 1);
                cusomter.setName("疾风剑豪");
				//修改十条
                User stundent = new User();
                stundent.setId((long) 10);
                stundent.setName("Tom");
				
                User users[] = new User[]{cusomter,stundent};
                //userDao.update(cusomter);
                userDao.updateInTx(users);
                break;
            case R.id.btn_query:
            	//查询一条
                User user = userDao.load((long) 1);
                String userInfo = user.toString();
                tvContent.setText(userInfo);

                /*List userList = userDao.loadAll();
                //借助一个字符串缓冲区
                StringBuilder sb = new StringBuilder();
                //遍历循环获取所有内容
                for (int i = 0; i < userList.size(); i++) {
                    User user = userList.get(i);
                    String userInfo = user.toString();
                    sb.append(userInfo + "---");
                }
                //查询到的所有数据设置给TextView进行显示
                String queryAllInfo = sb.toString();
                tvContent.setText(queryAllInfo);*/
                break;
        }
    }
	}

不要忘记在manifest中声明哦

android:name=".MyApp"

创作不易,仅供参考,如有问题,评论留言,如有雷同,纯属巧合!

你可能感兴趣的:(Android本地数据库Greendao)