如何配置GreenDao
* 1:在项目的build.grade文件里加入一行代码 classpath 'org.greenrobot:greendao-gradle-plugin:3.2.0'
* 2:在app的build.grade里面加上代码 apply plugin: 'org.greenrobot.greendao'
* 3:在app的build.grad里面Android{}里加上代码
* greendao{
* schemaVersion 1 //指定数据库schema版本号,迁移等操作会用到
* //包名是活的这是变动的
* daoPackage 'soexample.umeng.com.greendaodemo.mydao' //dao的包名,包名默认是entity所在的包;
* targetGenDir 'src/main/java'//生成数据库文件的目录
* }
* 4:导入依赖
* 需要新建一个实体类 通过@Entity
* 通过锤子(make Project)
1.创建Bean包
@Entity
public class UserBean {
@Id(autoincrement = true)
private Long id;
private String name;
private String sex;
private int age;
@Generated(hash = 1291483711)
public UserBean(Long id, String name, String sex, int age) {
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
}
@Generated(hash = 1203313951)
public UserBean() {
}
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;
}
public String getSex() {
return this.sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
public UserBean(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
@Override
public String toString() {
return "UserBean{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
'}';
}
}
2.MyApp创建
public class MyApp extends Application {
private static DaoSession mDaoSessin;
@Override
public void onCreate() {
super.onCreate();
initGreenDao();
}
public void initGreenDao(){
DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(this, "config.db");
SQLiteDatabase database = openHelper.getWritableDatabase();
DaoMaster master = new DaoMaster(database);
mDaoSessin = master.newSession();
}
public static DaoSession getDaoSession(){
return mDaoSessin;
}
}
清单文件application中配置
android:name=".MyApp"
然后MainActivity使用
public class MainActivity extends AppCompatActivity {
private UserBeanDao mUserBeanDao;
private TextView mAdd;
private TextView mDel;
private TextView mUpdate;
private TextView mQuery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdd = findViewById(R.id.main_add);
mAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
add();
}
});
mDel = findViewById(R.id.main_del);
mDel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
del();
}
});
mUpdate = findViewById(R.id.main_update);
mUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
update();
}
});
mQuery = findViewById(R.id.main_query);
mQuery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
query();
}
});
mUserBeanDao = MyApp.getDaoSession().getUserBeanDao();
}
public void add(){
UserBean userBean = new UserBean("李四","男",30);
long insert = mUserBeanDao.insert(userBean);
if(insert>0){
Toast.makeText(MainActivity.this,"插入成功",Toast.LENGTH_SHORT).show();
}
}
public void del(){
mUserBeanDao.deleteByKey(1l);
Toast.makeText(MainActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
}
public void update(){
UserBean userBean = mUserBeanDao.load(1l);
userBean.setName("张三");
userBean.setSex("女");
userBean.setAge(40);
}
public void query(){
List<UserBean> list = mUserBeanDao.loadAll();
Log.e("数据",list+"");
}
}