Skip to content

Latest commit

History

26 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

ClapSQL

特性

缩写全称释义
CClient客户端,无需考虑服务不可用问题
LLight轻量级,不依赖底层系统,无需安装任何依赖,有Java的地方就能跑
AAlmighty全能型,除了基本的CRUD,还具备分表、异步、批处理等能力
PPerformance性能高,内部使用缓存和批处理机制减少IO操作,哈希分表减少单个表文件处理的数据量,快速定位数据
其它数据可加密,管理访问权限,代码逻辑替代SQL语句的编写

意图

客户端的开发其实或多或少会使用到数据库,比如应用埋点的数据记录、用户数据的记录等等。

  • 客户端是基本无需考虑高并发带来的数据库压力的,所以大型的数据库根本就用不上;
  • 单个用户产生的数据量是可预见的,所以一个小而精悍的数据库就特别合适;
  • 少量的数据当然可以自己用文件解决,但是每次写IO流程的开发效率可想而知;
  • 关系型数据库基本都是需要编写SQL语句的,使用ClapSQL无需编写任何的SQL语句。

用法

  1. 需要自定义一个数据实体TestBean,并继承自SQLBean
publicclassTestBeanextendsSQLBean {
Stringkey;
Stringname = "default";
publicTestBean(Stringkey) {
this.key = key;
}
publicTestBean(Stringkey, Stringname) {
this.key = key;
this.name = name;
}
@OverridepublicvoidsetKey(Stringkey) {
this.key = key;
}
@OverrideStringgetKey() {
returnkey;
}
publicvoidsetName(Stringname) {
this.name = name;
}
publicStringgetName(){
returnname;
}
@OverridebooleanisSame(Objectanother) {
returntrue;
}
}
  1. 创建使用数据实体TestBean的SQL服务
publicclassServiceextendsSQLService<TestBean> {
publicService(StringdbPath) {
super(dbPath);
}
privatestaticfinalGsonGSON = newGson();
@OverrideStringencoderRow(TestBeanbean) {
/* 方式1 :将bean封装成json字符串 */returnGSON.toJson(bean);
/* 方式2 :自定义封装,可被解析成bean对象即可 return bean.getKey(); */
}
@OverrideTestBeandecoderRow(Stringrow) {
/* 方式1 :获取到的数据表中一行json字符串,需要解析成对应的bean对象 */returnGSON.fromJson(row, TestBean.class);
/* 方式2 :将字符串解析成对应对象 return new TestBean(row); */
}
}
  1. 使用Service实现简单的数据库操作
publicclassTest{
publicstaticvoidmain(String[] args) throwsIOException {
StringdbName = "db";
Stringtable = "test";
/* 创建数据库服务 */Serviceservice = newService(dbName);
/* 删除test表 */service.dropTable(table);
/* 创建test表 */service.createTable(table);
/* 测试插入数据 */for (inti = 0; i < 10000; i++) {
service.insert(table, newTestBean(i + ""));
}
/* 测试表更新 */// 按主键更新 : update test set name = 'update(50)' where key = 50;service.updateByKey(table, newTestBean("50", "update(50)"));
// 按条件和操作更新 : update test set name = 'update' where key > 0;service.updateByCondition(table,
/* 相当于SQL语句:‘where key > 0’ */bean -> Integer.parseInt(bean.key) > 0,
/* 相当于SQL语句:‘set name = 'update'’ */origin -> {
origin.setName("update");
returnorigin;
});
/* 测试查找数据 */// 查找全部 : select * from test;System.out.println("select all size = "+service.selectAll(table).size());
// 按主键查找 : select name from test where key = 50;System.out.println("select by key = 50 , result = " + service.selectByKey(table, "50").getName());
// 按条件查找 : select * from test where key = 10;System.out.println("select by key = 10 , size = " + service.selectByCondition(table, bean -> bean.getKey().equals("10")).size());
/* 测试删除数据 */// 根据主键删除 : delete test where key = 50;System.out.println("delete by key = 50 , result = " + service.deleteByKey(table, "50"));
// 根据条件删除 : delete test where key > 50;System.out.println("delete by key > 50 , size = " + service.deleteByCondition(table,
bean -> Integer.parseInt(bean.getKey()) > 50).size());
// 删除全部 : delete test;System.out.println("delete all size = " + service.deleteAll(table).size());
}
}

高级用法

  1. 使用批处理优化批量操作
publicclassBatchTest{
publicstaticvoidmain(String[] args){
// 测试批处理SQLBatch<TestBean> batch = newSQLBatch<>(service);
/* 测试批量插入 */ArrayList<TestBean> list = newArrayList<>();
for (inti = 0; i < 100000; i++) {
list.add(newTestBean(i + ""));
}
batch.insertBatch(table, list, respond -> System.out.println("insert into test1 result = " + respond));
/* 测试批量更新 */batch.updateBatch(table,
bean -> Integer.parseInt(bean.key) > 0,
origin -> {
origin.setName("update by batch");
returnorigin;
}, null);
/* 测试批量查找 */batch.selectBatch(table, bean -> Integer.parseInt(bean.key) >= 0, respond -> System.out.println("select test1 by batch beans size = " + respond.size()));
/* 测试批量删除 */batch.deleteBatch(table, bean -> Integer.parseInt(bean.key) >= 0, null);
}
}

相关文档

  1. ClapSQL 1.0 API文档

About

极简本地数据库,无需编写SQL语句,自带缓存、批处理等能力。

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages