From cb3685d084ea0b647a0d9a282cbbab40ab250809 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9D=B3=E5=B8=85?= <34823011+20144101037@users.noreply.github.com> Date: Fri, 6 Dec 2024 16:42:04 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90ISSUE=20#12911=E3=80=91=E5=B0=86for?= =?UTF-8?q?=E5=BE=AA=E7=8E=AF=E6=8B=BC=E6=8E=A5=E7=9A=84SQL=E8=AF=AD?= =?UTF-8?q?=E5=8F=A5=E6=94=B9=E4=B8=BALambda=E8=A1=A8=E8=BE=BE=E5=BC=8F?= =?UTF-8?q?=E6=8B=BC=E6=8E=A5=EF=BC=8C=E4=BD=BF=E5=BE=97=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=AE=80=E6=B4=81=E6=B8=85=E6=99=B0=EF=BC=8C=E5=B9=B6=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E7=9B=B8=E5=85=B3=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= =?UTF-8?q?=20(#12914)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(优化抽象类AbstractMapper) 将for循环拼接的SQL语句改为Lambda表达式拼接,使得代码简洁清晰,并添加相关测试用例 * [ISSUE #12914] 优化抽象类AbstractMapper,将for循环拼接的SQL语句改为Lambda表达式拼接,使得代码简洁清晰,并添加相关测试用例 * [ISSUE #12911] resolve code conflict --- .../datasource/mapper/AbstractMapper.java | 30 +++++-------------- .../datasource/mapper/AbstractMapperTest.java | 24 +++++++++++---- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapper.java b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapper.java index b86e481efc9..81c9adc8a87 100644 --- a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapper.java +++ b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapper.java @@ -19,6 +19,7 @@ import com.alibaba.nacos.common.utils.CollectionUtils; import java.util.List; +import java.util.stream.Collectors; /** * The abstract mapper contains CRUD methods. @@ -33,17 +34,9 @@ public String select(List columns, List where) { StringBuilder sql = new StringBuilder(); String method = "SELECT "; sql.append(method); - for (int i = 0; i < columns.size(); i++) { - sql.append(columns.get(i)); - if (i == columns.size() - 1) { - sql.append(" "); - } else { - sql.append(","); - } - } - sql.append("FROM "); + sql.append(columns.stream().collect(Collectors.joining(","))); + sql.append(" FROM "); sql.append(getTableName()); - sql.append(" "); if (CollectionUtils.isEmpty(where)) { return sql.toString(); @@ -111,7 +104,6 @@ public String update(List columns, List where) { return sql.toString(); } - sql.append(" "); appendWhereClause(where, sql); return sql.toString(); @@ -121,7 +113,7 @@ public String update(List columns, List where) { public String delete(List params) { StringBuilder sql = new StringBuilder(); String method = "DELETE "; - sql.append(method).append("FROM ").append(getTableName()).append(" "); + sql.append(method).append("FROM ").append(getTableName()); appendWhereClause(params, sql); return sql.toString(); @@ -134,9 +126,8 @@ public String count(List where) { sql.append(method); sql.append("COUNT(*) FROM "); sql.append(getTableName()); - sql.append(" "); - if (null == where || where.size() == 0) { + if (CollectionUtils.isEmpty(where)) { return sql.toString(); } @@ -150,13 +141,8 @@ public String[] getPrimaryKeyGeneratedKeys() { return new String[]{"id"}; } - protected void appendWhereClause(List where, StringBuilder sql) { - sql.append("WHERE "); - for (int i = 0; i < where.size(); i++) { - sql.append(where.get(i)).append(" = ").append("?"); - if (i != where.size() - 1) { - sql.append(" AND "); - } - } + private void appendWhereClause(List where, StringBuilder sql) { + sql.append(" WHERE "); + sql.append(where.stream().map(str -> (str + " = ?")).collect(Collectors.joining(" AND "))); } } diff --git a/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapperTest.java b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapperTest.java index 6531881e782..d2531e40a38 100644 --- a/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapperTest.java +++ b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapperTest.java @@ -49,9 +49,15 @@ public String getFunction(String functionName) { } }; } - + + @Test + void testSelectSingleField() { + String sql = abstractMapper.select(Arrays.asList("id"), Arrays.asList("id")); + assertEquals("SELECT id FROM tenant_info WHERE id = ?", sql); + } + @Test - void testSelect() { + public void testSelectMultiField() { String sql = abstractMapper.select(Arrays.asList("id", "name"), Arrays.asList("id")); assertEquals("SELECT id,name FROM tenant_info WHERE id = ?", sql); } @@ -67,12 +73,18 @@ void testUpdate() { String sql = abstractMapper.update(Arrays.asList("id", "name"), Arrays.asList("id")); assertEquals("UPDATE tenant_info SET id = ?,name = ? WHERE id = ?", sql); } - + @Test - void testDelete() { + public void testDeleteSingleField() { String sql = abstractMapper.delete(Arrays.asList("id")); assertEquals("DELETE FROM tenant_info WHERE id = ?", sql); } + + @Test + public void testDeleteMultiField() { + String sql = abstractMapper.delete(Arrays.asList("id", "name")); + assertEquals("DELETE FROM tenant_info WHERE id = ? AND name = ?", sql); + } @Test void testCount() { @@ -89,12 +101,12 @@ void testGetPrimaryKeyGeneratedKeys() { @Test void testSelectAll() { String sql = abstractMapper.select(Arrays.asList("id", "name"), null); - assertEquals("SELECT id,name FROM tenant_info ", sql); + assertEquals("SELECT id,name FROM tenant_info", sql); } @Test void testCountAll() { String sql = abstractMapper.count(null); - assertEquals("SELECT COUNT(*) FROM tenant_info ", sql); + assertEquals("SELECT COUNT(*) FROM tenant_info", sql); } }