Commit 5763962b authored by wujipeng's avatar wujipeng
Browse files

范围树-优化

parent f945d079
...@@ -119,7 +119,12 @@ ...@@ -119,7 +119,12 @@
or coalesce(sl.name,c.level) like concat('%',#{keyword},'%') or coalesce(sl.name,c.level) like concat('%',#{keyword},'%')
or c.name like concat('%',#{keyword},'%')) or c.name like concat('%',#{keyword},'%'))
</if> </if>
group by c.occupation, coalesce(sl.name,c.level), c.id, c.name <!--
sl.id 是等级树的稳定排序键。生产环境启用 ONLY_FULL_GROUP_BY 时,ORDER BY 中直接引用的
非聚合字段必须同时出现在 GROUP BY 中,因此这里显式加入 sl.id;c.id 仍保证按班级聚合,
该调整只满足严格分组规则,不改变每个班级的人数统计粒度和现有等级顺序。
-->
group by c.occupation, coalesce(sl.name,c.level), sl.id, c.id, c.name
order by c.occupation, sl.id, c.name, c.id order by c.occupation, sl.id, c.name, c.id
</select> </select>
</mapper> </mapper>
...@@ -18,6 +18,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; ...@@ -18,6 +18,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
class StudentCertificationMapperXmlTest { class StudentCertificationMapperXmlTest {
private static final String RESOURCE="com/platform/business/mapper/StudentCertificationMapper.xml"; private static final String RESOURCE="com/platform/business/mapper/StudentCertificationMapper.xml";
private static final String PAGE="com.platform.business.mapper.StudentCertificationMapper.selectPage"; private static final String PAGE="com.platform.business.mapper.StudentCertificationMapper.selectPage";
private static final String SCOPE="com.platform.business.mapper.StudentCertificationMapper.selectScope";
@Test void pageShouldOnlyReturnCompletedStudentsAndMatchNameOrIdCard() throws Exception { @Test void pageShouldOnlyReturnCompletedStudentsAndMatchNameOrIdCard() throws Exception {
StudentCertificationQueryDTO query=new StudentCertificationQueryDTO(); query.setKeyword("张"); StudentCertificationQueryDTO query=new StudentCertificationQueryDTO(); query.setKeyword("张");
...@@ -34,6 +35,19 @@ class StudentCertificationMapperXmlTest { ...@@ -34,6 +35,19 @@ class StudentCertificationMapperXmlTest {
assertTrue(!sql.contains("u.nick_name like")); assertTrue(!sql.contains("u.nick_name like"));
} }
/**
* 范围树必须兼容生产库的 ONLY_FULL_GROUP_BY 配置:等级主键既用于稳定排序,也必须显式参与分组。
* 同时传入关键字以覆盖 selectScope 的动态筛选分支,防止修复后查询条件被意外遗漏。
*/
@Test void scopeShouldGroupByLevelIdAndPreserveLevelOrdering() throws Exception {
String sql=compact(boundScopeSql("污水").getSql());
assertTrue(sql.contains("group by c.occupation, coalesce(sl.name,c.level), sl.id, c.id, c.name"));
assertTrue(sql.contains("order by c.occupation, sl.id, c.name, c.id"));
assertTrue(sql.contains("c.occupation like concat('%',?,'%')"));
assertTrue(sql.contains("coalesce(sl.name,c.level) like concat('%',?,'%')"));
assertTrue(sql.contains("c.name like concat('%',?,'%')"));
}
private BoundSql boundSql(String statement,StudentCertificationQueryDTO query,java.util.List<String> ids) throws Exception { private BoundSql boundSql(String statement,StudentCertificationQueryDTO query,java.util.List<String> ids) throws Exception {
Configuration configuration=new Configuration(); Configuration configuration=new Configuration();
try(InputStream input=Resources.getResourceAsStream(RESOURCE)) { try(InputStream input=Resources.getResourceAsStream(RESOURCE)) {
...@@ -42,5 +56,15 @@ class StudentCertificationMapperXmlTest { ...@@ -42,5 +56,15 @@ class StudentCertificationMapperXmlTest {
Map<String,Object> params=new HashMap<>(); params.put("query",query); params.put("studentIds",ids); Map<String,Object> params=new HashMap<>(); params.put("query",query); params.put("studentIds",ids);
return configuration.getMappedStatement(statement).getBoundSql(params); return configuration.getMappedStatement(statement).getBoundSql(params);
} }
/** 构造范围树查询的 BoundSql,使测试直接校验 MyBatis 动态 SQL 的最终展开结果。 */
private BoundSql boundScopeSql(String keyword) throws Exception {
Configuration configuration=new Configuration();
try(InputStream input=Resources.getResourceAsStream(RESOURCE)) {
new XMLMapperBuilder(input,configuration,RESOURCE,configuration.getSqlFragments()).parse();
}
Map<String,Object> params=new HashMap<>(); params.put("keyword",keyword);
return configuration.getMappedStatement(SCOPE).getBoundSql(params);
}
private String compact(String sql) { return sql.replaceAll("\\s+"," ").trim(); } private String compact(String sql) { return sql.replaceAll("\\s+"," ").trim(); }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment