Compare commits

...

3 Commits

Author SHA1 Message Date
25dd7a5eb4 add RawMapper 2026-01-05 14:55:58 +08:00
430921a16c fix BaseMapper.NOT_DELETE invalid 2026-01-05 12:59:52 +08:00
77f9feb1a1 remove not usage length 2026-01-05 11:56:56 +08:00
4 changed files with 107 additions and 9 deletions

View File

@ -14,9 +14,6 @@ public class RequestRange {
/** 结束值 */
private long end;
/** 范围长度 */
private long length;
/**
* 创建请求范围
*

View File

@ -12,7 +12,7 @@ import org.apache.ibatis.annotations.UpdateProvider;
import java.util.List;
/**
* 基本 SQL 映射,子接口可以不实现
* 基本 SQL 映射
*
* @param <T> 实体类型
* @param <P> 主键类型
@ -36,15 +36,15 @@ public interface BaseMapper<T, P> {
DESC
}
/** 当前时间戳毫秒 */
String UNIX_TIME = " FLOOR(UNIX_TIMESTAMP(NOW(3)) * 1000) ";
/** 未删除条件 */
String NOT_DELETE = " AND `deleted_at` IS NULL ";
String NOT_DELETE = " AND (`deleted_at` IS NULL OR " + UNIX_TIME + " < `deleted_at`) ";
/** 限制一条 */
String LIMIT_1 = " LIMIT 1";
/** 当前时间戳毫秒 */
String UNIX_TIME = " FLOOR(UNIX_TIMESTAMP(NOW(3)) * 1000) ";
/** 分页限制 */
String PAGE = NOT_DELETE + " LIMIT #{offset}, #{limit}";
@ -88,7 +88,7 @@ public interface BaseMapper<T, P> {
List<T> selectAll();
/**
* 创建数据。默认自增主键为 id如需修改请重写此接口
* 创建数据
*
* @param t 数据对象
*/

View File

@ -0,0 +1,52 @@
package com.imyeyu.spring.mapper;
import com.imyeyu.spring.util.RawSQLProvider;
import org.apache.ibatis.annotations.SelectProvider;
import java.util.List;
/**
* 原始 SQL 映射
*
* @param <T> 实体类型
* @param <P> 主键类型
* @author 夜雨
* @since 2026-01-05 12:58
*/
public interface RawMapper<T, P> {
/**
* 查询全部数据
*
* @return 数据列表
*/
@SelectProvider(type = RawSQLProvider.class, method = "selectAll")
List<T> selectAllRaw();
/**
* 根据 ID 获取对象
*
* @param id 索引
* @return 数据对象
*/
@SelectProvider(type = RawSQLProvider.class, method = "select")
T selectRaw(P id);
/**
* 根据示例查询单条数据
*
* @param t 示例对象
* @return 数据对象
*/
@SelectProvider(type = RawSQLProvider.class, method = "selectByExample")
T selectByExampleRaw(T t);
/**
* 根据示例查询全部数据
*
* @param t 示例对象
* @return 数据列表
*/
@SelectProvider(type = RawSQLProvider.class, method = "selectAllByExample")
List<T> selectAllByExampleRaw(T t);
}

View File

@ -0,0 +1,49 @@
package com.imyeyu.spring.util;
import com.imyeyu.java.bean.timi.TimiException;
import com.imyeyu.spring.mapper.BaseMapper;
import org.apache.ibatis.builder.annotation.ProviderContext;
import java.util.stream.Collectors;
/**
* 原始 Mapper SQL 代理器
*
* @author 夜雨
* @since 2026-01-05 13:00
*/
public class RawSQLProvider extends SQLProvider {
@Override
public String selectAll(ProviderContext context) {
EntityMeta meta = getEntityMeta(context);
return "SELECT * FROM %s WHERE 1 = 1".formatted(meta.table);
}
@Override
public String select(ProviderContext context, Object id) {
EntityMeta meta = getEntityMeta(context);
TimiException.required(meta.idFieldColumn, "not found id field in %s".formatted(meta.entityClass));
return "SELECT %s FROM `%s` WHERE `%s` = #{%s}".formatted(meta.selectAllClause, meta.table, meta.idFieldColumn.columnName, id) + " LIMIT 1";
}
@Override
public String selectByExample(Object entity) {
return selectAllByExample(entity) + BaseMapper.LIMIT_1;
}
@Override
public String selectAllByExample(Object entity) {
EntityMeta meta = getEntityMeta(entity.getClass());
String conditionClause = meta.fieldColumnList.stream()
.filter(fc -> fc.isNotEmpty(entity))
.map(fc -> "`%s` = #{%s}".formatted(fc.columnName, fc.fieldName))
.collect(Collectors.joining(" AND "));
return "SELECT %s FROM `%s` WHERE %s".formatted(meta.selectAllClause, meta.table, conditionClause);
}
@Override
public String deleteAllByExample(Object entity) {
return super.deleteAllByExample(entity);
}
}