第一步 先引入对应的包
classpath ‘org.greenrobot:greendao-gradle-plugin:3.3.0’ // add plugin
apply plugin: ‘org.greenrobot.greendao’
implementation ‘org.greenrobot:greendao:3.3.0’
greendao {
schemaVersion 2 //数据库版本
daoPackage ‘net.china.snow.hxmeasure.db’ // 生成代码存放位置
targetGenDir ‘src/main/java’ //根目录
}
下面是对应的截图放在对应位置
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210318095157733.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM5NzUwMDU3,size_16,color_FFFFFF,t_70
第二步创建封装类
package net.china.snow.hxmeasure.util;
import android.app.Application;
import android.database.sqlite.SQLiteDatabase;
import net.china.snow.hxmeasure.db.DaoMaster;
import net.china.snow.hxmeasure.db.DaoSession;
/**
* @author Mcally
* @date: 2021年3月17日10:27:19
* @des 自定义的通用Application 类
*/
public class MyApplication extends Application {
private DaoMaster.DevOpenHelper mHelper;
private SQLiteDatabase db;
private DaoMaster mDaoMaster;
private DaoSession mDaoSession;
public static MyApplication instances;
@Override public void onCreate() {
super.onCreate();
instances = this;
setDatabase();
}
public static MyApplication getInstances(){
return instances;
}
/**
* 设置greenDAO
*/
private void setDatabase() {
// 通过 DaoMaster 的内部类 DevOpenHelper,你可以得到一个便利的 SQLiteOpenHelper 对象。
// 可能你已经注意到了,你并不需要去编写「CREATE TABLE」这样的 SQL 语句,因为 greenDAO已经帮你做了。
// 注意:默认的 DaoMaster.DevOpenHelper 会在数据库升级时,删除所有的表,意味着这将导致数据的丢失。
// 所以,在正式的项目中,你还应该做一层封装,来实现数据库的安全升级。
mHelper = new DaoMaster.DevOpenHelper(this, "measure-db", null);
db = mHelper.getWritableDatabase();
// 注意:该数据库连接属于 DaoMaster,所以多个 Session 指的是相同的数据库连接。
mDaoMaster = new DaoMaster(db);
mDaoSession = mDaoMaster.newSession();
}
public DaoSession getDaoSession() {
return mDaoSession;
}
public SQLiteDatabase getDb() {
return db;
}
}
第三步建立实体类
package net.china.snow.hxmeasure.entity;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.NotNull;
import org.greenrobot.greendao.annotation.Generated;
@Entity
public class DbEntity {
@Id(autoincrement = true)//设置自增长
private Long id;
@NotNull //不为空
private String ip;
@NotNull
private String userName;
@NotNull
private String passWord;
@NotNull
private String dbName;
@NotNull
private String dbType;
private Integer isDb;
}
执行上面操作代码就自动代码就生成了如下
第四步使用
package net.china.snow.hxmeasure.util;
import android.content.Context;
import android.os.Build;
import android.webkit.JavascriptInterface;
import androidx.annotation.RequiresApi;
import net.china.snow.hxmeasure.db.DbEntityDao;
import net.china.snow.hxmeasure.entity.DbEntity;
import java.util.List;
import java.util.Map;
/**
* @author Mcally
* @date: 2021年3月13日11:12:39
* @des Android 与 JS 交互 公共类
*/
public class AndroidAndJsInteractive {
Context content;
IWebViewComm comm;
//数据库操作类
DbEntityDao mDbEntityDao = MyApplication.getInstances().getDaoSession().getDbEntityDao();
public AndroidAndJsInteractive(Context content, IWebViewComm comm) {
this.content = content;
this.comm = comm;
}
// 定义JS需要调用的方法
// 被JS调用的方法必须加入@JavascriptInterface注解
@RequiresApi(api = Build.VERSION_CODES.N)
@JavascriptInterface
public String request(String data) {
Map<String, Object> map = JsonAnalysisUtil.JsontransMap(data);
switch (map.get("url").toString()) {
case "showActivity":
comm.showActivity(map.get("key").toString());
return JsonAnalysisUtil.SUCCESS("ok", "");
case "showToast":
comm.showToast(map.get("key").toString());
return JsonAnalysisUtil.SUCCESS("ok", "");
case "scanQR":
comm.scanQR();
return JsonAnalysisUtil.SUCCESS("ok", "");
case "saveDb":
return saveDB(map);
case "getDb":
return getDBList(map);
case "delDb":
return delDb(map);
case "updateDb":
return updateDb(map);
default:
return JsonAnalysisUtil.Fail("未有对应操作!!", "");
}
}
/**
* 保存Db一条数据
*
* @param map
* @return
*/
public String saveDB(Map<String, Object> map) {
try {
//{"DbName":"sdfds","DbType":"oracle","IP":"19266.155","Password":"sfs","UserName":"dsfa"}
Map<String, Object> data = JsonAnalysisUtil.JsontransMap(map.get("data").toString());
DbEntity entity = new DbEntity();
entity.setIp(data.get("IP").toString());
entity.setUserName(data.get("UserName").toString());
entity.setPassWord(data.get("Password").toString());
entity.setDbName(data.get("DbName").toString());
entity.setDbType(data.get("DbType").toString());
entity.setIsDb(0);
if (mDbEntityDao.insert(entity) > 0)
return JsonAnalysisUtil.SUCCESS("新增成功", "");
else
return JsonAnalysisUtil.Fail("新增失败", "");
} catch (Exception ex) {
return JsonAnalysisUtil.Fail(ex.getMessage(), "");
}
}
/**
* 获取Db 所有数据
*
* @param map
* @return
*/
public String getDBList(Map<String, Object> map) {
try {
return JsonAnalysisUtil.SUCCESS("ok", mDbEntityDao.loadAll());
} catch (Exception ex) {
return JsonAnalysisUtil.Fail(ex.getMessage(), "");
}
}
/**
* 删除DB
*
* @param map
* @return
*/
public String delDb(Map<String, Object> map) {
try {
DbEntity entity = JsonAnalysisUtil.JsontransEntity(map.get("data").toString(), DbEntity.class);
mDbEntityDao.delete(entity);
return JsonAnalysisUtil.SUCCESS("删除成功", "");
} catch (Exception ex) {
return JsonAnalysisUtil.Fail(ex.getMessage(), "");
}
}
/**
* 更新DB
*
* @param map
* @return
*/
@RequiresApi(api = Build.VERSION_CODES.N)
public String updateDb(Map<String, Object> map) {
try {
DbEntity entity = JsonAnalysisUtil.JsontransEntity(map.get("data").toString(), DbEntity.class);
entity.setIsDb(1);
List<DbEntity> ldbs = mDbEntityDao.loadAll();
ldbs.forEach(s -> s.setIsDb(0));
mDbEntityDao.updateInTx(ldbs);
mDbEntityDao.update(entity);
return JsonAnalysisUtil.SUCCESS("设置成功", "");
} catch (Exception ex) {
return JsonAnalysisUtil.Fail(ex.getMessage(), "");
}
}
}