android中很多地方遇到离线缓存,我们需要把数据存储在本地
数据库框架很多,比如ormlite,但是我觉得比较好用的有greendao
官方网址
http://greenrobot.org/greendao/
怎么开始呢,查看这个文档
http://greenrobot.org/greendao/documentation/how-to-get-started/
添加依赖参考这个文档
http://greenrobot.org/greendao/documentation/introduction/
按照教程一步一步添加依赖就可以了
project的gradle文件修改如下
buildscript {
repositories {
google()
jcenter()
mavenCentral() // add repository
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
app的gradle文件修改如下
apply plugin: 'org.greenrobot.greendao' // apply plugin
implementation 'org.greenrobot:greendao:3.2.2' // add library
以下核心类是greenDAO的基本接口:
DaoMaster:使用greenDAO的切入点。DaoMaster保存数据库对象(SQLiteDatabase)并管理特定模式的DAO类(而不是对象)。它有静态方法来创建表或删除它们。其内部类OpenHelper和DevOpenHelper是SQLiteOpenHelper实现,它们在SQLite数据库中创建模式。
DaoSession:管理特定模式的所有可用DAO对象,您可以使用其中一个getter方法获取该对象。DaoSession还提供了一些通用的持久性方法,如实体的插入,加载,更新,刷新和删除。最后,DaoSession对象还跟踪身份范围。有关更多详细信息,请查看会话文档。
DAO:数据访问对象(DAO)持久存在并查询实体。对于每个实体,greenDAO生成DAO。它具有比DaoSession更多的持久性方法,例如:count,loadAll和insertInTx。
实体:可持久的对象。通常,实体是使用标准Java属性(如POJO或JavaBean)表示数据库行的对象。
然后我们写一个实体类,user用户的
代码如下:
@Entity(nameInDb = "USER")
public class User {
@Id
private Long id;
private String name;
private String sex;
private String age;
private String phone;
}
Entity代表这是一张表,ID必须为long类型,nameInDb = "USER"代表这张表的名字是USER
然后进入app的gradle文件下
添加如下代码
greendao {
schemaVersion 2
daoPackage "com.example.ceshi.DAO" // 根据实体类生产的三个dao文件放在哪儿
targetGenDir 'src/main/java'
}
这段代码与android{}这个节点平级
之后我们点make module
如图
项目就会自动生成我们的三个文件
如图:
在APP中初始化
MyApplication代码如下
public class MyApplication extends Application {
private static MyApplication app;
public static MyApplication getInstance() {
return app;
}
public DaoSession daoSession;
@Override
public void onCreate() {
super.onCreate();
app = this;
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "user-db");
Database db = helper.getWritableDb();
daoSession = new DaoMaster(db).newSession();
}
public DaoSession getDaoSession() {
return daoSession;
}
有了这些配置,我们可以简单的使用添加,查询功能
先上XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="姓名"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="手机"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/insert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="插入" />
<Button
android:id="@+id/query"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询" />
<TextView
android:id="@+id/zhanshi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询结果"/>
</LinearLayout>
主界面代码
public class MainActivity extends AppCompatActivity {
public EditText name,phone;
public Button insert,query;
public TextView zhanshi;
UserDao userDao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=findViewById(R.id.name);
insert=findViewById(R.id.insert);
phone=findViewById(R.id.phone);
query=findViewById(R.id.query);
zhanshi=findViewById(R.id.zhanshi);
DaoSession daoSession = ((MyApplication) getApplication()).getDaoSession();
userDao=daoSession.getUserDao();
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username=name.getText().toString();
String userphone=phone.getText().toString();
User user=new User();
user.setName(username);
user.setPhone(userphone);
userDao.insert(user);
}
});
query.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String userphone=phone.getText().toString();
List<User> a= userDao.queryBuilder().where(UserDao.Properties.Phone.eq(userphone)).list();
zhanshi.setText(a.get(0).getName());
}
});
效果图
简单的集成就好了!