package com.imyeyu.spring.mapper; import com.imyeyu.spring.bean.Logic; 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 映射接口 *

相比 {@link BaseMapper},所有方法都需要显式传入表名参数

* * @param 实体类型 * @param

主键类型 * @author 夜雨 * @since 2026-01-07 11:00 */ public interface DynamicTableMapper { /** * 根据 Page 对象查询数据列表 * * @param tableName 表名 * @param page 分页参数 * @return 数据列表 */ @SelectProvider(type = DynamicTableSQLProvider.class, method = "selectByPage") List selectByPage(@Param("tableName") String tableName, @Param("page") Page page); /** * 根据 Page 对象统计数据量 * * @param tableName 表名 * @param page 分页参数 * @return 数据量 */ @SelectProvider(type = DynamicTableSQLProvider.class, method = "countByPage") long countByPage(@Param("tableName") String tableName, @Param("page") Page page); /** * 分页查询 * * @param tableName 表名 * @param page 分页参数 * @return 分页结果 */ default PageResult selectPageResult(String tableName, Page page) { PageResult 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 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 数据对象 */ default T selectByExample(String tableName, T entity) { return selectByExample(tableName, entity, Logic.AND); } /** * 根据示例查询单条数据 * * @param tableName 表名 * @param entity 示例对象 * @param logic 条件连接逻辑 * @return 数据对象 */ @SelectProvider(type = DynamicTableSQLProvider.class, method = "selectByExample") T selectByExample(@Param("tableName") String tableName, @Param("entity") T entity, @Param("logic") Logic logic); /** * 根据示例查询全部数据 * * @param tableName 表名 * @param entity 示例对象 * @return 数据列表 */ default List selectAllByExample(String tableName, T entity) { return selectAllByExample(tableName, entity, Logic.AND); } /** * 根据示例查询全部数据 * * @param tableName 表名 * @param entity 示例对象 * @param logic 条件连接逻辑 * @return 数据列表 */ @SelectProvider(type = DynamicTableSQLProvider.class, method = "selectAllByExample") List selectAllByExample(@Param("tableName") String tableName, @Param("entity") T entity, @Param("logic") Logic logic); /** * 修改数据 * * @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 示例对象 */ default void deleteAllByExample(String tableName, T entity) { deleteAllByExample(tableName, entity, Logic.AND); } /** * 根据示例批量逻辑删除 * * @param tableName 表名 * @param entity 示例对象 * @param logic 条件连接逻辑 */ @UpdateProvider(type = DynamicTableSQLProvider.class, method = "deleteAllByExample") void deleteAllByExample(@Param("tableName") String tableName, @Param("entity") T entity, @Param("logic") Logic logic); /** * 销毁(物理删除) * * @param tableName 表名 * @param id 索引 */ @DeleteProvider(type = DynamicTableSQLProvider.class, method = "destroy") void destroy(@Param("tableName") String tableName, @Param("id") P id); /** * 根据示例批量销毁(物理删除) * * @param tableName 表名 * @param entity 示例对象 */ default void destroyAllByExample(String tableName, T entity) { destroyAllByExample(tableName, entity, Logic.AND); } /** * 根据示例批量销毁(物理删除) * * @param tableName 表名 * @param entity 示例对象 * @param logic 条件连接逻辑 */ @DeleteProvider(type = DynamicTableSQLProvider.class, method = "destroyAllByExample") void destroyAllByExample(@Param("tableName") String tableName, @Param("entity") T entity, @Param("logic") Logic logic); }