Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(优化抽象类AbstractMapper) 将for循环拼接的SQL语句改为Lambda表达式拼接,使得代码简洁清晰,并添加相关测试用例 #12914

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -33,17 +34,9 @@ public String select(List<String> columns, List<String> 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();
Expand Down Expand Up @@ -111,7 +104,6 @@ public String update(List<String> columns, List<String> where) {
return sql.toString();
}

sql.append(" ");
appendWhereClause(where, sql);

return sql.toString();
Expand All @@ -122,12 +114,7 @@ public String delete(List<String> params) {
StringBuilder sql = new StringBuilder();
String method = "DELETE ";
sql.append(method).append("FROM ").append(getTableName()).append(" ").append("WHERE ");
for (int i = 0; i < params.size(); i++) {
sql.append(params.get(i)).append(" ").append("=").append(" ? ");
if (i != params.size() - 1) {
sql.append("AND ");
}
}
sql.append(params.stream().map(str -> (str + " = ?")).collect(Collectors.joining(" AND ")));

return sql.toString();
}
Expand All @@ -139,9 +126,8 @@ public String count(List<String> 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();
}

Expand All @@ -156,12 +142,7 @@ public String[] getPrimaryKeyGeneratedKeys() {
}

private void appendWhereClause(List<String> 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 ");
}
}
sql.append(" WHERE ");
sql.append(where.stream().map(str -> (str + " = ?")).collect(Collectors.joining(" AND ")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -67,11 +73,17 @@ 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);
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
Expand All @@ -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);
}
}
Loading