一、引用DevRing依赖
//导入DevRing依赖implementation 'com.ljy.ring:devring:1.1.8'创建数据库表的依赖implementation 'org.greenrobot:greendao:3.2.2' // add libraryimplementation 'org.greenrobot:greendao-generator:3.0.0'
二、修改工程目录下的.idea->gradle.xml文件,添加:
<option name="delegatedBuild" value="false" />
三、创建生成实体类的执行类
public class TableDaoGenerator {private static void addTaskDetail(Schema schema) {//实体类1Entity entity = schema.addEntity("User");//添加id属性//entity.addIdProperty();//添加列userId,指定非空,默认可为空entity.addLongProperty("Id").primaryKey();entity.addLongProperty("userId");entity.addStringProperty("realName");entity.addStringProperty("mobile");entity.addStringProperty("createTime");entity.addStringProperty("modifyTime");entity.addStringProperty("sex");entity.addStringProperty("userPhoto");entity.addStringProperty("certificateNumber");entity.addStringProperty("employeeNo");entity.addStringProperty("terminalPassword");entity.addIntProperty("userCabinetType");entity.addStringProperty("cardNum");entity.addStringProperty("spaceNos");entity.addStringProperty("fingerId");entity.addStringProperty("bindingUserTime");entity.addStringProperty("isTestTimeout");entity.addBooleanProperty("isDelete");entity.addIntProperty("userType");entity.addStringProperty("addFromTag");entity.addStringProperty("userTypeStr");//实体类2Entity entity2 = schema.addEntity("User01");//添加id属性//entity.addIdProperty();//添加列userId,指定非空,默认可为空entity2.addLongProperty("Id").primaryKey();entity2.addLongProperty("userId");entity2.addStringProperty("realName");entity2.addStringProperty("mobile");entity2.addStringProperty("createTime");entity2.addStringProperty("modifyTime");entity2.addStringProperty("sex");entity2.addStringProperty("userPhoto");entity2.addStringProperty("certificateNumber");entity2.addStringProperty("employeeNo");//实体类...}public static void main(String[] args) throws Exception {//第一个参数是数据库版本号,第二个参数是所在包名Schema schema = new Schema(1,"been");addTaskDetail(schema);try {//第二个参数是我们前面新建的空文件夹db,这里采用绝对路径的写法new DaoGenerator().generateAll(schema, "E:\\Android\\Projects\\SpDemo\\app\\src\\main\\java\\com\\example\\spdemo\\db");} catch (Exception e) {e.printStackTrace();}}}
执行即可在db包下生成对应的实体类和配置类:
注意:
自动生成的类中都需要手动导包,每个实体类的Dao类中都需要重写hasKey(对应实体类 entity)
四、创建每个数据库表对应的管理者类GreenTableManager(继承数据库管理者基类)
//User01表
public class User01GreenTableManager extends GreenTableManager<User01, Long> {private DaoSession mDaoSession;public User01GreenTableManager(DaoSession daoSession) {this.mDaoSession = daoSession;}@Overridepublic AbstractDao<been.User01, Long> getDao() {return mDaoSession.getUser01Dao();}
}//User表
public class UserGreenTableManager extends GreenTableManager<User, Long> {private DaoSession mDaoSession;public UserGreenTableManager(DaoSession daoSession) {this.mDaoSession = daoSession;}@Overridepublic AbstractDao<User, Long> getDao() {return mDaoSession.getUserDao();}
}
五、创建GreenDBManager类继承IDBManager接口
由于GreenDao的特殊性以及具体数据表的不确定,无法很好地集成到DevRing当中。 所以需要实现IDBManager接口,在实现类中手动增加实体类对应的GreenTableManager类。
/*** author: ljy* date: 2018/3/10* description:* 由于GreenDao的特殊性以及具体数据表的不确定,无法很好地集成到DevRing当中。* 所以需要实现IDBManager接口,并通过DevRing.configureDB()方法传入。* 1.在init()中对数据库进行初始化操作,如建库建表。* 2.在putTableManager()方法中将数据表管理者存进参数map中,请记清楚key值* 后面对数据表的操作是通过DevRing.tableManager(key)方法得到数据表管理者,然后进行相关增删改查。* 3.可在本类中添加IDBManager接口以外的方法* ,然后通过DevRing.<GreenDBManager>dbManager()来调用。**/public class GreenDBManager implements IDBManager {DaoSession mDaoSession;UserGreenTableManager userGreenTableManager;User01GreenTableManager user01GreenTableManager;@Overridepublic void init() {String dbName = "intelligent_cabinet.db";Integer dbVersion = DaoMaster.SCHEMA_VERSION;Class<? extends AbstractDao<?, ?>>[] classes = new Class[]{UserDao.class,User01Dao.class,};//这里使用DevRing提供的GreenOpenHelper对DaoMaster进行初始化,这样就可以实现数据库升级时的数据迁移//默认的DaoMaster.OpenHelper不具备数据迁移功能,它会在数据库升级时将数据删除。GreenOpenHelper openHelper = new GreenOpenHelper(DevRing.application(), dbName, dbVersion, classes);DaoMaster daoMaster = new DaoMaster(openHelper.getWritableDatabase());daoMaster.createAllTables(daoMaster.getDatabase(),true);
// DaoMaster daoMaster = new DaoMaster(greenOpenHelper.getEncryptedWritableDb("your_secret"));//加密mDaoSession = daoMaster.newSession();Log.i("初始化数据开始","初始化数据开始1====================================");userGreenTableManager = new UserGreenTableManager(mDaoSession);user01GreenTableManager = new User01GreenTableManager(mDaoSession);Log.i("初始化数据开始","初始化数据开始2====================================");//查看数据库更新版本时数据迁移的logMigrationHelper.DEBUG = false;//数据库增删改查时的logQueryBuilder.LOG_SQL = false;QueryBuilder.LOG_VALUES = false;//清空缓存mDaoSession.clear();}@Overridepublic void putTableManager(SimpleArrayMap<Object, ITableManger> mapTables) {mapTables.put(User.class,userGreenTableManager);mapTables.put(User01.class, user01GreenTableManager);}
}
六、在Application的onCreate中初始化DevRing和配置上面创建的数据库管理者(GreenDBManager )
public class MainApplication extends Application
{@Overridepublic void onCreate() {super.onCreate();//初始化DecRing框架DevRing.init(this);System.out.println("=======================配置数据库管理者开始==============================================");DevRing.configureDB(new GreenDBManager());//传入GreenDao数据库的管理者System.out.println("=======================配置数据库管理者==============================================");Log.i("TAG", "onCreate: 创建执行");DevRing.dbManager().init();//创建DevRing.create();}}
七、使用
这里以插入数据和读取数据为例:
User01 user01 = new User01();user01.setUserId((long)101011);user01.setEmployeeNo("dj10101");user01.setRealName("test01");user01.setMobile("123456");user01.setCreateTime("2024-06-20");user01.setMobile("2024-06-20");user01.setSex("男");user01.setUserPhoto("1888888888");user01.setCertificateNumber("00000");user01.setEmployeeNo("10001");//插入一条用户数据DevRing.tableManager(User01.class).insertOne(user01);//获取全部用户数据String user01Sql = "select * from USER01";List list = DevRing.tableManager(User01.class).queryBySQL(user01Sql, null);