refactor(SQL分层): TaskImageCacheMapper 9 条注解 SQL 迁进 mapper XML,读侧判据改逐接口钉

档 B 口径:自定义语句一律进 XML,"特别简单"只剩 BaseMapper 自带 CRUD 与单表主键/唯一键点查。

判据这次不只是改数字。读侧原来钉的是「总数 65 + 文件数 16」两个标量,那么 A 接口加一条、
B 接口减一条就能让两个标量双双不动而绕过闸门,等于档 B 下没有闸门。改成逐接口清单
SELECT_SQL_BASELINE,并加 thePerFileLedgerCatchesWhatTheScalarsCouldNot 自证:在内存里造出
那次搬家,先断言总数与文件数确实都不变(证明旧钉法失明),再断言逐接口清单必须不等。

复用片段抽在真重复处:WHERE url_hash IN <foreach> 被三条语句共用,抽成 byUrlHashIn。
没抽列清单是刻意的——这个表每条语句只取必要列,image_bytes 是 BLOB,整行回读会把省内存
的缓存变成内存负担。所以新守卫 TaskImageCacheMapperXmlTest 钉的第一条就是"只有两条直读
字节的语句能碰 image_bytes"(迁 SQL 最顺手写成 SELECT *,而它在没有库的单测里不会自然变红),
其余钉 &lt; 是否还原成 <、LIMIT ? 是否仍是绑定参数、NOW(3) 毫秒精度、byte[] 的 resultType
必须写 "[B"(别名表里没有它)。

基线同步递减:写侧 29→25、读侧 65→60,MIGRATED_TO_XML 加 TaskImageCacheMapper。
定向 47 例绿(判据 9 + 新守卫 7 + asincache 样板 6 + 两个消费方 service 25)。
既有接口是 CRLF,改动走二进制删行,没把整份文件翻成 LF。
This commit is contained in:
2026-10-08 10:29:36 +08:00
parent 207225eb86
commit 3ed8a4265d
4 changed files with 308 additions and 33 deletions
@@ -0,0 +1,182 @@
package com.nanri.aiimage.modules.task.mapper;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.nanri.aiimage.modules.task.model.entity.TaskImageCacheEntity;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.Configuration;
import org.junit.jupiter.api.Test;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* {@code TaskImageCacheMapper} 从注解迁到 mapper XML 后的等价性守卫。
*
* <p>这个 mapper 的行为不变量只有一条值得钉死:<b>任何语句都不许把 {@code image_bytes} 顺带拉回来</b>
* ——它是 BLOB,缓存表整行回读会把"省内存的缓存"变成内存负担。迁 SQL 时最容易顺手写成
* {@code SELECT *},而那在单测里不会红(没有库),所以这里直接对真 {@code BoundSql} 断言列形状。
*
* <p>另外两条是 XML 特有的失明面:{@code <} 忘了转义成 {@code &lt;} 会让文件解析失败(这条会红,
* 但红得没道理可寻),而注解+XML 同名只在启动期抛,本地不起容器看不见。
*/
class TaskImageCacheMapperXmlTest {
private static final String XML = "mapper/task/TaskImageCacheMapper.xml";
private static final String NS = "com.nanri.aiimage.modules.task.mapper.TaskImageCacheMapper";
private static final List<String> STATEMENT_IDS = List.of(
"touchLastUsed", "selectUrlHashes", "touchLastUsedBatch", "selectBytesByUrlHash",
"selectBytesByUrlHashes", "sumByteSize", "sumOldestBatchByteSize",
"deleteExpiredBatch", "deleteOldestBatch");
@Test
void xmlParsesAndBindsEveryInterfaceMethod() throws Exception {
Configuration configuration = parseXml();
for (String id : STATEMENT_IDS) {
assertTrue(configuration.hasStatement(NS + "." + id),
() -> XML + " 里没有 " + id + ":语句名必须与接口方法名逐字一致,否则调用时才报 no statement");
}
assertTrue(configuration.hasMapper(TaskImageCacheMapper.class),
"namespace 没能绑定到接口——检查包路径与 mybatis-plus.mapper-locations");
assertEquals(STATEMENT_IDS.size(), STATEMENT_IDS.stream()
.filter(id -> configuration.hasStatement(NS + "." + id)).count(),
"接口方法与 XML 语句数不齐");
}
@Test
void annotationsAreGoneSoTheStatementIsNotDefinedTwice() {
for (var method : TaskImageCacheMapper.class.getDeclaredMethods()) {
for (var annotation : List.of(org.apache.ibatis.annotations.Select.class,
org.apache.ibatis.annotations.Insert.class, org.apache.ibatis.annotations.Update.class,
org.apache.ibatis.annotations.Delete.class)) {
assertFalse(method.isAnnotationPresent(annotation),
() -> method.getName() + " 还带着 " + annotation.getSimpleName()
+ ":注解 + XML 同名会让 MyBatis 启动期抛重复语句");
}
}
}
/** 缓存表的本行不变量:只有两条直读字节的语句允许碰 image_bytes。 */
@Test
void onlyTheByteReadingStatementsTouchTheBlobColumn() throws Exception {
Configuration configuration = parseXml();
for (String id : List.of("touchLastUsed", "selectUrlHashes", "touchLastUsedBatch",
"sumByteSize", "sumOldestBatchByteSize", "deleteExpiredBatch", "deleteOldestBatch")) {
String sql = sqlOf(configuration, id, params(id));
assertFalse(sql.contains("image_bytes"),
() -> id + " 把 BLOB 列带回来了:" + sql);
}
assertTrue(sqlOf(configuration, "selectBytesByUrlHash", params("selectBytesByUrlHash"))
.contains("image_bytes"), "直读字节的语句必须真的取 image_bytes");
}
@Test
void theSharedInFragmentStillExpandsOnePlaceholderPerHash() throws Exception {
Configuration configuration = parseXml();
for (String id : List.of("selectUrlHashes", "touchLastUsedBatch", "selectBytesByUrlHashes")) {
BoundSql boundSql = statement(configuration, id).getBoundSql(Map.of("urlHashes", List.of("a", "b", "c")));
String sql = boundSql.getSql().replaceAll("\\s+", " ");
assertEquals(3, countOccurrences(sql, "?"),
() -> id + " 的 IN 长度不对(片段被改坏或 foreach 丢了): " + sql);
assertTrue(sql.contains("url_hash IN ( ? , ? , ? )"), () -> id + " 的 IN 形状变了: " + sql);
assertFalse(sql.contains("$"), () -> id + " 出现 ${} 拼接面");
}
}
/** 复用的代价要说清:片段写死了参数名,引用它的语句必须都带 @Param("urlHashes")。 */
@Test
void everyStatementUsingTheFragmentDeclaresTheHashesParam() throws Exception {
for (String id : List.of("selectUrlHashes", "touchLastUsedBatch", "selectBytesByUrlHashes")) {
BoundSql boundSql = statement(parseXml(), id).getBoundSql(Map.of("urlHashes", List.of("a")));
assertTrue(boundSql.getParameterMappings().stream()
.anyMatch(m -> "urlHashes".equals(m.getProperty()) || "__frch_hash_0".equals(m.getProperty())),
() -> id + " 没接上片段的 urlHashes 参数");
}
}
@Test
void limitStaysABoundParameterAndTheLessThanSurvivedEscaping() throws Exception {
Configuration configuration = parseXml();
String oldest = sqlOf(configuration, "sumOldestBatchByteSize", Map.of("limit", 20));
assertTrue(oldest.contains("LIMIT ?"), () -> "LIMIT 被写死成字面量了:" + oldest);
assertEquals(1, countOccurrences(oldest, "?"), "只该有 1 个 limit 占位符: " + oldest);
String expired = sqlOf(configuration, "deleteExpiredBatch", Map.of("cutoff", LocalDateTime.now(), "limit", 50));
// &lt; 还原成 < 才对;残留 &lt; 说明转义写进了 SQL 本体
assertTrue(expired.contains("last_used_at < ?"), () -> "删除条件里的 < 没还原:" + expired);
assertFalse(expired.contains("&lt;"), () -> "XML 转义漏进了 SQL:" + expired);
assertTrue(expired.contains("ORDER BY last_used_at LIMIT ?"),
() -> "LRU 顺序删变成了全表删风险:" + expired);
}
@Test
void now3PrecisionAndResultTypesSurviveTheMove() throws Exception {
Configuration configuration = parseXml();
for (String id : List.of("touchLastUsed", "touchLastUsedBatch")) {
assertEquals(1, countOccurrences(sqlOf(configuration, id, params(id)), "NOW(3)"),
() -> id + " 的 NOW(3) 精度丢了(退化成 NOW() 会丢毫秒,LRU 排序就不准了)");
}
assertSame(byte[].class, statement(configuration, "selectBytesByUrlHash").getResultMaps().get(0).getType(),
"byte[] 的 resultType 应为 \"[B\"");
assertSame(String.class, statement(configuration, "selectUrlHashes").getResultMaps().get(0).getType());
assertSame(TaskImageCacheEntity.class,
statement(configuration, "selectBytesByUrlHashes").getResultMaps().get(0).getType());
assertSame(Long.class, statement(configuration, "sumByteSize").getResultMaps().get(0).getType());
}
private static MappedStatement statement(Configuration configuration, String id) {
return configuration.getMappedStatement(NS + "." + id);
}
private static String sqlOf(Configuration configuration, String id, Object parameter) {
return statement(configuration, id).getBoundSql(parameter).getSql().replaceAll("\\s+", " ").trim();
}
/** 各语句按自己的 @Param 形状喂,缺一个 MyBatis 会在取 BoundSql 时就抛。 */
private static Map<String, Object> params(String id) {
Map<String, Object> parameter = new HashMap<>();
switch (id) {
case "touchLastUsed", "selectBytesByUrlHash" -> parameter.put("urlHash", "h1");
case "selectUrlHashes", "touchLastUsedBatch", "selectBytesByUrlHashes" ->
parameter.put("urlHashes", List.of("h1", "h2"));
case "sumOldestBatchByteSize", "deleteOldestBatch" -> parameter.put("limit", 10);
case "deleteExpiredBatch" -> {
parameter.put("cutoff", LocalDateTime.now());
parameter.put("limit", 10);
}
default -> {
}
}
return parameter;
}
private static Configuration parseXml() throws Exception {
MybatisConfiguration configuration = new MybatisConfiguration();
try (InputStream in = Resources.getResourceAsStream(XML)) {
new XMLMapperBuilder(in, configuration, XML, configuration.getSqlFragments()).parse();
}
return configuration;
}
private static int countOccurrences(String haystack, String needle) {
int at = 0;
int count = 0;
while ((at = haystack.indexOf(needle, at)) >= 0) {
count++;
at += needle.length();
}
return count;
}
}