add DynamicTableMapper support and refactor SQL provider architecture
新增动态表名支持和 SQL 提供器架构重构,用于支持分表等动态表名场景: - 新增 DynamicTableMapper 接口,所有方法支持显式传入表名参数 - 新增 BaseSQLProvider 基类,包含所有 SQL 构建逻辑和实体元数据管理 - 重构 SQLProvider 为 BaseSQLProvider 的适配层,专注于 BaseMapper 参数适配 - 新增 DynamicTableSQLProvider 适配层,提供动态表名的 SQL 构建能力 - 通过参数前缀 (paramPrefix) 统一处理不同的参数绑定方式 - 消除所有代码重复,SQL 构建逻辑统一到基类 - 版本升级到 0.0.2 架构优势:单一职责、零重复、易扩展、易维护 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
package com.imyeyu.spring.util;
|
||||
|
||||
import com.imyeyu.spring.bean.Page;
|
||||
import com.imyeyu.spring.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.builder.annotation.ProviderContext;
|
||||
|
||||
/**
|
||||
* 支持动态表名的 SQL 提供器
|
||||
* <p>继承自 {@link BaseSQLProvider},为 {@link com.imyeyu.spring.mapper.DynamicTableMapper DynamicTableMapper} 提供适配层</p>
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2026-01-07 11:10
|
||||
*/
|
||||
public class DynamicTableSQLProvider extends BaseSQLProvider {
|
||||
|
||||
/**
|
||||
* 根据 Page 对象查询数据列表
|
||||
*
|
||||
* @param context 代理器上下文
|
||||
* @param tableName 表名
|
||||
* @param page 分页参数
|
||||
* @return SQL
|
||||
*/
|
||||
public String selectByPage(ProviderContext context, @Param("tableName") String tableName, @Param("page") Page<?> page) {
|
||||
EntityMeta meta = getEntityMeta(context);
|
||||
return buildSelectByPageSQL(meta, tableName, page, "#{page.offset}", "#{page.limit}");
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 Page 对象统计数据量
|
||||
*
|
||||
* @param context 代理器上下文
|
||||
* @param tableName 表名
|
||||
* @param page 分页参数
|
||||
* @return SQL
|
||||
*/
|
||||
public String countByPage(ProviderContext context, @Param("tableName") String tableName, @Param("page") Page<?> page) {
|
||||
EntityMeta meta = getEntityMeta(context);
|
||||
return buildCountByPageSQL(meta, tableName, page);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询全部数据
|
||||
*
|
||||
* @param context 代理器上下文
|
||||
* @param tableName 表名
|
||||
* @return SQL
|
||||
*/
|
||||
public String selectAll(ProviderContext context, @Param("tableName") String tableName) {
|
||||
EntityMeta meta = getEntityMeta(context);
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append("SELECT * FROM `%s` WHERE 1 = 1".formatted(tableName));
|
||||
if (meta.canDelete) {
|
||||
sql.append(BaseMapper.NOT_DELETE);
|
||||
}
|
||||
return sql.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入
|
||||
*
|
||||
* @param context 代理器上下文
|
||||
* @param tableName 表名
|
||||
* @param entity 实体
|
||||
* @return SQL
|
||||
*/
|
||||
public String insert(ProviderContext context, @Param("tableName") String tableName, @Param("entity") Object entity) {
|
||||
EntityMeta meta = getEntityMeta(context);
|
||||
return buildInsertSQL(meta, tableName, entity, "entity.");
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 ID 查询
|
||||
*
|
||||
* @param context 代理器上下文
|
||||
* @param tableName 表名
|
||||
* @param id ID
|
||||
* @return SQL
|
||||
*/
|
||||
public String select(ProviderContext context, @Param("tableName") String tableName, @Param("id") Object id) {
|
||||
EntityMeta meta = getEntityMeta(context);
|
||||
return buildSelectByIdSQL(meta, tableName, "id");
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据实体非空字段使用等号查询
|
||||
*
|
||||
* @param context 代理器上下文
|
||||
* @param tableName 表名
|
||||
* @param entity 实体
|
||||
* @return SQL
|
||||
*/
|
||||
public String selectByExample(ProviderContext context, @Param("tableName") String tableName, @Param("entity") Object entity) {
|
||||
return selectAllByExample(context, tableName, entity) + BaseMapper.LIMIT_1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据实体非空字段使用等号查询
|
||||
*
|
||||
* @param context 代理器上下文
|
||||
* @param tableName 表名
|
||||
* @param entity 实体
|
||||
* @return SQL
|
||||
*/
|
||||
public String selectAllByExample(ProviderContext context, @Param("tableName") String tableName, @Param("entity") Object entity) {
|
||||
EntityMeta meta = getEntityMeta(context);
|
||||
return buildSelectAllByExampleSQL(meta, tableName, entity, "entity.");
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 ID 更新
|
||||
*
|
||||
* @param context 代理器上下文
|
||||
* @param tableName 表名
|
||||
* @param entity 实体
|
||||
* @return SQL
|
||||
*/
|
||||
public String update(ProviderContext context, @Param("tableName") String tableName, @Param("entity") Object entity) {
|
||||
EntityMeta meta = getEntityMeta(context);
|
||||
return buildUpdateSQL(meta, tableName, entity, "entity.");
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 ID 更新,选择性更新非空属性
|
||||
*
|
||||
* @param context 代理器上下文
|
||||
* @param tableName 表名
|
||||
* @param entity 实体
|
||||
* @return SQL
|
||||
*/
|
||||
public String updateSelective(ProviderContext context, @Param("tableName") String tableName, @Param("entity") Object entity) {
|
||||
EntityMeta meta = getEntityMeta(context);
|
||||
return buildUpdateSelectiveSQL(meta, tableName, entity, "entity.");
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 ID 软删除
|
||||
*
|
||||
* @param context 代理器上下文
|
||||
* @param tableName 表名
|
||||
* @param id ID
|
||||
* @return SQL
|
||||
*/
|
||||
public String delete(ProviderContext context, @Param("tableName") String tableName, @Param("id") Object id) {
|
||||
EntityMeta meta = getEntityMeta(context);
|
||||
return buildDeleteSQL(meta, tableName, "id");
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据示例批量逻辑删除
|
||||
*
|
||||
* @param context 代理器上下文
|
||||
* @param tableName 表名
|
||||
* @param entity 实体
|
||||
* @return SQL
|
||||
*/
|
||||
public String deleteAllByExample(ProviderContext context, @Param("tableName") String tableName, @Param("entity") Object entity) {
|
||||
EntityMeta meta = getEntityMeta(context);
|
||||
return buildDeleteAllByExampleSQL(meta, tableName, entity, "entity.");
|
||||
}
|
||||
|
||||
/**
|
||||
* 硬删除
|
||||
*
|
||||
* @param context 代理器上下文
|
||||
* @param tableName 表名
|
||||
* @param id ID
|
||||
* @return SQL
|
||||
*/
|
||||
public String destroy(ProviderContext context, @Param("tableName") String tableName, @Param("id") Object id) {
|
||||
EntityMeta meta = getEntityMeta(context);
|
||||
return buildDestroySQL(meta, tableName, "id");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user