194 lines
5.9 KiB
Java
194 lines
5.9 KiB
Java
package com.imyeyu.spring.util;
|
|
|
|
import com.imyeyu.spring.bean.Logic;
|
|
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 实体
|
|
* @param logic 条件连接逻辑
|
|
* @return SQL
|
|
*/
|
|
public String selectByExample(ProviderContext context, @Param("tableName") String tableName, @Param("entity") Object entity, @Param("logic") Logic logic) {
|
|
return selectAllByExample(context, tableName, entity, logic) + BaseMapper.LIMIT_1;
|
|
}
|
|
|
|
/**
|
|
* 根据实体非空字段使用等号查询
|
|
*
|
|
* @param context 代理器上下文
|
|
* @param tableName 表名
|
|
* @param entity 实体
|
|
* @param logic 条件连接逻辑
|
|
* @return SQL
|
|
*/
|
|
public String selectAllByExample(ProviderContext context, @Param("tableName") String tableName, @Param("entity") Object entity, @Param("logic") Logic logic) {
|
|
EntityMeta meta = getEntityMeta(context);
|
|
return buildSelectAllByExampleSQL(meta, tableName, entity, "entity.", logic);
|
|
}
|
|
|
|
/**
|
|
* 根据 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 实体
|
|
* @param logic 条件连接逻辑
|
|
* @return SQL
|
|
*/
|
|
public String deleteAllByExample(ProviderContext context, @Param("tableName") String tableName, @Param("entity") Object entity, @Param("logic") Logic logic) {
|
|
EntityMeta meta = getEntityMeta(context);
|
|
return buildDeleteAllByExampleSQL(meta, tableName, entity, "entity.", logic);
|
|
}
|
|
|
|
/**
|
|
* 硬删除
|
|
*
|
|
* @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");
|
|
}
|
|
|
|
/**
|
|
* 根据示例批量物理删除
|
|
*
|
|
* @param context 代理器上下文
|
|
* @param tableName 表名
|
|
* @param entity 实体
|
|
* @param logic 条件连接逻辑
|
|
* @return SQL
|
|
*/
|
|
public String destroyAllByExample(ProviderContext context, @Param("tableName") String tableName, @Param("entity") Object entity, @Param("logic") Logic logic) {
|
|
EntityMeta meta = getEntityMeta(context);
|
|
return buildDestroyAllByExampleSQL(meta, tableName, entity, "entity.", logic);
|
|
}
|
|
}
|