fix(pagination): auto-detect DbType for correct total counts on MySQL

This commit is contained in:
matevip 2026-04-24 06:55:19 +08:00
parent af8c2fe6a9
commit a8f4236d90
2 changed files with 21 additions and 5 deletions

View File

@ -1,6 +1,5 @@
package vip.mate;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
@ -33,12 +32,17 @@ public class MateClawApplication {
}
/**
* MyBatis Plus 分页插件
* MyBatis Plus pagination plugin.
*
* <p>DbType is auto-detected from the JDBC connection at runtime rather
* than hardcoded. Hardcoding H2 here meant the MySQL deployment used
* the H2 dialect for the count query, which silently returned 0
* frontends saw records but total=0 and couldn't paginate (RFC-042 P0).
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}

View File

@ -416,8 +416,20 @@ async function loadSkills() {
const res: any = await skillApi.page(params)
const data = res.data || {}
skills.value = data.records || []
total.value = data.total || 0
const records: Skill[] = Array.isArray(data.records) ? data.records : []
skills.value = records
// Defensive total: if the backend pagination count is broken (seen with the
// old hardcoded-H2 MyBatisPlus interceptor on MySQL), infer a floor so the
// user can at least reach the next page. The real total overrides this.
const reportedTotal = Number(data.total) || 0
const inferredMin = records.length >= query.size
? query.page * query.size + 1 // at least one more page exists
: (query.page - 1) * query.size + records.length
total.value = Math.max(reportedTotal, inferredMin)
if (records.length > 0 && reportedTotal === 0) {
// eslint-disable-next-line no-console
console.warn('[SkillMarket] backend returned records but total=0; rebuild server JAR to pick up the DbType fix')
}
} catch (e) {
skills.value = []
total.value = 0