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:
Timi
2026-01-07 16:12:52 +08:00
parent 25dd7a5eb4
commit 7ac4cdae56
5 changed files with 1134 additions and 606 deletions

View File

@@ -0,0 +1,153 @@
package com.imyeyu.spring.mapper;
import com.imyeyu.spring.bean.Page;
import com.imyeyu.spring.bean.PageResult;
import com.imyeyu.spring.util.DynamicTableSQLProvider;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider;
import java.util.List;
/**
* 支持动态表名的 SQL 映射接口
* <p>相比 {@link BaseMapper},所有方法都需要显式传入表名参数</p>
*
* @param <T> 实体类型
* @param <P> 主键类型
* @author 夜雨
* @since 2026-01-07 11:00
*/
public interface DynamicTableMapper<T, P> {
/**
* 根据 Page 对象查询数据列表
*
* @param tableName 表名
* @param page 分页参数
* @return 数据列表
*/
@SelectProvider(type = DynamicTableSQLProvider.class, method = "selectByPage")
List<T> selectByPage(@Param("tableName") String tableName, @Param("page") Page<T> page);
/**
* 根据 Page 对象统计数据量
*
* @param tableName 表名
* @param page 分页参数
* @return 数据量
*/
@SelectProvider(type = DynamicTableSQLProvider.class, method = "countByPage")
long countByPage(@Param("tableName") String tableName, @Param("page") Page<T> page);
/**
* 分页查询
*
* @param tableName 表名
* @param page 分页参数
* @return 分页结果
*/
default PageResult<T> selectPageResult(String tableName, Page<T> page) {
PageResult<T> result = new PageResult<>();
result.setTotal(countByPage(tableName, page));
result.setList(selectByPage(tableName, page));
return result;
}
/**
* 查询全部数据
*
* @param tableName 表名
* @return 数据列表
*/
@SelectProvider(type = DynamicTableSQLProvider.class, method = "selectAll")
List<T> selectAll(@Param("tableName") String tableName);
/**
* 创建数据
*
* @param tableName 表名
* @param entity 数据对象
*/
@InsertProvider(type = DynamicTableSQLProvider.class, method = "insert")
@Options(useGeneratedKeys = true, keyProperty = "entity.id")
void insert(@Param("tableName") String tableName, @Param("entity") T entity);
/**
* 根据 ID 获取对象
*
* @param tableName 表名
* @param id 索引
* @return 数据对象
*/
@SelectProvider(type = DynamicTableSQLProvider.class, method = "select")
T select(@Param("tableName") String tableName, @Param("id") P id);
/**
* 根据示例查询单条数据
*
* @param tableName 表名
* @param entity 示例对象
* @return 数据对象
*/
@SelectProvider(type = DynamicTableSQLProvider.class, method = "selectByExample")
T selectByExample(@Param("tableName") String tableName, @Param("entity") T entity);
/**
* 根据示例查询全部数据
*
* @param tableName 表名
* @param entity 示例对象
* @return 数据列表
*/
@SelectProvider(type = DynamicTableSQLProvider.class, method = "selectAllByExample")
List<T> selectAllByExample(@Param("tableName") String tableName, @Param("entity") T entity);
/**
* 修改数据
*
* @param tableName 表名
* @param entity 数据对象
*/
@UpdateProvider(type = DynamicTableSQLProvider.class, method = "update")
void update(@Param("tableName") String tableName, @Param("entity") T entity);
/**
* 选择性更新
*
* @param tableName 表名
* @param entity 数据对象
*/
@UpdateProvider(type = DynamicTableSQLProvider.class, method = "updateSelective")
void updateSelective(@Param("tableName") String tableName, @Param("entity") T entity);
/**
* 软删除
*
* @param tableName 表名
* @param id 索引
*/
@UpdateProvider(type = DynamicTableSQLProvider.class, method = "delete")
void delete(@Param("tableName") String tableName, @Param("id") P id);
/**
* 根据示例批量逻辑删除
*
* @param tableName 表名
* @param entity 示例对象
*/
@UpdateProvider(type = DynamicTableSQLProvider.class, method = "deleteAllByExample")
void deleteAllByExample(@Param("tableName") String tableName, @Param("entity") T entity);
/**
* 销毁(物理删除)
*
* @param tableName 表名
* @param id 索引
*/
@DeleteProvider(type = DynamicTableSQLProvider.class, method = "destroy")
void destroy(@Param("tableName") String tableName, @Param("id") P id);
}