remote-db是一款轻量级、高性能的Android端JDBC库,基于DBUtils扩展而来,保留了DBUtils的所有功能,还具备以下功能特点:
- 封装了常用CRUD 及批量CUD,支持sql查询自动映射成实体、集合(List,Map)
- 自定义了数据库连接池(负责分配、管理和释放数据库连接),支持配置多个数据源(连接不同的数据库)
- 支持同步和异步执行sql语句,支持注解注入dao实例(可配置单例还是多例),更方便地执行sql语句
- 只要引入对应的数据库驱动包,即可连接mysql、sqlserver、oracle等数据库
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
dependencies {
implementation 'com.github.kellysong:remote-db:1.1.0'
//mysql驱动包
implementation 'mysql:mysql-connector-java:5.0.8'
}
DbConfig dbConfig = new DbConfig.Builder()
.setDbName("mysql")//dbName是数据源名字,可以自定义,主要用于区分不同的数据源
.setActive(true)
.setDriverClass("com.mysql.jdbc.Driver")
.setJdbcUrl("jdbc:mysql://192.168.0.45:3306/test_db")
.setUsername("root")
.setPassword("mysqladmin")
.setInitialPoolSize(3)
.setMaxPoolSize(6)
.setKeepAliveTime(5000).build();
RemoteDb.get().initDataSource(dbConfig, new DbCallback() {
@Override
public void onSuccess() {
//必须在数据源初始化后,获取DaoExecutor;
}
@Override
public void onFailed(Throwable e) {
LogUtils.e("初始化数据源异常", e);
}
});
方法1:
(1)在需要使用DaoExecutor的地方使用@InjectDao注入DaoExecutor
@InjectDao(dbName = AppConstant.DB_MYSQL, isAsync = false)
SyncDaoExecutor syncDaoExecutor;//同步dao
@InjectDao(dbName = "mysql")
AsyncDaoExecutor asyncDaoExecutor;//异步dao
(2)处理注解
RemoteDb.get().inject(this);//参数可以任意实例,包括Activity,Fragment,Dao层,MVP的P层等
or 方法2:
SyncDaoExecutor syncDaoExecutor = RemoteDb.get().getSyncDaoExecutor("mysql");//同步dao
AsyncDaoExecutor asyncDaoExecutor =RemoteDb.get().getAsyncDaoExecutor("mysql");//异步dao
由于数据库连接请求也属于网络请求,故使用异步asyncDaoExecutor
//将结果查询为Bean
asyncDaoExecutor.queryBean(...){...}
//将结果查询为BeanList
asyncDaoExecutor.queryBeanList(...){...}
//分页查询
asyncDaoExecutor.queryPagination(...){...}
//将结果查询为Map
asyncDaoExecutor.queryBeanForMap(...){...}
//将结果查询为ListMap
asyncDaoExecutor.queryBeanForListMap(...){...}
//更新、修改,删除
asyncDaoExecutor.update(...){...}
//更新、修改,删除(带事务)
asyncDaoExecutor.updateInTx(...){...}
//批量更新、修改,删除(带事务)
asyncDaoExecutor.batchDeleteInTx(...){...}
RemoteDb.get().close();
更多用法参考博客:
https://blog.csdn.net/u011082160/article/details/96303046
-keep class com.sjl.remotedb.db.DbConfig{*;}
-keep class com.sjl.remotedb.db.DbXmlBean{*;}
-keep class com.sjl.remotedb.dao.SyncDaoExecutor {
public static SyncDaoExecutor init(java.lang.String);
}
-keep class com.sjl.remotedb.dao.AsyncDaoExecutor {
public static AsyncDaoExecutor init(java.lang.String);
}
-keep class org.apache.commons.dbutils.**{*;}
-keep class org.xmlpull.v1.** { *;}
-dontwarn org.xmlpull.v1.**
Copyright 2019 Song Jiali
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.