{"id":19545713,"url":"https://github.com/1024-lab/smartdb","last_synced_at":"2025-04-26T19:32:04.473Z","repository":{"id":47106579,"uuid":"303365296","full_name":"1024-lab/smartdb","owner":"1024-lab","description":"SmartDb是以SQL为中心，支持多数据源、读写分离，同时又不与原有项目mybatis、hibernate冲突的非常轻量级的Java ORM框架。 https://zhuoluodada.cn/smartdb","archived":false,"fork":false,"pushed_at":"2024-03-17T15:00:52.000Z","size":115,"stargazers_count":10,"open_issues_count":1,"forks_count":7,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-19T14:59:10.127Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/1024-lab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-10-12T11:08:10.000Z","updated_at":"2024-10-21T01:11:13.000Z","dependencies_parsed_at":"2024-11-11T03:41:14.815Z","dependency_job_id":"c4b1dfaa-3dab-4f5d-89a3-e19302ee51e2","html_url":"https://github.com/1024-lab/smartdb","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1024-lab%2Fsmartdb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1024-lab%2Fsmartdb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1024-lab%2Fsmartdb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1024-lab%2Fsmartdb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/1024-lab","download_url":"https://codeload.github.com/1024-lab/smartdb/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251041485,"owners_count":21527204,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T03:40:44.570Z","updated_at":"2025-04-26T19:32:03.156Z","avatar_url":"https://github.com/1024-lab.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"### SmartDb  卓大观点\n【卓大观点】互联网已经发展了二十年，每个企业均有一定的数字化发展，但随着“产业互联网”，“新基建”，“国产数据库”，“国产开源框架”等时代的到来，越来越多的企业在原有数字化（原有各类数据库）的基础之上，需要再向上构建一层新的数据聚合，以此来适应产业互联网的发展需要。所以当下的系统都会在原有数据库基础之上连接其他两个、甚至多个数据库，故一个支持多数据库、多数据源且轻量级的java数据库中间件是及其需要的! \n\n **所以必须有一个能5分钟上手，就可以完美解决多数据库、多数据源问题的框架 。** \n\n### SmartDb特点\nmybatis现在在国内已经是一家独大，而且也非常好用，笔者也推荐在大型项目中使用mybatis这种框架，但是总有一些场景，mybatis显得有些力不从心，这个时候选择SmartDb是一个不错的选择。\n\n- 在原有mybatis或hibernate连接其他数据库进行一些数据处理\n- 读写分离场景，有些数据需要操作主库，有些操作从库\n- 多数据源场景，要从mysql读出数据，然后进行转换存储到sqlserver中，等等\n- 在一个方法中进行多个数据源的数据库操作\n- 一个简单并追求开发效率的小型项目，例如 [SmartBlog（卓大的官方博客）](https://zhuoluodada.cn \"SmartBlog（一个精心雕琢的博客）\")\n- 不想使用sharding-sphere,sharding-jdbc, mycat，cobar等重型读写分离框架\n- 等等其他\n\n\n\n基于以上种种，设计了一个支持多数据源、读写分离的轻量级ORM框架:SmartDb, 让使用者有个极致的开发体验。\n\n### SmartDb简介\n**SmartDb** 是以SQL为中心，支持多数据源、读写分离，同时又不与原有项目mybatis、hibernate冲突的非常轻量级的Java ORM框架。\n\n特性如下：\n- 支持ORM操作、常见增删改查、批量、分页等功能\n- 支持MySQL、Oracle、Postgresql、SqlServer\n- 支持在mybatis、hibernate等项目上引入SmartDb\n- 支持多数据源\n- 支持分写分离\n- 支持枚举Enum映射\n- 支持Spring和Spring事务\n- 支持SQL链式API\n- 支持代码生成\n- 支持 filter 过滤器\n- 所有类均可扩展\n- 支持Java 6 ( **收费，收费是为了更好的伺候 “客官 ” ** )\n\n### SmartDb 文档\ngithub:  [https://github.com/1024-lab/smartdb](https://github.com/1024-lab/smartdb \"https://github.com/1024-lab/smartdb\")\n\ngitee:  [https://gitee.com/lab1024/smartdb](https://gitee.com/lab1024/smartdb \"https://gitee.com/lab1024/smartdb\")\n\n官方文档:  [https://zhuoluodada.cn/smartdb](https://zhuoluodada.cn/smartdb \"https://zhuoluodada.cn/smartdb\")\n\n例子:  [https://gitee.com/lab1024/smartdb-demos](https://gitee.com/lab1024/smartdb-demos)\n\n### SmartDb演示\n#### SmartDb POM\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003enet.1024lab\u003c/groupId\u003e\n  \u003cartifactId\u003esmartdb\u003c/artifactId\u003e\n  \u003cversion\u003e1.2.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n#### SmartDb 操作多数据源\n在一个方法中操作多个数据库，不用再使用注解切换来切换去，还容易出错， oh yeah~\n```java\npublic void multiDatasource(UserEntity userEntity) {\n    // mysqlSmartDb 对象为 连接SqlServer的SmartDb\n    SmartDb mysqlSmartDb = build1(); \n    // sqlServerSmartDb 对象为 连接SqlServer的SmartDb\n    SmartDb sqlServerSmartDb = build2();\n    // 将数据插入mysql数据库\n    mysqlSmartDb.insert(userEntity);\n    // 将数据插入sql server数据库\n    sqlServerSmartDb.insert(userEntity);\n}\n```\n#### SmartDb ORM操作\n支持简单的orm操作\n```java\nUserEntity userEntity = new UserEntity(1, \"zhuoda\");\n// 插入\nsmartDb.insert(userEntity);\nsmartDb.insertSelective(userEntity); // 非空字段插入\nsmartDb.batchInsert(userList);//批量插入\n// 更新\nsmartDb.update(userEntity);  \nsmartDb.updateSelective(userEntity); // 非空字段更新\n// 删除\nsmartDb.delete(userEntity);//根据主键删除\n```\n#### SmartDb 读写分离操作\n```java\n//默认操作 都是操作从库\nList\u003cUserEntity\u003e userList = smartDb.selectSqlBuilder()\n        .select(\"*\")\n        .from(UserEntity.class)\n        .queryList(UserEntity.class);\n\n//获取写库\nSmartDb writeSmartDb = smartDb.getMaster();\n//将数据写入主库\nwriteSmartDb.insert(userEntity);\n//进行主库其他操作\nwriteSmartDb.delete(userEntity);\nwriteSmartDb.updateSelective(userEntity);\n```\n\n#### SmartDb 链式Builder操作\nsmartdb为了支持快速开发，支持增删查改四种链式Builder操作\n##### SmartDb 链式SelectBuilder操作\n支持and、or、in、like、group、having、limit等等\n```java\nUserEntity userEntity = smartDb.selectSqlBuilder()\n        .select(\"user.*\")\n        .select(\"score.*\")\n        .from(UserEntity.class)\n        .joinLeft(\"t_score on t_user.id = t_score.user_id\")\n        .whereAnd(\"user.id = ?\", 1)\n        .whereAnd(\"type = 3\")\n        .whereAndIn(\" status \", Arrays.asList(1, 2, 3))\n        .groupby(\"user.type\")\n        .havingAnd(\"user.type = 1\")\n        .queryFirst(UserEntity.class);\n```\n##### SmartDb 链式InsertBuilder操作\n```java\nsmartDb.insertSqlBuilder()\n        .table(\"t_user\")\n        .insertColumn(\"id\",1)\n        .insertColumn(\"name\",\"smartdb\")\n        .insertFunctionColumn(\"login_time\",\"now()\") //sql方法\n        .execute();\n```\n##### SmartDb 链式UpdateBuilder操作\n更新操作支持and、or、in、like、等等\n```java\nsmartDb.updateSqlBuilder()\n        .table(\"t_user\")\n        .updateColumn(\"name = ?\",\"smart\")\n        .updateColumn(\" login_time = now()\")\n        .whereAnd(\"id = 1\")\n        .execute();\n```\n##### SmartDb 链式DeleteBuilder操作\n更新操作支持and、or、in、like、等等\n```java\nsmartDb.deleteSqlBuilder()\n        .table(\"t_user\")\n        .whereAnd(\"id = 1\")\n        .execute();\n```\n##### SmartDb 链式ReplaceBuilder操作\n```java\n((MysqlSmartDb)smartDb).replaceSqlBuilder()\n        .table(\"t_user\")\n        .replaceColumn(\"id\",1)\n        .replaceColumn(\"name\",\"smartdb\")\n        .replaceFunctionColumn(\"login_time\",\"now()\")\n        .execute();\n```\n#### 创建 SmartDb \n```java\nSmartDb smartDb = \n\tSmartDbBuilder.create()\n\t//设置 写库 数据源\n\t.setMasterDataSource(writeDataSource)\n\t//设置 两个读库 数据源\n\t.setSlaveDataSource(readDataSource1,readDataSource2)\n\t// 打印 info 级别sql\n\t.setShowSql(true)\n\t//设置数据库类型\n\t.setSupportDatabaseType(SupportDatabaseType.MYSQL)\n\t//设置支持spring\n\t.setSmartDbExtEnum(SmartDbExtEnum.SPRING5)\n\t//表名与类名转换\n\t.setTableNameConverter(cls -\u003e \"t_\" + CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, cls.getSimpleName()))\n\t//列名字 转换\n\t.setColumnNameConverter(new CaseFormatColumnNameConverter(CaseFormat.LOWER_CAMEL, CaseFormat.LOWER_UNDERSCORE))\n\t.build();\n```\n\n\n#### SmartDb 文档\n\n官方文档:  [https://zhuoluodada.cn/smartdb](https://zhuoluodada.cn/smartdb \"https://zhuoluodada.cn/smartdb\")\n\n例子:  [https://gitee.com/lab1024/smartdb-demos](https://gitee.com/lab1024/smartdb-demos)\n\n---\n\n#### 联系我们 :\n[1024创新实验室](https://www.1024lab.net/)\n\n#### SmartDb微信交流群（**加我微信拉你入群，和小伙伴们一起探讨！**）\n![](https://zhuoluodada.cn/cdn/images/zhuoda/zhuoda-wechat.jpg)\n\n#### 捐赠\n开源不易，感谢捐赠\n\u003e*佛祖保佑捐赠这些人写程序永无bug，工资翻倍，迎娶白富美，走上人生巅峰！*\n\n![](https://zhuoluodada.cn/cdn/images/zhuoda/zhuoda-wechat-money-v1.jpg)\n\n---\n作者简介:\n[卓大](https://zhuoluodada.cn)， 1024创新实验室主任，混迹于各个技术圈，研究过计算机，熟悉点java，略懂点前端。\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F1024-lab%2Fsmartdb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F1024-lab%2Fsmartdb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F1024-lab%2Fsmartdb/lists"}