177 lines
4.6 KiB
Java
177 lines
4.6 KiB
Java
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;
|
|
|
|
/**
|
|
* 通用 Mapper SQL 代理器
|
|
* <p>继承 {@link BaseSQLProvider},为 {@link BaseMapper} 提供适配层</p>
|
|
*
|
|
* @author 夜雨
|
|
* @since 2025-02-05 23:34
|
|
*/
|
|
public class SQLProvider extends BaseSQLProvider {
|
|
|
|
/**
|
|
* 创建 SQL 提供器
|
|
*/
|
|
public SQLProvider() {
|
|
}
|
|
|
|
/**
|
|
* 根据 Page 对象查询数据列表
|
|
*
|
|
* @param context 代理器上下文
|
|
* @param page 分页参数
|
|
* @return SQL
|
|
*/
|
|
public String selectByPage(ProviderContext context, @Param("page") Page<?> page) {
|
|
EntityMeta meta = getEntityMeta(context);
|
|
return buildSelectByPageSQL(meta, meta.table, page, "#{offset}", "#{limit}");
|
|
}
|
|
|
|
/**
|
|
* 根据 Page 对象统计数据量
|
|
*
|
|
* @param context 代理器上下文
|
|
* @param page 分页参数
|
|
* @return SQL
|
|
*/
|
|
public String countByPage(ProviderContext context, @Param("page") Page<?> page) {
|
|
EntityMeta meta = getEntityMeta(context);
|
|
return buildCountByPageSQL(meta, meta.table, page);
|
|
}
|
|
|
|
/**
|
|
* 查询全部数据
|
|
*
|
|
* @param context 代理器上下文
|
|
* @return SQL
|
|
*/
|
|
public String selectAll(ProviderContext context) {
|
|
EntityMeta meta = getEntityMeta(context);
|
|
StringBuilder sql = new StringBuilder();
|
|
sql.append("SELECT * FROM %s WHERE 1 = 1".formatted(meta.table));
|
|
if (meta.canDelete) {
|
|
sql.append(BaseMapper.NOT_DELETE);
|
|
}
|
|
return sql.toString();
|
|
}
|
|
|
|
/**
|
|
* 插入
|
|
* <p><i>不实现 {@link com.imyeyu.spring.entity.Creatable Creatable} 也允许调用是合理的,某些数据属于关联数据,不参与主创建过程</i></p>
|
|
*
|
|
* @param context 代理器上下文
|
|
* @param entity 实体
|
|
* @return SQL
|
|
*/
|
|
public String insert(ProviderContext context, Object entity) {
|
|
EntityMeta meta = getEntityMeta(entity.getClass());
|
|
return buildInsertSQL(meta, meta.table, entity, "");
|
|
}
|
|
|
|
/**
|
|
* 根据 ID 查询
|
|
*
|
|
* @param context 代理器上下文
|
|
* @param id ID
|
|
* @return SQL
|
|
*/
|
|
public String select(ProviderContext context, @Param("id") Object id) {
|
|
EntityMeta meta = getEntityMeta(context);
|
|
return buildSelectByIdSQL(meta, meta.table, "id");
|
|
}
|
|
|
|
/**
|
|
* 根据实体非空字段使用等号查询
|
|
*
|
|
* @param entity 实体
|
|
* @return SQL
|
|
*/
|
|
public String selectByExample(Object entity) {
|
|
return selectAllByExample(entity) + BaseMapper.LIMIT_1;
|
|
}
|
|
|
|
/**
|
|
* 根据实体非空字段使用等号查询
|
|
*
|
|
* @param entity 实体
|
|
* @return SQL
|
|
*/
|
|
public String selectAllByExample(Object entity) {
|
|
EntityMeta meta = getEntityMeta(entity.getClass());
|
|
return buildSelectAllByExampleSQL(meta, meta.table, entity, "");
|
|
}
|
|
|
|
/**
|
|
* 根据 ID 更新,需要实体实现 {@link com.imyeyu.spring.entity.Updatable Updatable}
|
|
*
|
|
* @param entity 实体
|
|
* @return SQL
|
|
*/
|
|
public String update(Object entity) {
|
|
EntityMeta meta = getEntityMeta(entity.getClass());
|
|
return buildUpdateSQL(meta, meta.table, entity, "");
|
|
}
|
|
|
|
/**
|
|
* 根据 ID 更新,选择性更新非空属性,需要实体实现 {@link com.imyeyu.spring.entity.Updatable Updatable}
|
|
*
|
|
* @param entity 实体
|
|
* @return SQL
|
|
*/
|
|
public String updateSelective(Object entity) {
|
|
EntityMeta meta = getEntityMeta(entity.getClass());
|
|
return buildUpdateSelectiveSQL(meta, meta.table, entity, "");
|
|
}
|
|
|
|
/**
|
|
* 根据 ID 软删除,需要实体实现 {@link com.imyeyu.spring.entity.Deletable Deletable}
|
|
*
|
|
* @param context 代理器上下文
|
|
* @param id ID
|
|
* @return SQL
|
|
*/
|
|
public String delete(ProviderContext context, @Param("id") Object id) {
|
|
EntityMeta meta = getEntityMeta(context);
|
|
return buildDeleteSQL(meta, meta.table, "id");
|
|
}
|
|
|
|
/**
|
|
* 根据示例批量逻辑删除
|
|
*
|
|
* @param entity 实体
|
|
* @return SQL
|
|
*/
|
|
public String deleteAllByExample(Object entity) {
|
|
EntityMeta meta = getEntityMeta(entity.getClass());
|
|
return buildDeleteAllByExampleSQL(meta, meta.table, entity, "");
|
|
}
|
|
|
|
/**
|
|
* 硬删除,需要实体实现 {@link com.imyeyu.spring.entity.Destroyable Destroyable}
|
|
*
|
|
* @param context 代理器上下文
|
|
* @param id ID
|
|
* @return SQL
|
|
*/
|
|
public String destroy(ProviderContext context, @Param("id") Object id) {
|
|
EntityMeta meta = getEntityMeta(context);
|
|
return buildDestroySQL(meta, meta.table, "id");
|
|
}
|
|
|
|
/**
|
|
* 根据示例批量销毁(物理删除)
|
|
*
|
|
* @param entity 实体
|
|
* @return SQL
|
|
*/
|
|
public String destroyAllByExample(Object entity) {
|
|
EntityMeta meta = getEntityMeta(entity.getClass());
|
|
return buildDestroyAllByExampleSQL(meta, meta.table, entity, "");
|
|
}
|
|
}
|