# custom-db-orm
**Repository Path**: openter/custom-db-orm
## Basic Information
- **Project Name**: custom-db-orm
- **Description**: 简易ORM操作工具,低代码,操作简单,功能强大,上手快,纯原生JDBC+阿里的Druid连接池,集成Mybatis-Plus的条件构造器,在此之上添加了多表连接的条件构造,使增删改查变得更容易,支持动态一对一(一对多)查询,支持ActiveRecord以及链式查询。
- **Primary Language**: Java
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 7
- **Forks**: 1
- **Created**: 2021-08-22
- **Last Updated**: 2025-06-05
## Categories & Tags
**Categories**: database-dev
**Tags**: ORM, JDBC, 数据库访问, SQL
## README
# custom-springboot-starter
### 简介
简易ORM操作工具,纯原生JDBC+阿里的Druid连接池,集成条件构造器,使用简单,一看就会,一用就爽,让增删改查变得更容易,支持```ActiveRecord```以及```链式查询```。
### 说明:
- ```com.custom.action.core.JdbcDao```,该类提供了多种增删改查方法以供用户自定义使用,使用时只需要在service或controller注入该对象即可,只需要编写部分的条件```sql```
,即可完成单表的大部分增删改查操作。
- 只需要创建实体类,并添加上自定义的几个注解,即可生成对应的表结构。
- 暂时只支持Mysql
- 支持可配置的表关联查询以及逻辑删除,```sql```语句打印输出,下划线转驼峰等功能。
- 该工具已完成```springboot```的自动配置,在```springboot```项目中引入该依赖即可,无需另外配置,轻松便捷。
#### 安装依赖
##### SpringBoot-自动配置
```xml
com.xb-custom
custom-springboot-starter
1.0.0
```
**springboot方式 -- 配置数据源**
```SpringBoot```项目配置数据源:因dataSource类为本工具自定义,所以在```application.yml(properties)```中进行如下基本配置即可,```mysql```
驱动默认为```mysql8.0```--->```com.mysql.cj.jdbc.Driver```(配置文件中可不写)
```properties
custom.db.datasource.url=jdbc:mysql://127.0.0.1:3306/hos?characterEncoding=utf-8&allowMultiQueries=true&autoreconnect=true&serverTimezone=UTC
custom.db.datasource.username=root
custom.db.datasource.password=123456
```
**纯依赖-手动配置**
```xml
com.xb-custom
custom-db-action
1.0.0
```
**手动方式 -- 配置数据源**
```java
// 数据库连接配置
DbDataSource dbDataSource = new DbDataSource();
dbDataSource.setUrl("jdbc:mysql://127.0.0.1:3306/hos?characterEncoding=utf-8&allowMultiQueries=true&autoreconnect=true&serverTimezone=UTC");
dbDataSource.setUsername("root");
dbDataSource.setPassword("123456");
// Driver不用填,默认mysql8.0
// 全局配置
DbGlobalConfig globalConfig = new DbGlobalConfig();
// 策略配置
DbCustomStrategy dbCustomStrategy = new DbCustomStrategy();
// sql打印开关
dbCustomStrategy.setSqlOutPrinting(true);
// sql打印时, true为可执行的sql(即参数?已经替换为真实的值), 默认false
dbCustomStrategy.setSqlOutPrintExecute(true);
// 是否下划线转驼峰?
dbCustomStrategy.setUnderlineToCamel(true);
// 逻辑删除的字段(表字段)
dbCustomStrategy.setDbFieldDeleteLogic("state");
// 逻辑删除的标识值
dbCustomStrategy.setDeleteLogicValue(1);
// 未逻辑删除的标识值
dbCustomStrategy.setNotDeleteLogicValue(0);
globalConfig.setStrategy(dbCustomStrategy);
```
#### 使用说明
```java
import com.custom.action.core.JdbcDao;
// 注入JdbcDao,即可使用
@Autowired
private JdbcDao jdbcDao;
```
1. 该工具提供大量的增删改查方法API
- 一般查询
```java
查询多条记录: 例1(and a.name = ?, "张三"),例2 (and a.name = "张三")
public List selectList(Class t, String condition, Object... params);
根据多个主键查询多条记录
public List selectListByKeys(Class t, Collection extends Serializable> keys);
根据sql查询多条记录: 例(select * from table)
public List selectListBySql(Class t, String sql, Object... params);
根据条件进行分页查询并排序: 例(and a.name = ?, 1, 10, "张三")
public DbPageRows selectPageRows(Class t, String condition, int pageIndex, int pageSize, Object... params);
根据主键查询一条记录: 例 (25)
public T selectOneByKey(Class t, Object key);
纯sql查询一条记录: 例(select * from table where age = ?, 25)
public T selectOneBySql(Class t, String sql, Object... params);
纯sql查询单个值: 例(select name from table where age = ?, 25)
public Object selectObjBySql(String sql, Object... params);
根据条件查询一条记录
public T selectOneByCondition(Class t, String condition, Object... params);
```
- 条件构造查询**(**与mybatis-plus的条件构造器相差无几)
`selectPageRows(查询分页)`
`selectList(查询多条)`
`selectCount(查询记录数)`
`selectOne(查询单条记录)`
`selectObj(查询单列字段,并且只有一个值,若有多个,只返回第一个满足条件的值)`
`selectObjs(同上,但允许会返回多个值)`
```java
public DbPageRows selectPageRows(ConditionWrapper wrapper);
public List selectList(ConditionWrapper wrapper);
public T selectOne(ConditionWrapper wrapper);
public long selectCount(ConditionWrapper wrapper);
public Object selectObj(ConditionWrapper wrapper);
public List